openpyxl字体条件格式

我正在使用pyopenxl输出一些Excel电子表格,我遇到了字体条件格式的问题。 我想用红色突出显示小于0的单元格,以下是我所做的:

from pyopenxl import formatting, styles red_font = styles.Font(size=self.font_size, bold=bold, color=self.red_color_font) red_fill = styles.PatternFill(start_color=self.red_color, end_color=self.red_color, fill_type='solid') self.ws.conditional_formatting.add( cell.coordinate, formatting.CellIsRule(operator='lessThan', formula=['0'], fill=red_fill, font=red_font) ) 

所以我只是创build了字体和填充样式,并将其应用于我的单元格。 坏事是它不起作用。 只要我从CellIsRule()删除字体格式,一切都会恢复正常,并且我的单元格被填充为红色。 但事情是,我需要改变颜色,没有人知道我的代码有什么问题吗? 或者也许与openpyxl?

下面的代码适用于我使用openpyxl版本2.2.6

 from openpyxl import formatting, styles wb = Workbook() ws = wb.active red_color = 'ffc7ce' red_color_font = '9c0103' red_font = styles.Font(size=14, bold=True, color=red_color_font) red_fill = styles.PatternFill(start_color=red_color, end_color=red_color, fill_type='solid') for row in range(1,10): ws.cell(row=row, column=1, value=row-5) ws.cell(row=row, column=2, value=row-5) ws.conditional_formatting.add('A1:A10', formatting.CellIsRule(operator='lessThan', formula=['0'], fill=red_fill, font=red_font)) ws.conditional_formatting.add('B1:B10', formatting.CellIsRule(operator='lessThan', formula=['0'], fill=red_fill)) wb.save("test.xlsx") 

这显示如下:

在这里输入图像描述