本文介绍了OpenPyXL:CellIsRule函数意外失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开一个Excel文件并应用一些条件格式:

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'

这是怎么回事?我使用的是openpyxl版本2.4.8,而此答案仅适用于版本2.2.6.但是,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?

推荐答案

尝试以下类型的方法:

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

这篇关于OpenPyXL:CellIsRule函数意外失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 01:06