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

1

6

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.

Condemnation answered 21/2, 2018 at 12:28 Comment(2)
I suspect it's one of the many problems relating to DrawingMLCesta
Same issue happen with me so I degraded version of openpyxl to 2.4.11 it is working. But still I am not able to get my graphs. [pip install openpyxl==2.4.11]Whipcord
Q
5

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

Quadriga answered 28/2, 2018 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.