无法使用openpyxl加载工作簿:在处理上述异常期间,发生了另一个异常 [英] Can't load workbook with openpyxl: during handling of the above exception, another exception occurred

查看:2308
本文介绍了无法使用openpyxl加载工作簿:在处理上述异常期间,发生了另一个异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用openpyxl 2.5.0编辑一个xlsx文件.

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.

感谢Charlie Clark,我注意到-如果我将openpyxl降级到2.4.9-我可以解决此问题.因此,这似乎是与2.5.0相关的错误.

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. 从Excel工作簿中删除图形.
  2. 降级到openpyxl版本< 2.5.0.

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

这篇关于无法使用openpyxl加载工作簿:在处理上述异常期间,发生了另一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆