简体   繁体   English

Python Pandas:无条件导出为Excel格式的“粗体”行

[英]Python Pandas: export to excel format a row 'Bold' without condition

I have what I feel could be a silly question but it seems I can't find the answer: 我有一个我觉得可能是个愚蠢的问题,但似乎找不到答案:

  • How to, while exporting to Excel, set a row/range to bold? 如何在导出到Excel时将行/范围设置为粗体?

I know how to conditional format eg 我知道如何条件格式例如

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

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

or even to format columns: 甚至格式化列:

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

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

But let's say I want for row A1:A40 to be written in bold, whitout any particular criteria? 但是,假设我想将A1:A40行写成粗体,没有任何特定条件? I just want to set to bold or a color or any formatting a given range whatever data it may contain. 我只想将其包含的任何数据设置为粗体或颜色或给定范围的任何格式。

In this case, for instance, apply 'format1' to range A1:A40 whatever the values. 例如,在这种情况下,无论值如何,都将“ format1”应用于范围A1:A40。

Does anyone know the answer? 有人知道答案吗?

It looks like there isn't a helper function specifically designed for this, according to github . 根据github的说法,似乎没有专门为此设计的帮助程序功能。

But Gabriel's answer to a similar question proposes a workaround, where you use two (or more) conditional formats to cover all values: 但是加百列(Gabriel)对类似问题的回答提出了一种解决方法,其中您使用两种(或多种)条件格式来覆盖所有值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM