【问题标题】:Python Pandas: export to excel format a row 'Bold' without conditionPython Pandas:导出到 excel 格式一行“粗体”,无条件
【发布时间】:2018-10-04 22:42:03
【问题描述】:

我有一个我觉得可能是个愚蠢的问题,但我似乎找不到答案:

  • 如何在导出到 Excel 时将行/范围设置为粗体?

我知道如何设置条件格式,例如

    format1 = workbook.add_format({'bg_color': '#FFC7CE',
                                   'font_color': '#9C0006'})
...

        worksheet.conditional_format('C2:AF41', {'type': 'cell', 'criteria': '<', 'value': 0, 'format': format1})

甚至格式化列:

    border_format = workbook.add_format({
                                'border': 1,
                                'align': 'center',
                                'font_size': 10
                               })
...

        worksheet.set_column(0, 0, 30, border_format)

但是假设我希望行 A1:A40 以粗体书写,没有任何特定标准?我只想将给定范围内可能包含的任何数据设置为粗体或颜色或任何格式。

在这种情况下,例如,将“format1”应用于范围 A1:A40,无论值如何。

有人知道答案吗?

【问题讨论】:

标签: python pandas


【解决方案1】:

根据github,似乎没有专门为此设计的辅助函数。

但Gabriel's answer to a similar question 提出了一种解决方法,您可以使用两种(或更多)条件格式来覆盖所有值:

worksheet.conditional_format('A1:A40', {'type': 'cell', 
    'criteria': '>=', 'value': 0, 'format': format1})
worksheet.conditional_format('A1:A40', {'type': 'cell', 
    'criteria': '<', 'value': 0, 'format': format1})

【讨论】:

  • 所以没有没有条件的“普通”格式作为此类辅助函数:worksheet.format('A1:A40', {'bold': True}。我已经看到了那个答案,但老实说当时并没有完全明白:)。要应用 2 种条件格式来覆盖所有值,我现在明白了,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多