将样式应用于 Pandas 生成的 excel 文件的标题 [英] Applying styles to the headers of pandas-generated excel files
问题描述
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.
根据我在 xlsxwriter 格式课程指南 中看到的内容,并且已经有对齐关键字...
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?
我认为我也可以使用 xlsxwriter 写入方法重写数据,但这对我来说感觉工作量更大,所以我希望以这种方式使其工作.>
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.
推荐答案
我希望得到结果,但我没有.有人知道为什么吗?
I would expect to get the result, but I'm not. Anyone know why?
AFAIK 你正在修改的 header_style
dict 是内部的,不是通过 API 公开的.
AFAIK the header_style
dict that you are modifying is internal and isn't exposed via an API.
header_style
dict 采用 xlwt
easyxf
格式,仅部分被 xlsxwriter
引擎映射Pandas 获取默认标题格式.text_wrap
属性 不是一个被映射的(并且因为格式是内部的,我认为它不需要).
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.
这篇关于将样式应用于 Pandas 生成的 excel 文件的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!