python - Openpyxl: unexpected fail with CellIsRule function
问题描述
I am opening an Excel file and applying some conditional formatting:
from openpyxl import *
from openpyxl.formatting.rule import CellIsRule
os.chdir(r'C:\Users\myfolder')
wb= load_workbook('myfile.xlsx')
ws = wb.active
#Conditional formatting
ws.conditional_formatting.add('S3:S89', formatting.CellIsRule(operator='equal', formula=['1'], font='#FF0000')) #Red
This was done based on this answer. However, I get this error:
AttributeError Traceback (most recent call last)
<ipython-input-190-fb94e6065444> in <module>()
---> 10 ws.conditional_formatting.add('S3:S89', formatting.CellIsRule(operator='equal', formula=['1'], font='#FF0000')) #Red
AttributeError: module 'openpyxl.formatting' has no attribute 'CellIsRule'
What's going on? I am using openpyxl
version 2.4.8, whilst this answer was offered for version 2.2.6. However, the CellIsRule
function seems to be still present. What am I missing?
解决方案
Try the following type of approach:
import openpyxl
os.chdir(r'C:\Users\myfolder')
wb = openpyxl.load_workbook('myfile.xlsx')
ws = wb.active
#Conditional formatting
red_font = openpyxl.styles.Font(size=14, bold=True, color='9c0103')
ws.conditional_formatting.add('S3:S89', openpyxl.formatting.rule.CellIsRule(operator='equal', formula=['1'], font=red_font)) #Red
推荐阅读
- sql - 如何每行获得一个结果?我已经尝试过 DISTINCT 但它不起作用
- eclipse - 成功执行单元测试后增加 SonarQube 中的代码覆盖率
- python - 如何使用枚举键访问和打印类的成员?
- qt - 如何将 QtQuick.Controls 2.xx 与 QtGraphicalEffects 一起使用
- java - 执行程序后 CMD 中显示的 JNI 错误是什么?
- c# - 保持传输线高(编辑实际上需要 TX 线低和 RX 线高)
- firebase - 安排 Firebase 云函数在每小时开始时每 30 分钟运行一次,然后在 30 分钟后运行
- excel - 如何将权重分配给低于目标的高分
- javascript - 从 setInterval 调用的函数内部更新全局变量
- python - 组合多个值和排序