diff options
| -rw-r--r-- | mammoth/docx/body_xml.py | 12 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 43 |
2 files changed, 54 insertions, 1 deletions
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index f99a799..091caef 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -399,6 +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) return _elements_result_with_messages(rows, [results.warning( "unexpected non-row element in table, cell merging may be incorrect" )]) @@ -438,6 +439,17 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return _success(rows) + 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, + ) + + def read_child_elements(element): return _read_xml_elements(element.children) diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index fc677be..0c86c6e 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -1118,8 +1118,49 @@ class TableTests(object): def test_warning_if_non_row_in_table(self): - element = xml_element("w:tbl", {}, [xml_element("w:p")]) + # Include normal rows to ensure they're still read correctly. + element = xml_element("w:tbl", {}, [ + xml_element("w:tr", {}, [ + xml_element("w:tc", {}, [ + xml_element("w:p", {}, [ + _run_element_with_text("Row 1"), + ]), + ]), + ]), + xml_element("w:p"), + xml_element("w:tr", {}, [ + xml_element("w:tc", {}, [ + xml_element("w:p", {}, [ + _run_element_with_text("Row 2"), + ]), + ]), + ]), + ]) + result = _read_document_xml_element(element) + + expected_value = documents.table([ + documents.table_row([ + documents.table_cell([ + documents.paragraph([ + documents.run([ + documents.text("Row 1"), + ]), + ]), + ]), + ]), + documents.paragraph([]), + documents.table_row([ + documents.table_cell([ + documents.paragraph([ + documents.run([ + documents.text("Row 2"), + ]), + ]), + ]), + ]), + ]) + assert_equal(expected_value, result.value) expected_warning = results.warning("unexpected non-row element in table, cell merging may be incorrect") assert_equal([expected_warning], result.messages) |
