1

I can find the list of conditional formatting available in the worksheet. However, how do I determine if a cell has any conditional formatting at all ?

from openpyxl import load_workbook
ws= load_workbook('Path to excel file')
WS=ws.active
cf_rules =[]
for i in WS.conditional_formatting:
       cf_rules.append(i)

for row in WS.iter_rows(): 
    for cell in row: 
         if ( "Check for formatting"): 
             print("Formatting Detected")
Doomski
  • 117
  • 1
  • 11

1 Answers1

2

You can iterate over workbook.conditional_formatting and then check if your cell is included in one of the ranges of the conditional formatting.

To make it simpler I just check one single cell(A1), if it conatains conditional formatting.

import openpyxl

wb = openpyxl.load_workbook('Path to file')
ws = wb['my_sheet']
my_cell = wb['A1']

for conditional_formatting in worksheet.conditional_formatting._cf_rules:
    for cell_range in conditional_formatting.cells.ranges:
        if my_cell.coordinate in cell_range:
            print('cell contains a conditional formatting')

This is the only solution I found. Sadly its pretty slow performance wise.