Venkatesh Gorige
Venkatesh Gorige

Reputation: 11

Python3 Openpyxl patternfill not detecting multiple colors

I am trying to add 2 different colors to my cell rows using openpyxl engine for my xlsm file having macros based on the positive or negative values. But I end up having the most used color for all the cells.

Iam using openpyxl==3.0.6

Here is my code. from openpyxl import Workbook

from openpyxl import load_workbook

from openpyxl.styles import Protection,PatternFill

                    wb = load_workbook(path,keep_vba=True)

                    ws = wb['Sheet1']

                    CheckColor = [-1,2,5,-5,5,-9,10,11,-1,-8,1,5,3]

                    yellow  = PatternFill(patternType='solid',fgColor='FFF2CC')
                    pink  = PatternFill(patternType='solid',fgColor='FF9999')


                    try:
                        for row in range(30,43):
                            for color in CheckColor:
                                if color > 0:
                                    print("printing yellow color")
                                    ws.cell(row = row, column = 2).value = 10
                                    ws.cell(row = row, column = 2).fill = yellow

                                    ws.cell(row = row, column = 3).value = 30
                                    ws.cell(row = row, column = 3).fill = yellow

                                    ws.cell(row = row, column = 4).value = 40
                                    ws.cell(row = row, column = 4).fill = yellow

                                else:
                                    print("printing pink")
                                    ws.cell(row = row, column = 2).value = 10
                                    ws.cell(row = row, column = 2).fill = pink

                                    ws.cell(row = row, column = 3).value = 30
                                    ws.cell(row = row, column = 3).fill = pink

                                    ws.cell(row = row, column = 4).value = 40
                                    ws.cell(row = row, column = 4).fill = pink
                            



                        wb.save(path)
                    except Exception as e:
                        print(e)

Upvotes: 0

Views: 114

Answers (1)

moken
moken

Reputation: 6554

As @Warcupine states, you select the first row (30) then create a new loop

for color in CheckColor:

which loops through the values in the CheckColor list for all of row 30. In other words you set all 13 colour options to row 30 ending with the last colour 3. Then move on to the next row (31) and do the same thing again. Thus all rows end up with the last colour in the list, 3 which being greater than 0 fills yellow.
You want to loop the rows and select the next value in the list for each. There is a couple of ways to do this, using enum allows you a count from 0 - 12 that can be used to set the colour index.

Code extract

...
CheckColor = [-1, 2, 5, -5, 5, -9, 10, 11, -1, -8, 1, 5, 3]

yellow = PatternFill(patternType='solid', fgColor='FFF2CC')
pink = PatternFill(patternType='solid', fgColor='FF9999')

try:
    for enum, row in enumerate(range(30, 43)):
        color = CheckColor[enum]  # For each loop of the rows enum increments and sets color to the next CheckColor index
        if color > 0:
            print("printing yellow color")
            ws.cell(row=row, column=2).value = 10
            ws.cell(row=row, column=2).fill = yellow

            ws.cell(row=row, column=3).value = 30
            ws.cell(row=row, column=3).fill = yellow

...

Upvotes: 0

Related Questions