Skip to content

Index error when reading table #732

@B0und

Description

@B0und

I need to read data from the table in docx, but the loop raises IndexError. I tried to use iter_unique_cells method that i found, but it didnt solve the problem.Any help would be greatly appreciated !
another_broken_table.docx

def iter_unique_cells(row):
    """Generate cells in *row* skipping empty grid cells."""
    prior_tc = None
    for cell in row.cells:
        this_tc = cell._tc
        if this_tc is prior_tc:
            continue
        prior_tc = this_tc
        yield cell
document = Document('another_broken_table.docx')
tables = document.tables
for table in tables:
      for row in table.rows:
            for cell in iter_unique_cells(row):
                pass
Traceback (most recent call last):
  File "d:/test.py", line 94, in <module>
    xml_string = loop_tables(tables)
  File "d:/test.py", line 52, in loop_tables
    for cell in iter_unique_cells(row):
  File "d:/test.py", line 12, in iter_unique_cells
    for cell in row.cells:
  File "D:\Python\Python36-32\lib\site-packages\docx\table.py", line 401, in cells
    return tuple(self.table.row_cells(self._index))
  File "D:\Python\Python36-32\lib\site-packages\docx\table.py", line 106, in row_cells
    return self._cells[start:end]
  File "D:\Python\Python36-32\lib\site-packages\docx\table.py", line 173, in _cells
    cells.append(cells[-col_count])
IndexError: list index out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions