简体   繁体   中英

Conditional Formatting Openpyxl

Can someone help me with how to replicate this conditional formatting formula in Python using Openpyxl?

=AND(RIGHT(B$7;9)="(Message)";B8<>"";A8=C8;C8="") If true,color the cell red.

There are a couple of and ways to do this and lots of examples, below is one option;
cf_range will be the cell range to apply this conditional formatting to

from openpyxl import load_workbook
from openpyxl.styles import fills
from openpyxl.formatting.rule import FormulaRule

work_book = load_workbook('foo.xlsx')
sheet = work_book.active

cf_range = '$A1'

sheet.conditional_formatting.add(cf_range,
                                 FormulaRule(
                                     formula=["AND(RIGHT(B$7,9)=\"(Message)\",B8<>\"\",A8=C8,C8=\"\")"],
                                     fill=fills.PatternFill("solid", bgColor="FF0000"),
                                     stopIfTrue=False
                                     )
                                 )

work_book.save('foo.xlsx')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM