【问题标题】:Applying styles to the headers of pandas-generated excel files将样式应用于 pandas 生成的 excel 文件的标题
【发布时间】:2016-02-15 18:45:57
【问题描述】:

查看Tim Hoffmanthis broader question 的回答让我想把我的标题换行。

我尝试了以下代码:

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

现在,实际结果不是粗体,所以我知道我正在设法覆盖默认值。但是,文本没有换行。

根据我在xlsxwriter format class guide 中看到的内容,并且已经有一个 alignment 关键字...

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

...按照上面的修改后...

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'}

...我希望得到结果,但我没有。有人知道为什么吗?

我知道我也可以使用 xlsxwriter 写入方法重新写入数据,但这对我来说感觉工作量更大,所以我希望以这种方式工作。 p>

【问题讨论】:

    标签: python-2.7 pandas xlsxwriter


    【解决方案1】:

    我希望得到结果,但我没有。有人知道为什么吗?

    AFAIK 您正在修改的 header_style dict 是内部的,不会通过 API 公开。

    header_style dict 是 xlwt easyxf 格式,它仅由 Pandas 中的 xlsxwriter 引擎部分映射以获得默认标题格式。 text_wrap 属性 isn't one that is mapped(因为格式是内部的,我认为不需要)。

    所以总的来说,我认为您不能依赖这种解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      相关资源
      最近更新 更多