diff options
| -rw-r--r-- | mammoth/documents.py | 7 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 8 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 20 | ||||
| -rw-r--r-- | tests/docx/document_matchers.py | 2 |
4 files changed, 34 insertions, 3 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index a6e125c..dbd78dc 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -48,7 +48,7 @@ class Table(HasChildren): @cobble.data class TableRow(HasChildren): - pass + is_header = cobble.field() @cobble.data class TableCell(HasChildren): @@ -132,7 +132,10 @@ bookmark = Bookmark table = Table -table_row = TableRow + +def table_row(children, is_header=None): + return TableRow(children=children, is_header=bool(is_header)) + def table_cell(children, colspan=None, rowspan=None): if colspan is None: colspan = 1 diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index cc4e98f..49ff431 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -62,6 +62,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f "w:rPr", "w:tblPr", "w:tblGrid", + "w:trPr", "w:tcPr", ]) @@ -208,8 +209,13 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f def table_row(element): + properties = element.find_child_or_null("w:trPr") + is_header = bool(properties.find_child("w:tblHeader")) return _read_xml_elements(element.children) \ - .map(documents.table_row) + .map(lambda children: documents.table_row( + children=children, + is_header=is_header, + )) def table_cell(element): diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index cccf472..e768bd5 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -17,6 +17,8 @@ from .document_matchers import ( is_run, is_hyperlink, is_text, + is_table, + is_row, ) @@ -466,6 +468,24 @@ class TableTests(object): ]) assert_equal(expected_result, table) + @istest + def tbl_header_marks_table_row_as_header(self): + element = xml_element("w:tbl", {}, [ + xml_element("w:tr", {}, [ + xml_element("w:trPr", {}, [ + xml_element("w:tblHeader") + ]), + ]), + xml_element("w:tr"), + ]) + table = _read_and_get_document_xml_element(element) + assert_that(table, is_table( + children=is_sequence( + is_row(is_header=True), + is_row(is_header=False), + ), + )) + @istest def gridspan_is_read_as_colspan_for_table_cell(self): diff --git a/tests/docx/document_matchers.py b/tests/docx/document_matchers.py index dd2276d..d2e5d5c 100644 --- a/tests/docx/document_matchers.py +++ b/tests/docx/document_matchers.py @@ -16,6 +16,8 @@ def create_element_matcher(element_type): is_paragraph = create_element_matcher(documents.Paragraph) is_run = create_element_matcher(documents.Run) is_hyperlink = create_element_matcher(documents.Hyperlink) +is_table = create_element_matcher(documents.Table) +is_row = create_element_matcher(documents.TableRow) is_empty_run = is_run(children=[]) |
