Openpyxl: unexpected fail with CellIsRule function

試著忘記壹切 提交于 2019-12-24 20:32:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!