How to set cell background color in OpenPyXL
In order to set a cell’s background color in OpenPyXL, use PatternFill
with fill_type="solid"
and start_color="your_desired_color"
and no end_color
. The following example will set a cell’s background to green:
sheet["A1"].fill = PatternFill("solid", start_color="5cb800")
Full example based on our OpenPyXL minimal XLSX write example
from openpyxl import Workbook
wb = Workbook()
sheet = wb["Sheet"] # This sheet is created by default
# Add content to sheet
sheet["A1"] = "This is cell A1"
sheet["A1"].fill = PatternFill("solid", start_color="5cb800")
# Save
wb.save("openpyxl-test.xlsx")