python excel条件格式_python怎么查看excel某个单元格的条件格式?

碰到这种情况,你可以有两种方法。

最直接的,是去看它的文档:https://openpyxl.readthedocs.io/en/stable/formatting.html​openpyxl.readthedocs.io

如果实在看不懂,也可以使用dir()来看看 conditional_formating里面有些啥。发现里面有个叫_cf_rules的属性,也许就是你要找的东西。

>>> from openpyxl import load_workbook

>>> wb = load_workbook(filename = 's.xlsx')

>>> sheet1 = wb['Sheet1']

>>> dir(sheet1.conditional_formatting)

['_ConditionalFormattingList__nonzero', '__bool__', '__class__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cf_rules', 'add', 'max_priority']

>>> sheet1.conditional_formatting._cf_rules

OrderedDict([(, [

Parameters:

type='cellIs', rank=None, priority=1, equalAverage=None, operator='greaterThan', aboveAverage=None, dxfId=0, stdDev=None, stopIfTrue=None, timePeriod=None, text=None, percent=None, bottom=None, colorScale=None, dataBar=None, iconSet=None, formula=['3.5']])])

可以发现,这个_cf_rules是OrderedDict类型,那么你想修改删除,只需要看看这个OrderedDict有些什么函数了。

同样,用上面的查文档或者dir()方法,可以看到OrderedDict.pop()或者OrderedDict.update()可能正是你想找的删除或者修改的函数。

有三个条件格式,分别是对C1:C1048576也就是C整列、D2:D3两个单元格、D4单独一个单元格。

>>> sheet1.conditional_formatting._cf_rules

OrderedDict([(,

[

Parameters:

type='cellIs', rank=None, priority=3, equalAverage=None, operator='greaterThan', aboveAverage=None, dxfId=4, stdDev=None, stopIfTrue=None, timePeriod=None, text=None, percent=None, bottom=None, colorScale=None, dataBar=None, iconSet=None, formula=['3.5']]),

(,

[

Parameters:

type='cellIs', rank=None, priority=2, equalAverage=None, operator='lessThan', aboveAverage=None, dxfId=3, stdDev=None, stopIfTrue=None, timePeriod=None, text=None, percent=None, bottom=None, colorScale=None, dataBar=None, iconSet=None, formula=['5.5']]),

(,

[

Parameters:

type='cellIs', rank=None, priority=1, equalAverage=None, operator='greaterThan', aboveAverage=None, dxfId=0, stdDev=None, stopIfTrue=None, timePeriod=None, text=None, percent=None, bottom=None, colorScale=None, dataBar=None, iconSet=None, formula=['3']])])

如果你要找某个单独的单元格,那你需要自己比对,比如要找C3,它显然包含在整个C列的条件格式里的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值