From dc381b5afcae0994094d49465142e6099dcec86c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 1 Dec 2013 11:26:10 +0000 Subject: Add style_name to paragraphs --- mammoth/documents.py | 6 +++++- mammoth/docx.py | 2 +- mammoth/xmlparser.py | 4 +++- tests/docx_tests.py | 11 ++++++++--- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/mammoth/documents.py b/mammoth/documents.py index 50b5e65..58e4f6b 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -1,7 +1,11 @@ import collections +def paragraph(children, style_name=None): + return Paragraph(children, style_name) + + Document = collections.namedtuple("Document", ["children"]) -Paragraph = collections.namedtuple("Paragraph", ["children"]) +Paragraph = collections.namedtuple("Paragraph", ["children", "style_name"]) Run = collections.namedtuple("Run", ["children"]) Text = collections.namedtuple("Text", ["value"]) diff --git a/mammoth/docx.py b/mammoth/docx.py index 9a3fe36..2f383dd 100644 --- a/mammoth/docx.py +++ b/mammoth/docx.py @@ -47,7 +47,7 @@ def run(element): @handler("w:p") def paragraph(element): - return documents.Paragraph(_read_xml_elements(element.children)) + return documents.paragraph(_read_xml_elements(element.children)) @handler("w:body") diff --git a/mammoth/xmlparser.py b/mammoth/xmlparser.py index c645ad4..8b26777 100644 --- a/mammoth/xmlparser.py +++ b/mammoth/xmlparser.py @@ -6,7 +6,9 @@ XmlElement = collections.namedtuple("XmlElement", ["name", "attributes", "childr XmlText = collections.namedtuple("XmlText", ["value"]) -element = XmlElement +def element(name, attributes=None, children=None): + return XmlElement(name, attributes or {}, children or []) + text = XmlText diff --git a/tests/docx_tests.py b/tests/docx_tests.py index d5f4990..80253fa 100644 --- a/tests/docx_tests.py +++ b/tests/docx_tests.py @@ -11,7 +11,7 @@ class ReadTests(object): with open(test_path("single-paragraph.docx")) as fileobj: result = docx.read(fileobj=fileobj) expected_document = documents.Document([ - documents.Paragraph([ + documents.paragraph([ documents.Run([ documents.Text("Walking on imported air") ]) @@ -39,7 +39,7 @@ class ReadXmlElementTests(object): def can_read_text_within_paragraph(self): element = _paragraph_element_with_text("Hello!") assert_equal( - documents.Paragraph([documents.Run([documents.Text("Hello!")])]), + documents.paragraph([documents.Run([documents.Text("Hello!")])]), docx.read_xml_element(element) ) @@ -47,9 +47,14 @@ class ReadXmlElementTests(object): def can_read_text_within_document(self): element = _document_element_with_text("Hello!") assert_equal( - documents.Document([documents.Paragraph([documents.Run([documents.Text("Hello!")])])]), + documents.Document([documents.paragraph([documents.Run([documents.Text("Hello!")])])]), docx.read_xml_element(element) ) + + @istest + def paragraph_has_no_style_if_it_has_no_properties(self): + element = xml_element("w:p") + assert_equal(None, docx.read_xml_element(element).style_name) @istest def unrecognised_elements_are_ignored(self): -- cgit v1.2.3