79504595

Date: 2025-03-12 18:46:26
Score: 1
Natty:
Report link

In every example, I see that the rule is added from python code. But in my case, its not easy to construct or maintain such rules.

I have several worksheets each with a fixed layout in terms of columns or rows. I select cells within the columns (to avoid summarized values being considered in hierarchical data) and have created conditional formatting within excel.

Ex: RULE: AND(INT($F5) > 95000, $M5 < 0.9) applies to $F$5:$F$10,$F$12:$F$14,$F$16:$F$18,$F$20:$F$25,$F$27:$F$33

There are at least 10 rules per worksheet ... and these can change bi-weekly. So I do not want to keep changing the Python code.

My intention is to clear formatting in the worksheet after filling the cell with the color applied via conditional formatting to help me in review stage (some fills may be added/ removed manually as rules are more of a benchmark and not an enforcing criteria)

But I see two problems:

cell.fill does not return the fill applied via conditional formatting. Rather it seems to contain the fill value applied statically to the cell and saved in the worksheet.

I m using Python 3.13.2 (latest version on date) with OpenPyPxl 3.1.5

However,

 worksheet.conditional_formatting.clear()
 worksheet.conditional_formatting.get_rules(coord)
 worksheet.conditional_formatting.remove(coord, rule)

all of the above statements fail with 
AttributeError: 'ConditionalFormattingList' object has no attribute 'clear' / 'get_rules'/ 'remove'
        for i in range(1, 33) :
            for j in range(1, 8):
                cell = worksheet.cell(row = i, column = j)
                k = 10+j
                trg_cell = worksheet.cell(row = i, column = k)
                if cell.fill:
                    trg_cell.fill = copy(worksheet.cell(row = i, column = j).fill)
                    
        

        '''
        worksheet.conditional_formatting.clear()
        
        for i in range(1, 33) :
            for j in range(1, 8):
                cell = worksheet.cell(row = i, column = j)
                coord = cell.coordinate
                for rule in worksheet.conditional_formatting.get_rules(coord):
                    worksheet.conditional_formatting.remove(coord, rule)
        '''
       
    
        '''
        # Iterate through the cells to extract the fill colors
        for row in worksheet.iter_rows():
            for cell in row:
                # if its white or greay used for row background
                if cell.fill and cell.fill.start_color.index != '00000000' and cell.fill.start_color.index != 'FFF8F8F8': 
                    # Save the fill color
                    fill = cell.fill
                    #mycolor = openpyxl.styles.colors.Color('FF00FF00')
                    #print(str(fill.))
                    #print(cell.coordinate + ' BG: ' + str(fill.bgColor) + ' FG: ' + str(fill.fgColor) + ' START: ' + str(fill.start_color) + ' END: ' + str(fill.end_color))
                    #print(cell.coordinate + ' '+ str(cell.fill.start_color.index) + ' ' + str(cell.fill.fgColor.index)  + ' ' + str(cell.fill.bgColor.index))
                    #cell.fill = PatternFill(bgColor=mycolor, fill_type="solid")
                    # Remove conditional formatting
                    #cell.fill = PatternFill(start_color=fill.start_color, end_color=fill.start_color, fill_type="solid") #fill_type=fill.fill_type
                    cell_colors[cell.coordinate] = cell.fill

        '''
Reasons:
  • Blacklisted phrase (1): help me
  • Long answer (-1):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Madan