iOSecure
iOSecure

Reputation: 232

Apply color to specific cells with openpyxl

I'm trying to apply the color blue to a row of cells, but when I do it doesn't return an error or make any changes.

wb = load_workbook(filename='concentrated.xlsx')
ws2 = wb.get_sheet_by_name('Data in')

HeaderFill = PatternFill(start_color='002b43', end_color='002b43', fill_type='solid')

for cell in ws2['A3':'O3']:
        HeaderFill

wb.save('concentrated.xlsx')

Upvotes: 0

Views: 1540

Answers (1)

Triggernometry
Triggernometry

Reputation: 583

Well, you're not actually assigning HeaderFill to anything in your loop. The correct thing to assign it to is the fill property of the cell.

So:

for row in ws2['A3':'O3']:
    for cell in row:
        cell.fill = HeaderFill

EDIT: Thanks to Charlie Clark for catching my oversight: slices return row tuples, not cell tuples.

Upvotes: 2

Related Questions