python - Conditional Formatting with openpyxl based on text -


i have spreadsheet generating openpyxl contains number of system checks. based on rules; words pass, fail or informational inserted column e in spreadsheet. use openpyxl conditionally format fill of spreadsheet based on value of pass or fail. green pass , red fail.

my current code openpyxl is:

wb = workbook() ws = wb.active ws.freeze_panes = ws.cell('a3')  ws.title = 'best practice report xxx' ws['a1'] = 'best practice report 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')  

my issue conditional formatting rule, cannot find examples conditional formatting based on text in cell.

update
charlie clarks response got working. created 2 rules follows.

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)) 

i whipped file , did introspection. has values in a2:a5 think should along:

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 @ 0x108e6dfd0>]} rule = _['a2:a5'] rule = rule[0] rule.type 'containstext' rule.formula ['not(iserror(search("fail",a2)))'] rule.stopiftrue none rule.operator 'containstext' 

Comments