我想用openpyxl 2.5.0编辑xlsx文件。
这是我的代码,它适用于空文件:
from openpyxl import load_workbook
wb = load_workbook(filename="Untitled.xlsx")
ws = wb.active
ws["B5"] = "test"
wb.save("Export.xlsx")
但是,如果我尝试加载this file,我会收到以下模糊错误。
Traceback (most recent call last):
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\base.py", line 57, in _convert
value = expected_type(value)
ValueError: invalid literal for int() with base 10: 'l'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Matteo\PyCharm\CMakeR\XLSXEditor.py", line 3, in <module>
wb = load_workbook(filename="Modulo CMR.xlsx")
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\reader\excel.py", line 274, in load_workbook
for c in find_charts(archive, rel.target):
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\chart\reader.py", line 43, in find_charts
drawing = SpreadsheetDrawing.from_tree(tree)
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
[Previous line repeated 1 more times]
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 100, in from_tree
return cls(**attrib)
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\drawing\shapes.py", line 349, in __init__
self.l = l
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\base.py", line 69, in __set__
value = _convert(self.expected_type, value)
File "C:\Matteo\PyCharm\CMakeR\venv\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'int'>
我真的不知道可能导致它的原因。但是通过搜索互联网,我发现它可能与样式/格式转换问题有关。
感谢Charlie Clark,我注意到 - 如果我将openpyxl降级到2.4.9 - 我可以解决这个问题。所以它似乎是某种与2.5.0相关的错误。
答案 0 :(得分:2)
今天看起来只有两种解决方案:
参考 https://bitbucket.org/openpyxl/openpyxl/issues/967/expected-type-error-since-250