python 中的条件背景颜色 - python 的新功能

conditional background color in python - new to python

我正在尝试将工作脚本写入 excel,从 Python 到 excel。我已经必须格式化列的大小和类型,但我需要其中一列具有条件颜色。
前任。/ A列有百分比。 对于所有 < 0% 的细胞,我们需要细胞为红色 对于所有单元格 = 0%,我们需要单元格为黄色 对于所有 > 0% 的细胞,我们需要细胞为绿色

然后,每一列的第一行需要是蓝色的。

我发现了类似的脚本,但它们不是有条件的。

我尝试了

的几种变体
# Light red fill.
format1 = name.add_format({'bg_color':   '#FFC7CE'})

# Light yellow fill.
format2 = name.add_format({'bg_color':   '#FFEB9C'})

# Green fill.
format3 = name.add_format({'bg_color':   '#C6EFCE'})

try:
    sheet1.conditional_format('A:A', {'type': 'cell',
                                      'criteria': '<',
                                      'value': 0,
                                      'format': format1})


    sheet1.conditional_format('A:A', {'type': 'cell',
                                      'criteria': '==',
                                      'value': 0,
                                      'format': format2})

    sheet1.conditional_format('A:A', {'type': 'cell',
                                      'criteria': '>',
                                      'value': 0,
                                      'format': format3})

except AttributeError: 
    sheet1.conditional_format = None

except TypeError:
    type = None

但它不会改变颜色

并且我尝试了

的几种变体
while 'BU:BU' != " " :
    if 'BU:BU' < '0%':
        sheet1.style = redFill
    elif 'BU:BU' == '0%':
        sheet1.style = yellowFill
    elif 'BU:BU' > '0%':
        sheet1.style = '#C6EFCE'
    else: 
        pass 

continue

但这一直陷入无限循环。

谢谢

我想通了。为了使其工作,三种格式不能在同一个单元格中。除了例外,每个都需要独立。

此外,需要根据它 运行 在多少个单元格上设置边界。如果它是运行多次且行数不断变化的东西,你可以射高。

一个单元格:

# Light red fill.
format1 = name.add_format({'bg_color':   '#FFC7CE'})

try:
    sheet1.conditional_format('A1:A900', {'type': 'cell',
                                      'criteria': '<',
                                      'value': 0,
                                      'format': format1})

except AttributeError: 
    sheet1.conditional_format = None

except TypeError:
    type = None

另一个单元格:

# Light yellow fill.
format2 = name.add_format({'bg_color':   '#FFEB9C'})

...