Python 基于文本的openpyxl条件格式
我有一个用openpyxl生成的电子表格,其中包含许多系统检查。根据规则;“通过”、“失败”或“信息性”这三个词被插入到我的电子表格的E列中。我想使用Openpyxl根据Pass或Fail的值有条件地格式化电子表格的填充。绿色表示及格,红色表示不及格 我当前的openpyxl代码是:Python 基于文本的openpyxl条件格式,python,python-2.7,openpyxl,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' w
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')
我的问题是关于条件格式规则,我找不到任何基于单元格中文本的条件格式的好例子
更新多亏了查理·克拉克的回应,我才得以工作。创建了如下两条规则
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))
我刚刚整理了一份文件,做了一些反思。它在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'
从openpyxl导入加载\u工作簿
wb=加载工作簿(“问题/cf.xlsx”)
ws=wb.active
ws.conditional_formatting.cf_规则
{'A2:A5':[]}
规则=['A2:A5']
规则=规则[0]
规则类型
“containsText”
规则.公式
['NOT(ISERROR(SEARCH(“fail”,A2)))']
规则
没有一个
规则.运算符
“containsText”
我很想对现有文件使用内省。从OpenPYXL2.2开始,规则在Excel看到时公开。我同意文档可能会更好,但我个人对条件格式几乎没有经验。感谢您提供的信息,它帮助我找到了实现containsBlanks和notContainsBlanks功能的方法