简体   繁体   English

Openpyxl: 'ValueError: Max value is 14' 使用 load_workbook 时

[英]Openpyxl: 'ValueError: Max value is 14' when using load_workbook

I have attempted to open an excel file in which I need to insert dataframes to certain sheets, while leaving other sheets alone.我试图打开一个 excel 文件,在该文件中我需要将数据框插入某些工作表,而其他工作表则不理会。 The script works fine when I tested it on other excel files.当我在其他 excel 文件上测试它时,该脚本运行良好。 When I use it on the one I actually need, I get an error message.当我在我真正需要的地方使用它时,我收到一条错误消息。

Here is the script:这是脚本:

from openpyxl import load_workbook
book = load_workbook(self.directory)

Self.directory refers to my file location. Self.directory 是指我的文件位置。 As you can see in the traceback, it fails already at this line when trying to execute load_workbook(), and gives the following error message:正如您在回溯中看到的,当尝试执行 load_workbook() 时,它已经在这一行失败,并给出以下错误消息:

ValueError: Max value is 14值错误:最大值为 14

Here is the relevant traceback (I left the directory locations starting with the virtual environment folder 'virtual'):这是相关的回溯(我离开了从虚拟环境文件夹“virtual”开始的目录位置):

"""
book = load_workbook(self.directory)
virtual\lib\site-packages\openpyxl\reader\excel.py", line 217, in load_workbook
shared_strings = read_string_table(archive.read(strings_path))
virtual\lib\site-packages\openpyxl\reader\strings.py", line 22, in read_string_table
text = Text.from_tree(node).content
virtual\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
virtual\lib\site-packages\openpyxl\descriptors\serialisable.py", line 84, in from_tree
obj = desc.expected_type.from_tree(el)
virtual\lib\site-packages\openpyxl\styles\fonts.py", line 110, in from_tree
return super(Font, cls).from_tree(node)
virtual\lib\site-packages\openpyxl\descriptors\serialisable.py", line 100, in from_tree
return cls(**attrib)
virtual\lib\site-packages\openpyxl\cell\text.py", line 114, in __init__
self.family = family
virtual\lib\site-packages\openpyxl\descriptors\nested.py", line 36, in __set__ 6, in __set__
super(Nested, self).__set__(instance, value)
virtual\lib\site-packages\openpyxl\descriptors\base.py", line 110, in __set__ , in __set__ 
super(Min, self).__set__(instance, value)
virtual\lib\site-packages\openpyxl\descriptors\base.py", line 89, in __set__ in __set__
raise ValueError('Max value is {0}'.format(self.max))
ValueError: Max value is 14
"""

I realized that the excelfile I was using is over the limit of self.max.我意识到我使用的 excelfile 超出了 self.max 的限制。

I tried sifting through the the openpyxl scripts myself, but I could not manage to figure out what self.max refers to, or how I can change my Excel File so that I can load the workbook.我尝试自己筛选 openpyxl 脚本,但我无法弄清楚 self.max 指的是什么,或者如何更改我的 Excel 文件以便我可以加载工作簿。

Can anyone point me to the right direction?谁能指出我正确的方向?

Thanks in advance!提前致谢!

I had to remove all formatting in a sheet I was working with.我不得不删除我正在使用的工作表中的所有格式。

In Libreoffice;在 Libreoffice 中; select all, "clear direct formatting"全选,“清除直接格式”

Here's what fixed this error for me.这是为我修复此错误的方法。 I edited lib\\site-packages\\openpyxl\\descriptors\\base.py and added a print statement after line 86 in class Max like so:我编辑了lib\\site-packages\\openpyxl\\descriptors\\base.py并在类 Max 的第 86 行之后添加了一个打印语句,如下所示:

def __set__(self, instance, value):
    if ((self.allow_none and value is not None)
        or not self.allow_none):
        value = _convert(self.expected_type, value)
        if value > self.max:
            print(f"value is {value}")
            raise ValueError('Max value is {0}'.format(self.max))
    super(Max, self).__set__(instance, value)

This printed the value of 34 which is obviously higher than the max value of 14 (it's a font family value).这打印了 34 的值,这显然高于最大值 14(这是一个字体系列值)。

I then saved a copy of my Excel spreadsheet with a .zip extension, extracted all of the XML files, and then used grep searching for val="34".然后,我使用 .zip 扩展名保存了我的 Excel 电子表格的副本,提取了所有 XML 文件,然后使用 grep 搜索 val="34"。 This led me to 3 cells which somehow had font-family=34 in them.这让我找到了 3 个以某种方式具有 font-family=34 的单元格。 I changed the font to something else in Excel, saved the spreadsheet, then changed it back to the original font (Arial) and saved.我在 Excel 中将字体更改为其他字体,保存电子表格,然后将其更改回原始字体 (Arial) 并保存。
After all of this, the error was gone.在这一切之后,错误消失了。

删除工作表上第 15 个以上的小“评论框”后,我能够解决该错误。

the number of comment boxed didn't solve my problem.评论的数量并没有解决我的问题。 I had to remove some worksheets until I got below 14 worksheets in total to be able to open/read the document.我不得不删除一些工作表,直到我总共得到 14 个工作表以下才能打开/阅读文档。

It is an excel file generated WPS but not MS office.它是由 WPS 生成的 excel 文件,但不是 MS Office。

  1. you can use xlwings to open it.你可以使用xlwings来打开它。
  2. you can save to CSV file manually and read.您可以手动保存到 CSV 文件并阅读。

Issue is resolved if you suppress/commentout the exception like shown below in openpyxl:如果您在 openpyxl 中抑制/注释掉如下所示的异常,问题将得到解决:

def __set__(self, instance, value):
        if ((self.allow_none and value is not None)
            or not self.allow_none):
            value = _convert(self.expected_type, value)
            if value > self.max:
                self.max=self.max
                #raise ValueError('Max value is {0}'.format(self.max))
        super(Max, self).__set__(instance, value)

It resolved the issue and now I am able to use它解决了问题,现在我可以使用

pd.read_excel(io.BytesIO(obj['Body'].read()), engine='openpyxl', sheet_name=[0], header=None)

只需注释掉 openpyxl 中引发错误的代码行。

Instead of patching the __set__ method you can patch the specific descriptor's max value.您可以修补特定描述符的最大值,而不是修补__set__方法。

# IMPORTANT, you must do this before importing openpyxl
from unittest import mock
# Set max font family value to 100
p = mock.patch('openpyxl.styles.fonts.Font.family.max', new=100)
p.start()
import openpyxl
openpyxl.open('my-bugged-worksheet.xlsx') # this works now!

If you patch descriptors\\base.py you'll be allowing potentially bad values for all descriptors.如果您修补descriptors\\base.py您将允许所有描述符使用潜在的错误值。 This approach is more surgical in that it only patches the font family descriptor that is causing the error.这种方法更具手术性,因为它只修补导致错误的字体系列描述符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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