Fill cells with colors using openpyxl? Fill cells with colors using openpyxl? python python

Fill cells with colors using openpyxl?


I believe the issue is that you're trying to assign a fill object to a style.

ws['A1'].fill = redFill should work fine.


The API for styles changed once again. What worked for me was

my_red = openpyxl.styles.colors.Color(rgb='00FF0000')my_fill = openpyxl.styles.fills.PatternFill(patternType='solid', fgColor=my_red)cell.fill = my_fill

Color is an alpha RGB hex color. You can pass it in as 'rrggbb' with a default alpha of 00 or specify the alpha with 'aarrggbb'. A bunch of colors are defined as constants in openpyxl.styles.colors if you need to grab one quickly.


This worked for me. They changed things and most of the help you see on the internet is for older versions of the openpyxl library from what I am seeing.

# Change background color xls_cell.style = Style(fill=PatternFill(patternType='solid',                                        fill_type='solid',                                         fgColor=Color('C4C4C4')))