Jes*_*son 4 python python-2.7 openpyxl
我有一个使用 openpyxl 生成的电子表格,其中包含许多系统检查。基于规则;单词通过、失败或信息插入到我的电子表格的 E 列中。我想使用 Openpyxl 根据 Pass 或 Fail 的值有条件地格式化电子表格的填充。像绿色代表通过,红色代表失败。
我当前的 openpyxl 代码是:
wb = Workbook()
ws = wb.active
ws.freeze_panes = ws.cell('A3')
ws.title = 'Best Practice Report for XXX'
ws['A1'] = 'Best Practice Report for XXX %s' % curdate
ws['A2'] = 'IP Address/FQDN'
ws['B2'] = 'BP Number'
ws['C2'] = 'Title'
ws['D2'] = 'Priority'
ws['E2'] = 'Status'
ws['F2'] = 'Description'
a1 = ws['A1']
a1.font = Font(size=20)
redFill = PatternFill(start_color='FFEE1111', end_color='FFEE1111', fill_type='solid')
ws.conditional_formatting.add('E4:E1000', FormatRule(text=['Fail'], stopIfTrue=True, fill=redFill))
wb.save('bp--TESTresults.xlsx')
Run Code Online (Sandbox Code Playgroud)
我的问题是条件格式规则,我找不到任何基于单元格文本的条件格式的好例子。
更新
感谢查理克拉克斯的回应,我得到了这个工作。创建如下两条规则。
ws.conditional_formatting.add('E4:E1000', FormulaRule(formula=['NOT(ISERROR(SEARCH("Pass",E4)))'], stopIfTrue=True, fill=greenFill))
ws.conditional_formatting.add('E4:E1000', FormulaRule(formula=['NOT(ISERROR(SEARCH("Fail",E4)))'], stopIfTrue=True, fill=redFill))
Run Code Online (Sandbox Code Playgroud)
我只是掀起了一个文件,并做了一些内省。它在 A2:A5 中有值,我认为这应该对您有所帮助:
from openpyxl import load_workbook
wb = load_workbook("Issues/cf.xlsx")
ws = wb.active
ws.conditional_formatting.cf_rules
{'A2:A5': [<openpyxl.formatting.rule.Rule object at 0x108e6dfd0>]}
rule = _['A2:A5']
rule = rule[0]
rule.type
'containsText'
rule.formula
['NOT(ISERROR(SEARCH("fail",A2)))']
rule.stopIfTrue
None
rule.operator
'containsText'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6358 次 |
| 最近记录: |