python - Conditional_formatting 使用 openpyxl 查找重复值
问题描述
我正在尝试让一个类使用 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)
任何帮助将非常感激。
解决方案
试试这个代码,它对我有用
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)
推荐阅读
- python - Playsound 不播放音乐
- unity3d - Unity2D:使运动学玩家与瓷砖地图碰撞器发生碰撞
- node.js - 在节点中格式化日期(来自 mongodb)
- node.js - 错误:无法发布 /api/success
- javascript - NextJS 和 Formik:选择下拉菜单上的条件/动态呈现表单字段
- mongodb - 对空数组使用 $push
- ios - 当对象范围为容器时,多次调用 Swinject 工厂关闭
- laravel - 多个电子邮件控制器无法正常工作我该如何解决?
- javascript - 如何从 javascript 对象中获取项目
- linux - 通过 ebpf begin 探测使用 systemtap 探测用户空间进程不起作用