Applying styles to the headers of pandas-generated excel files
Asked Answered
L

1

1

Reviewing Tim Hoffman's answer to this broader question made me want to make my headers wrap.

I tried this with the following code:

import pandas.core.format
pandas.core.format.header_style["alignment"].update({"text_wrap" : True})
pandas.core.format.header_style["font"].update({"bold" : False})

Now, the actual result is NOT bolded, so I know I'm managing to overwrite the default. However, the text is not wrapping.

Based on what I see in the xlsxwriter format class guide and that there's already an alignment keyword...

ipdb> pandas.core.format.header_style["alignment"]
{'horizontal': 'center', 'vertical': 'top'} 

...and that after I modify it as per above...

ipdb> pandas.core.format.header_style["alignment"].update({"text_wrap" : True})
ipdb> pandas.core.format.header_style["alignment"]
{'horizontal': 'center', 'text_wrap': True, 'vertical': 'top'}

...I would expect to get the result, but I'm not. Anyone know why?

I gather that I could also re-write the data using an xlsxwriter write method, but that feels like more work to me, so I'm hoping to make it work this way.

Lawrencelawrencium answered 14/2, 2016 at 20:45 Comment(0)
Z
0

I would expect to get the result, but I'm not. Anyone know why?

AFAIK the header_style dict that you are modifying is internal and isn't exposed via an API.

The header_style dict is in the xlwt easyxf format which is only partially mapped by the xlsxwriter engine in Pandas to get the default heading formats. The text_wrap property isn't one that is mapped (and because the format is internal I don't think it needs to be).

So overall I don't think you can rely on this workaround.

Zel answered 15/2, 2016 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.