Openpyxl copy ColorScaleRule conditional formatting based on a reference cell/range?

350 views
Skip to first unread message

J.J. Morton

unread,
Aug 11, 2021, 9:29:48 AM8/11/21
to openpyxl-users

Hi,

I was wondering if anyone could help me figure out what I need to do in order to “copy” the format/style of a cell that has conditional formatting applied using a ColorScaleRule across to other cells in the same row using openpyxl?

For example, I have this spreadsheet generated with the attached openpyxl script.

spreadsheet1.png

But, I’d like the colored style formatting in Column A to be copied across the same column through Column E. 

So, the formatting I’m trying to achieve would look like this:

spreadsheet2.png

Directly in Excel I was able to copy the formatting using a macro with this code:

Sub Set_Colors_from_Conditions_in_Column_1()

'

' Set_Colors_from_Conditions_in_Column_1 Macro

'

rowStart = 1

rowEnd = Cells(Rows.Count, 1).End(xlUp).Row

columnStart = 2

columnEnd = 5

For i = rowStart To rowEnd

    For j = columnStart To columnEnd

        Cells(i, j).Interior.Color = Cells(i, 1).DisplayFormat.Interior.Color

    Next

Next

End Sub


Thanks for any help or suggestions!

openpyxl_test.py
Reply all
Reply to author
Forward
0 new messages