我尝试将dataframe写入xlsx并赋予其颜色。我使用
worksheet.conditional_format('A1:C1', {'type': '3_color_scale'})
但它不会给细胞着色。我想给这个单元格一种颜色。我看到了cell_format.set_font_color('#FF0000')
,但没有指定单元格数
sex = pd.concat([df2[["All"]],df3], axis=1)
excel_file = 'example.xlsx'
sheet_name = 'Sheet1'
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
sex.to_excel(writer, sheet_name=sheet_name, startrow=1)
workbook = writer.book
worksheet = writer.sheets[sheet_name]
format = workbook.add_format()
format.set_pattern(1)
format.set_bg_color('gray')
worksheet.write('A1:C1', 'Ray', format)
writer.save()
我需要给A1:C1
赋值颜色,但是我应该把name
赋值给cell。如何绘制我的df的多个单元格?
发布于 2016-07-28 10:00:27
问题是worksheet.write('A1:C1', 'Ray', format)
仅用于写入单个单元格。在一行中写入更多单元格的一个可能的解决方案是使用write_row()
。
worksheet.write_row("A1:C1", ['Ray','Ray2','Ray3'], format)
请记住,write_row()接受要写入单元格的字符串列表。
如果使用worksheet.write_row("A1:C1", 'Ray', format)
,则在第一个单元格中有R,在第二个单元格中有a,在第三个单元格中有y。
发布于 2021-03-17 19:50:53
cf = workbook.add_format({'bg_color': 'yellow'})
worksheet.write('A1', 'Column name', cf)
https://stackoverflow.com/questions/38632753
复制