简体   繁体   中英

Can't load workbook with openpyxl: during handling of the above exception, another exception occurred

I want to edit a xlsx file with openpyxl 2.5.0.

This is my code and it works with an empty file:

from openpyxl import load_workbook

wb = load_workbook(filename="Untitled.xlsx")
ws = wb.active
ws["B5"] = "test"
wb.save("Export.xlsx")

However if I try to load this file , I get the following obscure error.

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'>

I have really no clue what might be causing it. But from searching the internet, I gathered that it might be related to a style/formatting conversion issue.

Thanks to Charlie Clark I noticed that - if I downgrade openpyxl to 2.4.9 - I can work around this issue. So it seems to be some sort of bug related to 2.5.0.

Today it looks like there are only two solutions:

  1. Remove graphics from Excel workbook.
  2. Downgrade to openpyxl version < 2.5.0.

Refer to https://bitbucket.org/openpyxl/openpyxl/issues/967/expected-type-error-since-250

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM