我在pandas中使用.to_excel方法在excel文件中写入数据帧 . 但是,我想更改默认格式 . Apply styles while exporting to 'xlsx' in pandas with XlsxWriter的答案有助于基本格式化 .

但是,我想更改 Headers 单元格的单元格背景颜色,所以我尝试了

import pandas.core.format
header_style_backup = pandas.core.format.header_style
pandas.core.format.header_style = {"font": {"bold": True"},
                                   "borders": {},
                                   "pattern": {"bg_color": "green"},
                                   "alignment": {"horizontal": "left", "vertical": "top"}}

但是,“bg_color”不会显示在输出xlsx文件中 . 有没有其他方法来指定这个?

或者一般情况下,我可以使用xlsxwriter将pandas导出的数据帧的背景颜色更改为xlsx吗?