I'm saving pandas DataFrame to_excel using xlsxwriter. I've managed to format all of my data (set column width, font size etc) except for changing header's font and I can't find the way to do it. Here's my example:
import pandas as pd
data = pd.DataFrame({'test_data': [1,2,3,4,5]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
data.to_excel(writer, sheet_name='test', index=False)
workbook = writer.book
worksheet = writer.sheets['test']
font_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10})
header_fmt = workbook.add_format({'font_name': 'Arial', 'font_size': 10, 'bold': True})
worksheet.set_column('A:A', None, font_fmt)
worksheet.set_row(0, None, header_fmt)
writer.save()
The penultimate line that tries to set format for the header does nothing.
An update for anyone who comes across this post and is using Pandas 0.20.1.
It seems the required code is now
Apparently the
excel
submodule isn't imported automatically, so simply tryingpandas.io.formats.excel.header_style = None
alone will raise anAttributeError
.I think you need first reset default header style, then you can change it:
All together:
Explaining by jmcnamara, thank you:
In Excel a cell format overrides a row format overrides a column format.The
pd.core.format.header_style
is converted to a format and is applied to each cell in the header. As such the default cannot be overridden byset_row()
. Settingpd.core.format.header_style
toNone
means that the header cells don't have a user defined format and thus it can be overridden byset_row()
.EDIT: In version
0.18.1
you have to changeto:
thanks krvkir.
In pandas 0.20 the solution of the accepted answer changed again.
The format that should be set to None can be found at: