我应该如何使用 Pandas 在 Excel 中设置一组单元格的颜色?
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
axis=1)
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
workbook = writer.book
worksheet = workbook.add_worksheet('Summary')
writer.sheets['Summary'] = worksheet
format = workbook.add_format({'bg_color': '#21AD25'})
worksheet.set_row(0, cell_format= format) # set the color of the first row to green
df.to_excel(writer, sheet_name='Summary', index=False)
writer.save()
上面的代码可以改变第一行的颜色。但是,数据帧标题的背景颜色没有改变。
www说
相关分类