Home > Software design >  Python - Openpyexcel - Conditional formatting, How on earth do i apply the rule to a column, but hig
Python - Openpyexcel - Conditional formatting, How on earth do i apply the rule to a column, but hig

Time:01-13

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

load the workbook
wb = openpyxl.load_workbook('output.xlsx')
get the active sheet
ws = wb.active
#make the value of L3 "P"
ws["L3"].value = "P"
>#make the value of L4 "F"
ws["L4"].value = "F"
#make the value of L5 " "
ws["L5"].value = " "
#make the value of L6 "-"
ws["L6"].value = "-"

blueFill = PatternFill(start_color='ADD8E6', end_color='ADD8E6', fill_type='solid')
rule1 = CellIsRule(operator='equal', formula=['$L$3'], stopIfTrue=True, fill=blueFill)

greenFill = PatternFill(start_color='90EE90', end_color='90EE90', fill_type='solid')
rule2 = CellIsRule(operator='equal', formula=['$L$4'], stopIfTrue=True, fill=greenFill)

redFill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')
rule3 = CellIsRule(operator='equal', formula=['$L$5'], stopIfTrue=True, fill=redFill)

yellowFill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')
rule4 = CellIsRule(operator='equal', formula=['$L$6'], stopIfTrue=True, fill=yellowFill)

#add the rules to column K
ws.conditional_formatting.add('K2:K1000', rule1)
ws.conditional_formatting.add('K2:K1000', rule2)
ws.conditional_formatting.add('K2:K1000', rule3)
ws.conditional_formatting.add('K2:K1000', rule4)

 #save the changes
wb.save('output2.xlsx')


So ive made it this far and everything works the way i want it to, except, i cant seem to find a way to Highlight the row the cell is found on and not just the cell itself.

I just cant think of a way to write it. Google has led me down some dead ends, so im hoping someone out there has accomplished this and can let me in on the trick!`

CodePudding user response:

If you have no luck with changing your code to extend the range, the following code sample will apply the CF as formulas across the col range A - K. If you type any of your values in column K from K2 down (to K1000) the row from A to K is highlighted in your selected colour. See example image at the bottom.

import openpyxl
from openpyxl.styles import PatternFill
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting.rule import Rule


wb = openpyxl.load_workbook('output.xlsx')
# get the active sheet
# ws = wb.active
ws = wb['Sheet1']
# make the value of L3 "P"
ws["L3"].value = "P"
# make the value of L4 "F"
ws["L4"].value = "F"
# make the value of L5 " "
ws["L5"].value = " "
# make the value of L6 "-"
ws["L6"].value = "-"

### Colour fill formats
blueFill = PatternFill(start_color='ADD8E6', end_color='ADD8E6', fill_type='solid')
greenFill = PatternFill(start_color='90EE90', end_color='90EE90', fill_type='solid')
redFill = PatternFill(start_color='FF0000', end_color='FF0000', fill_type='solid')
yellowFill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')

### Fill range with sheet row location
fill_dict = {3:blueFill, 4:greenFill, 5:redFill, 6:yellowFill}
### Conditional format range so a fill row will be across columns A to K
cf_range = '$A$2:$K$1000'
### Add Conditional Formats to the sheet
for row, fill in fill_dict.items():
    rule = Rule(type='expression', dxf=DifferentialStyle(fill=fill), stopIfTrue=True)
    rule.formula = [f'$L${row}=$K2']
    ws.conditional_formatting.add(cf_range, rule)

# save the changes
wb.save('output2.xlsx')

Sample sheet

  • Related