我试图让一个类使用openpyxl突出显示列A中的任何重复值。
目前,A栏有以下值:
A
A
B
C
A
C
A
最终的结果是所有的A和C细胞都用红色着色。下面的代码不会抛出任何错误,但是在运行后打开的文件在复制的单元格中没有任何颜色。
import openpyxl
from openpyxl import formatting, styles
from openpyxl.formatting import Rule
class Duplicates():
def __init__(self, wb2):
self.wb2 = wb2
ws2=self.wb2.active
self.red_fill = styles.PatternFill(start_color ='ffc7ce', end_color = 'ffc7ce', fill_type = 'solid')
dxf= styles.differential.DifferentialStyle(fill=self.red_fill)
rule = Rule(type='duplicateValues',dxf=dxf,stopIfTrue = None)
ws2.conditional_formatting.add('$A:$A',rule)
self.wb2.save('testing.xlsx')
Duplicates(wb2)
任何帮助都将不胜感激。
发布于 2022-02-16 21:55:16
试试这段代码,它对我有用。
red_text = Font(color="9C0006")
red_fill = PatternFill(bgColor="FFC7CE")
dxf = DifferentialStyle(font=red_text, fill=red_fill)
rule = Rule(type="duplicateValues", text="highlight", dxf=dxf)
wsRes.conditional_formatting.add('B1:B10000', rule)
https://stackoverflow.com/questions/67219270
复制相似问题