Applying styles to the headers of pandas-generated excel files

荒凉一梦 提交于 2019-12-13 02:11:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/35397783/applying-styles-to-the-headers-of-pandas-generated-excel-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!