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

问题描述 投票:0回答:4

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

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
python openpyxl
4个回答
4
投票

我也遇到了同样的问题。这似乎不是文件格式的问题,因为该文件在 Excel 中打开得很好。这看起来像是

openpyxl
的一个错误。 (我已经在这里打开了问题请求

您可以使用以下猴子补丁来解决这个问题。只需将其放入启动代码中的某个位置即可。

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()

0
投票

tldr - 如果您使用

XlsxWriter
,请确保您没有将
None
写为单元格格式。


我在使用 XlsxWriter 生成一个简单的 xlsx 文件时遇到了这个问题。 xlsx 文件总是可以正常打开,但无法加载

openpyxl

原来我有一个像这样的辅助函数

def writerow(worksheet, row, cols, fmt=None):
  for i, val in enumerate(cols):
    worksheet.write(r, i, val, fmt)

如果您注意到,当我没有明确传递格式时,我实际上是在调用

worksheet.write(r, i, val, None)

解决方法是确保在写入一行时始终具有格式。

default_format = workbook.add_format()
worksheet.write(row, col, value, default_format)

0
投票

我遇到了同样的问题,这是我的解决方案: 转到 _reader.py (.\Python\Python311\Lib\site-packages\openpyxl\worksheet)

在班级

bind_row_dimensions()
中找到
WorksheetReader
并简单地应用
try/except
,如下所示:

def bind_row_dimensions(self):
    for row, rd in self.parser.row_dimensions.items():
        if 's' in rd:
            key = int(rd['s'])
            try: rd['s'] = self.ws.parent._cell_styles[key]
            except: rd['s'] = None
        self.ws.row_dimensions[int(row)] = RowDimension(self.ws, **rd)

它对我有用,希望你也如此。


0
投票

我不确定你的excel文件出了什么问题,我也没有遇到同样的问题,但试试这个,我相信它会很好用:

转到_reader.py (.\Python\Python311\Lib\site-packages\openpyxl\worksheet)

在类

bind_cells()
中找到
WorksheetReader
并简单地应用try/ except,如下所示:

 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: style = StyleArray('i', [0, 0, 0, 0, 0, 0, 0, 0, 0])
            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

    if self.ws._cells:
        self.ws._current_row = self.ws.max_row # use cells not row dimensions
© www.soinside.com 2019 - 2024. All rights reserved.