6

我想用 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")

但是,如果我尝试加载此文件,则会收到以下模糊错误。

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 相关的错误。

4

1 回答 1

5

今天看起来只有两个解决方案:

  1. 从 Excel 工作簿中删除图形。
  2. 降级到 openpyxl 版本 < 2.5.0。

参考 https://bitbucket.org/openpyxl/openpyxl/issues/967/expected-type-error-since-250

于 2018-02-28T13:00:16.270 回答