Skip to content

Commit 7b4f0c0

Browse files
committed
Use document transform for safer removal of unmerged table cells
1 parent aa583d3 commit 7b4f0c0

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

mammoth/docx/body_xml.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def calculate_row_spans(rows):
399399
for row in rows
400400
)
401401
if unexpected_non_rows:
402-
remove_unmerged_table_cells(rows)
402+
rows = remove_unmerged_table_cells(rows)
403403
return _elements_result_with_messages(rows, [results.warning(
404404
"unexpected non-row element in table, cell merging may be incorrect"
405405
)])
@@ -410,7 +410,7 @@ def calculate_row_spans(rows):
410410
for cell in row.children
411411
)
412412
if unexpected_non_cells:
413-
remove_unmerged_table_cells(rows)
413+
rows = remove_unmerged_table_cells(rows)
414414
return _elements_result_with_messages(rows, [results.warning(
415415
"unexpected non-cell element in table row, cell merging may be incorrect"
416416
)])
@@ -441,14 +441,17 @@ def calculate_row_spans(rows):
441441

442442

443443
def remove_unmerged_table_cells(rows):
444-
for row in rows:
445-
for cell_index, cell in enumerate(row.children):
446-
if isinstance(cell, documents.TableCellUnmerged):
447-
row.children[cell_index] = documents.table_cell(
448-
children=cell.children,
449-
colspan=cell.colspan,
450-
rowspan=cell.rowspan,
451-
)
444+
return list(map(
445+
transforms.element_of_type(
446+
documents.TableCellUnmerged,
447+
lambda cell: documents.table_cell(
448+
children=cell.children,
449+
colspan=cell.colspan,
450+
rowspan=cell.rowspan,
451+
),
452+
),
453+
rows,
454+
))
452455

453456

454457
def read_child_elements(element):

0 commit comments

Comments
 (0)