If you just want to style the header, you can modify pandas.io.formats.excel.header_style
. Of course, this is no general solution, but is an easy workaround for a common use-case.
import pandas.core.format
header_style_backup = pandas.io.formats.excel.header_style
try:
pandas.io.formats.excel.header_style = {"font": {"bold": True},
"borders": {"top": "thin", "right": "thin", "bottom": "thin", "left": "thin"},
"pattern": {"pattern": "solid", "fore_colour": 26},
"alignment": {"horizontal": "center", "vertical": "top"}}
df.to_excel(writer, sheet_name=sheetname, startrow=table_startrow)
finally:
pandas.formats.format.header_style = header_style_backup
Note: The location of header_style has been changing multiple times in prior pandas versions. Use the following for older versions:
version < 0.20.0 pandas.formats.format.header_style
version < 0.18.0 pandas.core.format.header_style