From 1498397f4ede71d81ec79f380d43ec041438a047 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 Nov 2017 16:30:11 +0000 Subject: Read table style applied to tables --- mammoth/documents.py | 6 ++++-- mammoth/docx/body_xml.py | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'mammoth') 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): -- cgit v1.2.3