excel本质是一堆xml的zip文件, 这个异常是由于遇到了 `<fill/>`的节点,然后openpyxl不处理
class Fill(Serialisable):
"""Base class"""
tagname = "fill"
@classmethod
def from_tree(cls, el):
children = [c for c in el]
if not children:
return # 这里返回None
child = children[0]
if "patternFill" in child.tag:
return PatternFill._from_tree(child)
return super(Fill, GradientFill).from_tree(child)
#--------------------------
def _convert(expected_type, value):
"""
Check value is of or can be converted to expected type.
"""
if not isinstance(value, expected_type):
try:
value = expected_type(value) # Fill 不支持__init__报错
except:
raise TypeError('expected ' + str(expected_type))
return value
我的解决方式
reader = ExcelReader(f'{table}.xlsx')
def new_read(read):
def inner(file):
r = read(file)
if file == 'xl/styles.xml':
r = r.replace(b'<fill/>', b'')
return r
return inner
reader.archive.read = new_read(reader.archive.read)
reader.read()
reader.wb.save(f'{table}.xlsx')
本文讲述了在使用openpyxl处理Excel文件时遇到的因<fill/>节点引发的异常,通过修改styles.xml文件并替换掉该节点,解决了不支持的转换问题,确保了正常读写Excel文件。

1528

被折叠的 条评论
为什么被折叠?



