【问题标题】:openpyxl conditional formatting - Rule is in Excel file but not the formattingopenpyxl 条件格式 - 规则在 Excel 文件中,但不是格式
【发布时间】:2019-12-12 00:36:25
【问题描述】:

我的目标是创建一个 Excel 文件,并使用 openpyxl 使用条件格式根据它们的值更改某些单元格的背景颜色。

当我打开使用 Excel 创建的文件时,我可以看到该规则存在,但该规则不包括要应用的格式(背景颜色设置为无)。因此,单元格没有背景颜色,尽管与公式有关的单元格的边框不可见,就像背景为白色时一样。 不知道是不是我写错了,还是openpyxl有问题。

这是一个 MWE:

from openpyxl import Workbook
from openpyxl.styles import PatternFill
from openpyxl.formatting.rule import CellIsRule

wb = Workbook()
ws = wb.active

ws['B2'] = -2
ws['B3'] = -1
ws['B4'] =  0
ws['C2'] = -1
ws['C3'] =  0
ws['C4'] =  1

fill = PatternFill(start_color='538DD5', fill_type='solid')
ws.conditional_formatting.add('B2:C4', CellIsRule(operator='lessThan', formula=[0], fill=fill))

wb.save('mwe.xlsx')
wb.close()

【问题讨论】:

标签: python excel openpyxl conditional-formatting


【解决方案1】:

您需要像这样添加参数 end_color :

fill = PatternFill(start_color='538DD5',end_color='538DD5',fill_type='solid')

查看此链接:https://openpyxl.readthedocs.io/en/stable/formatting.html

from openpyxl import Workbook
from openpyxl.styles import PatternFill
from openpyxl.formatting.rule import CellIsRule

wb = Workbook()
ws = wb.active

ws['B2'] = -2
ws['B3'] = -1
ws['B4'] =  0
ws['C2'] = -1
ws['C3'] =  0
ws['C4'] =  1

fill = PatternFill(start_color='538DD5',end_color='538DD5',fill_type='solid')
#print(fill)
ws.conditional_formatting.add('B2:C4', CellIsRule(operator='lessThan', formula=[0], fill=fill))
wb.save('mwe.xlsx')
wb.close()

结果:

【讨论】:

  • 谢谢,它解决了这个问题。我没有使用 end_color 参数,因为在进行简单格式化时它可以正常工作。
  • @James23 我们在这里寻求帮助
【解决方案2】:

在@GiovaniSalazar 回答之后,我做了更多测试。 用于颜色的参数(start_color、end_color、fgColor、bgColor)与条件格式和简单格式(openpyxl 中的错误?)的行为不同。

这是两者的比较。唯一适用于两种格式的是 start_color + end_color。

from openpyxl import Workbook
from openpyxl.styles import PatternFill
from openpyxl.formatting.rule import CellIsRule

wb = Workbook()
ws = wb.active

ws['C2'] = -4
ws['C3'] = -3
ws['C4'] = -2
ws['C5'] = -1
ws['D2'] =  4
ws['D3'] =  3
ws['D4'] =  2
ws['D5'] =  1


ws['C1'] = 'Cond. formatting'
ws['F1'] = 'Formatting'

ws['A2'] = 'start+end'
fill = PatternFill(start_color='538DD5', end_color='538DD5', fill_type='solid')
# OK
ws.conditional_formatting.add('C2:D2', CellIsRule(operator='lessThan', formula=[0], fill=fill))
# OK
ws['F2'].fill = fill


ws['A3'] = 'start'
fill = PatternFill(start_color='538DD5', fill_type='solid')
# Problem (white background)
ws.conditional_formatting.add('C3:D3', CellIsRule(operator='lessThan', formula=[0], fill=fill))
# OK
ws['F3'].fill = fill


ws['A4'] = 'fgColor'
fill = PatternFill(fgColor='538DD5', fill_type='solid')
# Problem (white background)
ws.conditional_formatting.add('C4:D4', CellIsRule(operator='lessThan', formula=[0], fill=fill))
# OK
ws['F4'].fill = fill


ws['A5'] = 'bgColor'
fill = PatternFill(bgColor='538DD5', fill_type='solid')
# OK
ws.conditional_formatting.add('C5:D5', CellIsRule(operator='lessThan', formula=[0], fill=fill))
# Problem (black background)
ws['F5'].fill = fill


wb.save('mwe.xlsx')
wb.close()

输出 Excel 文件:

Output Excel file

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2015-12-03
    • 2019-04-01
    相关资源
    最近更新 更多