diff options
| -rw-r--r-- | mammoth/documents.py | 9 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 15 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 23 |
3 files changed, 44 insertions, 3 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index d837556..7f17f3e 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -48,7 +48,7 @@ class TableRow(HasChildren): @cobble.data class TableCell(HasChildren): - pass + colspan = cobble.field() @cobble.data class LineBreak(Element): @@ -111,7 +111,12 @@ bookmark = Bookmark table = Table table_row = TableRow -table_cell = TableCell +def table_cell(children, colspan=None): + if colspan is None: + colspan = 1 + return TableCell(children=children, colspan=colspan) + + line_break = LineBreak def numbering_level(level_index, is_ordered): diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index f3166f5..a44169a 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -164,8 +164,21 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f def table_cell(element): + gridspan = element \ + .find_child_or_null("w:tcPr") \ + .find_child_or_null("w:gridSpan") \ + .attributes.get("w:val") + + if gridspan is None: + colspan = 1 + else: + colspan = int(gridspan) + return _read_xml_elements(element.children) \ - .map(documents.table_cell) + .map(lambda children: documents.table_cell( + children=children, + colspan=colspan + )) def read_child_elements(element): diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 7ed325c..8fb35cf 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -221,6 +221,29 @@ class ReadXmlElementTests(object): ]) ]) assert_equal(expected_result, table) + + + @istest + def gridspan_is_read_as_colspan_for_table_cell(self): + element = xml_element("w:tbl", {}, [ + xml_element("w:tr", {}, [ + xml_element("w:tc", {}, [ + xml_element("w:tcPr", {}, [ + xml_element("w:gridSpan", {"w:val": "2"}) + ]), + xml_element("w:p", {}, []) + ]), + ]), + ]) + table = _read_and_get_document_xml_element(element) + expected_result = documents.table([ + documents.table_row([ + documents.table_cell([ + documents.paragraph([]) + ], colspan=2) + ]) + ]) + assert_equal(expected_result, table) @istest |
