Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue Editing Complex Word Tables with Merged Cells in python-docx #1434

Open
MichaelRBot opened this issue Sep 26, 2024 · 0 comments
Open

Comments

@MichaelRBot
Copy link

When attempting to modify tables in a Word document using python-docx, an IndexError occurs if the table contains merged cells. Specifically, the error appears when trying to access certain rows or cells in tables that have vertical or horizontal cell merging, which leads to an IndexError: list index out of range.

Steps to Reproduce:

Load a Word document that contains a table with merged cells (either vertically or horizontally).
Attempt to access and edit cells within the table using the python-docx library, specifically targeting merged cells.
The library raises an IndexError: list index out of range when trying to access these merged cells.
Expected Behavior:
The library should handle merged cells appropriately, allowing access to their content and permitting modifications without raising an error.

Observed Behavior:
The table structure breaks when attempting to access merged cells, leading to an IndexError. This occurs because python-docx struggles to handle complex table layouts with merged cells or rows.

Traceback:

python
Copy code
IndexError: list index out of range
File ".../docx/table.py", line 401, in _Row.cells
return tuple(self.table.row_cells(self._index))
File ".../docx/table.py", line 106, in Table.row_cells
return self._cells[start:end]
File ".../docx/table.py", line 173, in Table._cells
cells.append(cells[-col_count])
IndexError: list index out of range
Additional Notes:

The issue was specifically encountered while trying to modify cells containing the placeholder text "Add answer here" in a table with merged rows. Other standard tables in the document could be edited without issues.
The merged cells seem to affect the way python-docx indexes table rows, causing the list index error.
Environment:

python-docx version: [Please specify the version used]
Python version: [Please specify your Python version]
Operating System: [e.g., Windows 10, macOS, etc.]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant