简体   繁体   English

使用openpyxl将颜色应用于特定单元格

[英]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')

Well, you're not actually assigning HeaderFill to anything in your loop. 好吧,您实际上并未将HeaderFill分配给循环中的任何内容。 The correct thing to assign it to is the fill property of the cell. 分配给它的正确方法是单元格的fill属性。

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. 编辑:感谢查理·克拉克(Charlie Clark)的监督:切片返回行元组,而不是单元元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM