我能够将数据从datafile.xlsx传输到test.xlsx,但是我不知道如何将红色添加到test.xlsx中的特定单元格
#代码的主要组件如下所示
import pandas as pd
import xlsxwriter
file = "datafile.xlsx"
xlsx = pd.ExcelFile(file)
sheet1 = xlsx.parse(0)
writer = pd.ExcelWriter("test.xlsx")
columns=["FirstName", "MiddleName", "LastName", ......]
#--My code doesn't work when a sheet_name is added in the to_excel piece--
dfc = pd.DataFrame([columns])
dfc.to_excel(writer,sheet_name=sheet1, startrow=0, startcol=0,
index=False, header=None)
#The body of the code retrieves data from datafile.xlsx
#example--> row = sheet1.iloc[i].real
#---This section has my new code for adding cell color-----
#---cell color code(I define a workbook and worksheet)
workbook = writer.book
worksheet = writer.sheets['sheet1']
cell_format = workbook.add_format()
cell_format.set_font_color('red')
worksheet.write(0, 0, 'Foo', cell_format)
#----cell color code
dt = [row[0], row[1], row[2], .....]
df1 = pd.DataFrame([dt])
df1.to_excel(writer, startrow=rownum, startcol=0, index=False, header=None)
writer.save()
答案 0 :(得分:0)
如果您要更改单元格的颜色而不是字体的颜色,请更改
cell_format.set_font_color('red')
对于
cell_format.set_bg_color('red')