diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-11-03 16:30:11 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-11-03 16:30:11 +0000 |
| commit | 1498397f4ede71d81ec79f380d43ec041438a047 (patch) | |
| tree | 8b9751ed744ca75cf6aa140fe227c94431129e1e | |
| parent | cd95535a1bb0f486d4a102cfc622ae5228c3071d (diff) | |
Read table style applied to tables
| -rw-r--r-- | mammoth/documents.py | 6 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 19 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 36 |
3 files changed, 56 insertions, 5 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index d89de3f..e4372ab 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -46,7 +46,8 @@ class Hyperlink(HasChildren): @cobble.data class Table(HasChildren): - pass + style_id = cobble.field() + style_name = cobble.field() @cobble.data class TableRow(HasChildren): @@ -141,7 +142,8 @@ class Bookmark(Element): bookmark = Bookmark -table = Table +def table(children, style_id=None, style_name=None): + return Table(children=children, style_id=style_id, style_name=style_name) def table_row(children, is_header=None): return TableRow(children=children, is_header=bool(is_header)) diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 6577522..01920f0 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -213,9 +213,22 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f def table(element): - return _read_xml_elements(element.children) \ - .flat_map(calculate_row_spans) \ - .map(documents.table) + properties = element.find_child_or_null("w:tblPr") + return _ReadResult.map_results( + read_table_style(properties), + _read_xml_elements(element.children) + .flat_map(calculate_row_spans), + + lambda style, children: documents.table( + children=children, + style_id=style[0], + style_name=style[1], + ), + ) + + + def read_table_style(properties): + return _read_style(properties, "w:tblStyle", "Table", styles.find_table_style_by_id) def table_row(element): diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index ad509c4..d1dd071 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -493,6 +493,39 @@ class TableTests(object): ]) ]) assert_equal(expected_result, table) + + @istest + def table_has_no_style_if_it_has_no_properties(self): + element = xml_element("w:tbl") + assert_equal(None, _read_and_get_document_xml_element(element).style_id) + + @istest + def table_has_style_id_and_name_read_from_paragraph_properties_if_present(self): + style_xml = xml_element("w:tblStyle", {"w:val": "TableNormal"}) + properties_xml = xml_element("w:tblPr", {}, [style_xml]) + table_xml = xml_element("w:tbl", {}, [properties_xml]) + + styles = Styles( + paragraph_styles={}, + character_styles={}, + table_styles={"TableNormal": Style(style_id="TableNormal", name="Normal Table")}, + ) + + paragraph = _read_and_get_document_xml_element(table_xml, styles=styles) + assert_equal("TableNormal", paragraph.style_id) + assert_equal("Normal Table", paragraph.style_name) + + @istest + def warning_is_emitted_when_table_style_cannot_be_found(self): + style_xml = xml_element("w:tblStyle", {"w:val": "TableNormal"}) + properties_xml = xml_element("w:tblPr", {}, [style_xml]) + table_xml = xml_element("w:tbl", {}, [properties_xml]) + + result = _read_document_xml_element(table_xml, styles=Styles.EMPTY) + table = result.value + assert_equal("TableNormal", table.style_id) + assert_equal(None, table.style_name) + assert_equal([results.warning("Table style with ID TableNormal was referenced but not defined in the document")], result.messages) @istest def tbl_header_marks_table_row_as_header(self): @@ -1102,6 +1135,9 @@ class FakeStyles(object): def find_character_style_by_id(self, style_id): return None + + def find_table_style_by_id(self, style_id): + return None def _document_element_with_text(text): return xml_element("w:document", {}, [ |
