question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Index error when reading table

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
scannycommented, Oct 19, 2019

What does print(row._tr.xml) output for the offending row?

You might also try:

from docx.table import _Cell
row_cells = [_Cell(tc, table) for tc in row._tr.tc_lst]
0reactions
dshefman1commented, Mar 1, 2022

You might also try:

from docx.table import _Cell
row_cells = [_Cell(tc, table) for tc in row._tr.tc_lst]

There is a problem with this solution. It solves one problem while causing another. When you use this fix, merged cells are no longer unmerged during the parsing process. Without unmerging the cells, you will never be able to reproduce your table with the output data because your columns will no longer line up when you try to reproduce the table. Is there a way to fix this IndexError without causing this problem?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix IndexError in Python - Rollbar
The IndexError in Python occurs when an item from a list is attempted to be accessed that is outside the index range of...
Read more >
How to solve "IndexError: list index out of range" error when ...
I hope it makes sense.. I am currently working on a populating a postgres database where I have this loop which iterates along...
Read more >
How to debug list index out of range error in python? - Flexiple
So let us first understand how indexes work and then look at the solutions for the list index out of range error. Lists...
Read more >
Error (The changes you requested to the table were not ...
The table may not have any relationships or any indexes. Cause. This problem occurs when the Autonumber field has been incorrectly seeded.
Read more >
mysqlcheck unexpectedly causes table to be marked as corrupt
2020-11-24 10:01:44 44 [ERROR] InnoDB: Record in index ... [ERROR] Got error 128 when reading table './schema/tablex' [ERROR] Got error 180 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found