Adding a background color to Cell OpenPyXL

With openpyxl 2.5.3, the code above does not work.

after trying, following code worked:

from openpyxl.styles import PatternFill
sheet['A1'].fill = PatternFill(start_color="FFC7CE", end_color="FFC7CE", fill_type = "solid")

From the documentation:

from openpyxl.styles import PatternFill
        
sheet['A1'].fill = PatternFill(bgColor="FFC7CE", fill_type = "solid")
    

As @Charlie Clark (co-author of openpyxl) suggests, Conditional formatting might be a better way to go. More information in the official doc

If you want to change the background color, from more recent versions, keyword bgcolor seems not to work (in my case, it the color of the cell ends up black).

Instead, you can use start_color or fgColor. For example, both solutions work:

from openpyxl.styles import PatternFill
from openpyxl.styles.colors import YELLOW

sheet['A1'].fill = PatternFill(start_color="FFC7CE", fill_type = "solid")
sheet['A1'].fill = PatternFill(fgColor=YELLOW, fill_type = "solid")