gpt4 book ai didi

python - 加载工作簿时列出与样式相关的 Openpyxl 中超出范围的索引

转载 作者:行者123 更新时间:2023-12-05 02:49:16 29 4
gpt4 key购买 nike

我正在尝试加载工作簿,但无法读取该特定文件。

input_workbook = openpyxl.load_workbook(input_file)

我收到的错误是:

Traceback (most recent call last):
File "/home/admin/Development/pythonProject/venv/RFQ_Analysis_R01.py", line 121, in <module>
input_workbook = openpyxl.load_workbook(input_file)
File "/home/admin/Development/pythonProject/venv/lib/python3.7/site-packages/openpyxl/reader/excel.py", line 315, in load_workbook
reader.read()
File "/home/admin/Development/pythonProject/venv/lib/python3.7/site-packages/openpyxl/reader/excel.py", line 280, in read
self.read_worksheets()
File "/home/admin/Development/pythonProject/venv/lib/python3.7/site-packages/openpyxl/reader/excel.py", line 228, in read_worksheets
ws_parser.bind_all()
File "/home/admin/Development/pythonProject/venv/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py", line 434, in bind_all
self.bind_cells()
File "/home/admin/Development/pythonProject/venv/lib/python3.7/site-packages/openpyxl/worksheet/_reader.py", line 339, in bind_cells
style = self.ws.parent._cell_styles[cell['style_id']]
IndexError: list index out of range

最佳答案

我遇到了同样的问题。它似乎不是文件格式的问题,因为文件在 Excel 中打开得很好。这看起来像是 openpyxl 的错误。 (我有 opened a issue request here )

您可以使用以下猴子补丁解决它。只需将它放入您的启动代码中的某处即可。

def monkey_patch_openpyxl():
'''Openpyxl has a bug with workbooks that have wrong cell styling information.
Monkey patch the library so it can handle these types of workbooks.'''
from openpyxl.worksheet import _reader
from openpyxl.cell import Cell
def bind_cells(self):
for idx, row in self.parser.parse():
for cell in row:
try:
style = self.ws.parent._cell_styles[cell['style_id']]
except: ## This is the patch, original doesn't have a try/except here
style = None
c = Cell(self.ws, row=cell['row'], column=cell['column'], style_array=style)
c._value = cell['value']
c.data_type = cell['data_type']
self.ws._cells[(cell['row'], cell['column'])] = c
self.ws.formula_attributes = self.parser.array_formulae
if self.ws._cells:
self.ws._current_row = self.ws.max_row # use cells not row dimensions

_reader.WorksheetReader.bind_cells = bind_cells

monkey_patch_openpyxl()

关于python - 加载工作簿时列出与样式相关的 Openpyxl 中超出范围的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64063802/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com