问题描述
我有一个制表符分隔的文件,我添加了一些格式并写出一个Excel文件。一种要求是对备用行进行颜色编码,但文件中有一个doc_no列,对于具有相同doc_no的行,它们的颜色必须相同。就像下面的示例一样:
在交替行用颜色编码和重复doc_nos用高亮显示的情况下(我在示例中使用的颜色是为了说明,可能与代码中的颜色不匹配)。我不确定如何获得请求的结果,将不胜感激。
我的代码:
import pandas as pd
import numpy as np
import time
infile = (r'C:\..\test.txt')
#read in tab delimited text file into dataframe
df = pd.read_csv(infile,sep='\t',dtype = str,encoding='cp1252')
#delete columns with all null values
df.dropna(how='all',axis='columns',inplace=True)
#Get Date Range into variable date_range to print as HEADER and remove column from dataframe
date_range = df.iloc[0,0]
df.drop('Date Range',axis=1,inplace=True)
#replace NAN with empty string
df = df.replace(np.nan,'',regex=True)
dtstamp = time.strftime('%m-%d-%Y')
#Writing dataframe to Excel
writer = pd.ExcelWriter(r'C:\..\TEST-'+dtstamp+'.xlsx',engine = 'xlsxwriter')
df.to_excel(writer,sheet_name = 'Sheet1',startrow=2,index=False)
workbook = writer.book
ws = writer.sheets['Sheet1']
#--------------------------color code rows-------------------------------------
rcolor1 = workbook.add_format({'bg_color': '#D3D3D3'})
rcolor2 = workbook.add_format({'bg_color': '#FFE4E1'})
for row in range(3,(len(df.index)+3),2):
ws.set_row(row,cell_format=rcolor2)
ws.conditional_format('B3:B'+str(len(df.index)+3),{'type': 'duplicate','format': rcolor1})
...
writer.save()
workbook.close()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)