aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-09-09 09:50:57 +0100
committerMichael Williamson <mike@zwobble.org>2025-09-09 09:50:57 +0100
commit7b4f0c08362e2328d3c6df26465362a50c56478f (patch)
tree6496565f477d9d1b24c41b55c6bb067a882fcb02
parentaa583d3eda56cb157b2eaff7d607d3d41b24c3f5 (diff)
Use document transform for safer removal of unmerged table cells
-rw-r--r--mammoth/docx/body_xml.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py
index e499cce..d3dd267 100644
--- a/mammoth/docx/body_xml.py
+++ b/mammoth/docx/body_xml.py
@@ -399,7 +399,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
for row in rows
)
if unexpected_non_rows:
- remove_unmerged_table_cells(rows)
+ rows = remove_unmerged_table_cells(rows)
return _elements_result_with_messages(rows, [results.warning(
"unexpected non-row element in table, cell merging may be incorrect"
)])
@@ -410,7 +410,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
for cell in row.children
)
if unexpected_non_cells:
- remove_unmerged_table_cells(rows)
+ rows = remove_unmerged_table_cells(rows)
return _elements_result_with_messages(rows, [results.warning(
"unexpected non-cell element in table row, cell merging may be incorrect"
)])
@@ -441,14 +441,17 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
def remove_unmerged_table_cells(rows):
- for row in rows:
- for cell_index, cell in enumerate(row.children):
- if isinstance(cell, documents.TableCellUnmerged):
- row.children[cell_index] = documents.table_cell(
- children=cell.children,
- colspan=cell.colspan,
- rowspan=cell.rowspan,
- )
+ return list(map(
+ transforms.element_of_type(
+ documents.TableCellUnmerged,
+ lambda cell: documents.table_cell(
+ children=cell.children,
+ colspan=cell.colspan,
+ rowspan=cell.rowspan,
+ ),
+ ),
+ rows,
+ ))
def read_child_elements(element):