我想将红色设置为Excel工作表中的一系列单元格(一个列为另一列),并使用openpyxl
。
我能够从官方文档中找到如何给单元格提供某种颜色,但是我无法弄清楚如何给出范围。
基本上,我想要使用openpyxl这样的东西:
inheritance_cell_format = wb.add_format()
inheritance_cell_format.set_font_color('red')
ws.conditional_format( 0, skip_cols-2, ws.max_row, skip_cols-1, { 'type': 'cell', 'criteria': '=', 'value': '⇒', 'format': inheritance_cell_format } )
以上代码段在xlsxwriter中有效。
到目前为止,很明显,第一行出现错误,指出工作簿没有属性'add_format()
'
您可以使用.iter_rows()或iter_cols()方法使用循环为范围着色。您可以导入PatternFill为其着色。
import openpyxl as px
from openpyxl.styles import PatternFill
wb = px.load_workbook(file) #imports file
ws = wb.active #Active sheet (first one)
for row in sheet.iter_rows(min_row=1, max_col=3, max_row=2): #max row and col (range)
for cell in row:
cell.fill = PatternFill(start_color="FF0000", fill_type = "solid") #html colors
wb.save(file)