From 8d1cc0932083a686e24ff02c9819e507fbb2cb9f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 1 Dec 2013 11:33:58 +0000 Subject: Read style from paragraph properties --- mammoth/docx.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'mammoth') diff --git a/mammoth/docx.py b/mammoth/docx.py index 2f383dd..bf4de0b 100644 --- a/mammoth/docx.py +++ b/mammoth/docx.py @@ -47,17 +47,24 @@ def run(element): @handler("w:p") def paragraph(element): - return documents.paragraph(_read_xml_elements(element.children)) + properties = _find_child(element, "w:pPr") + if properties is None: + style_name = None + else: + style_name = _find_child(properties, "w:pStyle").attributes.get("w:val") + + children = _read_xml_elements(element.children) + return documents.paragraph(children, style_name) @handler("w:body") -def paragraph(element): +def body(element): return _read_xml_elements(element.children) @handler("w:document") -def paragraph(element): - body_element = _find(lambda child: child.name == "w:body", element.children) +def document(element): + body_element = _find_child(element, "w:body") return documents.Document(_read_xml_elements(body_element.children)) @@ -67,6 +74,10 @@ def _find(predicate, iterable): return item +def _find_child(element, name): + return _find(lambda child: child.name == name, element.children) + + def _read_xml_elements(elements): return filter(None, map(read_xml_element, elements)) -- cgit v1.2.3