这可能是一个奇怪的问题,但我试图将一些数据写入Excel工作表,以便根据从不同工作表中读取的数据制定计划。我目前已经设置好了,所以我可以编写一个DataFrame并根据信息更改单元格的颜色。现在我想在保持颜色的同时在这些单元格中添加新信息。
import pandas as pd, datetime as dt
import glob, os
import math
runDir = "/Users/AaronsMac/Documents/"
if os.getcwd() != runDir:
os.chdir(runDir)
files = glob.glob("edcLineup_*.xlsx")
schedule = glob.glob("edcSetTime*.xlsx")
print schedule
for each in files:
sheets = pd.ExcelFile(each).sheet_names
sheets = sorted(sheets)
df = {}
for sheet in sheets:
df = pd.read_excel(each, sheet, index_col='Artists')
#print df
colNames = list(df.columns.values)
artists = list(df.index.values)
# print colNames
# print artists
for name in colNames:
schedule_sheets = pd.ExcelFile(schedule[0]).sheet_names
scheduleData = {}
for s_sheet in schedule_sheets:
scheduleData[s_sheet] = pd.read_excel(schedule[0],s_sheet, index_col='Time')
finalSched = pd.concat(scheduleData).unstack(0)
stages = list(finalSched.columns.values)
indSched = finalSched.copy()
asciiName = name.encode('ascii','ignore')
for stage in stages:
for dj in artists:
for time,prevtime in zip(list(finalSched.index.values),list(finalSched.index.values)[1:]):
if type(indSched.at[time,stage]) == float:
if math.isnan(indSched.at[time,stage]):
indSched.at[time,stage] = indSched.at[prevtime,stage]
finalSched.at[time,stage] = finalSched.at[prevtime,stage]
if type(indSched.at[time,stage]) == str or type(indSched.at[time,stage]) == unicode:
if indSched.at[time,stage].lower() == dj.lower():
indSched.at[time,stage] = df.at[dj,name]
writer = pd.ExcelWriter(asciiName+'_schedule.xlsx',engine='xlsxwriter')
indSched.to_excel(writer,sheet_name=s_sheet)
workbook = writer.book
worksheet = writer.sheets[s_sheet]
worksheet.conditional_format('B3:Y48',{'type': '3_color_scale'})
writer.save()`
以上是我目前的代码(我确定它的效率相当低,但我只是想让目前正常工作)。我已尝试执行以下操作来覆盖数据,但它给了我一个excel错误,说我需要修复文件才能查看它。它也消除了着色。
indSched.to_excel(writer,sheet_name=s_sheet)
workbook = writer.book
worksheet = writer.sheets[s_sheet]
worksheet.conditional_format('B3:Y48',{'type': '3_color_scale'})
finalSched.to_excel(writer,sheet_name=s_sheet)
writer.save()
我有点难过,也许这是完全错误的做事方式,但只是想知道是否有人有一些建议。