[英]PatternFill issue using Openpyxl on Python. Can anyone help me?
我正在尝试使用来自 Openpyxl 的 IF 和 Patternfill 根据其文本更改单元格的背景颜色,但我收到类型错误。 请看下面的代码:
#Level - Replied Travel Alerts(RTA)
incident_index = item.body.index("incident") + 12
category_indexpos = item.body[incident_index:].index("Category") + incident_index
level = item.body[incident_index:category_indexpos]
ws.cell(row=index+2, column = 2).value = level
if "Advisory" in level:
advisory_pattern = Pattern(patternType = "solid", fgColor="FFFF00")
ws.cell(row=index+2, column = 2).fill = advisory_pattern
elif "Notice" in level:
notice_pattern = Pattern(patternType = "solid", fgColor = "00B050")
ws.cell(row=index+2, column = 2).fill = notice_pattern
elif "Special" in level:
special_pattern = Pattern(patternType = "solid", fgColor= "FF0000")
ws.cell(row=index+2, column = 2).fill = special_pattern
但我在“advisory_pattern = Pattern(patternType = "solid", fgColor="FFFF00")" 行中收到错误。 查看消息:
发生异常:TypeError 无法创建“re.Pattern”实例
谁能帮我?
谢谢
嗯,re.Pattern来自python re库,与在Openpyxl中创建背景填充无关
首先,我会在您的代码顶部使用此导入:
from openpyxl.styles import fills
然后你可以修改你的线条以适应这种格式(我用你的第一个填充作为我的例子):
ws.cell(row=index+2, column = 2).fill = fills.PatternFill("solid", fgColor="FFFF00")
如果您仍想将填充存储在变量中,只需确保您使用的是来自 openpyxl 填充库的填充对象,而不是来自 python re 库的 Pattern 对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.