问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/50044582/openpyxl-unexpected-fail-with-cellisrule-function