我想将红色设置为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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句