pandas xlsxwriter, format header

28,893

Solution 1

I think you need first reset default header style, then you can change it:

pd.core.format.header_style = None

All together:

import pandas as pd

data = pd.DataFrame({'test_data': [1,2,3,4,5]})
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')

pd.core.format.header_style = None

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()

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 by set_row(). Setting pd.core.format.header_style to None means that the header cells don't have a user defined format and thus it can be overridden by set_row().

EDIT: In version 0.18.1 you have to change

pd.core.format.header_style = None

to:

pd.formats.format.header_style = None

EDIT: from version 0.20 this changed again

import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None

thanks krvkir.

EDIT: from version 0.24 this is now required

import pandas.io.formats.excel
pandas.io.formats.excel.ExcelFormatter.header_style = None

thanks Chris Vecchio.

Solution 2

An update for anyone who comes across this post and is using Pandas 0.20.1.

It seems the required code is now

import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None

Apparently the excel submodule isn't imported automatically, so simply trying pandas.io.formats.excel.header_style = None alone will raise an AttributeError.

Solution 3

Another option for Pandas 0.25 (probably also 0.24). Likely not the best way to do it, but it worked for me.

import pandas.io.formats.excel
pandas.io.formats.excel.ExcelFormatter.header_style = None

Solution 4

for pandas 0.24:

The below doesn't work anymore:

import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None

Instead, create a cell formatting object, and re-write the first row's content (your header) one cell at a time with the new cell formatting object.
Now, you are future proof.

Use the following pseudo code:

# [1] write df to excel as usual
writer = pd.ExcelWriter(path_output, engine='xlsxwriter')
df.to_excel(writer, sheet_name, index=False)

# [2] do formats for other rows and columns first

# [3] create a format object with your desired formatting for the header, let's name it: headercellformat

# [4] write to the 0th (header) row **one cell at a time**, with columnname and format
for columnnum, columnname in enumerate(list(df.columns)):
    worksheet.write(0, columnnum, columnname, headercellformat)

Solution 5

In pandas 0.20 the solution of the accepted answer changed again.

The format that should be set to None can be found at:

pandas.io.formats.excel.header_style
Share:
28,893
Egor Ignatenkov
Author by

Egor Ignatenkov

Python, R, MongoDB, Data Science, Bigdata, Machine Learning, Math.

Updated on February 18, 2021

Comments

  • Egor Ignatenkov
    Egor Ignatenkov about 3 years

    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.

  • rahlf23
    rahlf23 over 4 years
    This answer should be updated to reflect the contribution by Chris Vecchio below: stackoverflow.com/a/58033197/8146556
  • naccode
    naccode almost 4 years
    This is the recommended way according to docs (xlsxwriter.readthedocs.io/example_pandas_header_format.html‌​). AND, it prevents from formatting the whole row (up to XFD column)