From dbcb7129f21201a41ac0c41657e31b0c21b5f724 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Apr 2017 18:31:03 +0100 Subject: Read alignment for paragraphs --- mammoth/documents.py | 5 +++-- mammoth/docx/body_xml.py | 2 ++ tests/docx/body_xml_tests.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mammoth/documents.py b/mammoth/documents.py index fe1cbcb..b1e2c1e 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -20,6 +20,7 @@ class Paragraph(HasChildren): style_id = cobble.field() style_name = cobble.field() numbering = cobble.field() + alignment = cobble.field() @cobble.data class Run(HasChildren): @@ -83,8 +84,8 @@ def document(children, notes=None, comments=None): comments = [] return Document(children, notes, comments=comments) -def paragraph(children, style_id=None, style_name=None, numbering=None): - return Paragraph(children, style_id, style_name, numbering) +def paragraph(children, style_id=None, style_name=None, numbering=None, alignment=None): + return Paragraph(children, style_id, style_name, numbering, alignment=alignment) def run( children, diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index c2d7d44..e8ddb01 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -111,6 +111,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f def paragraph(element): properties = element.find_child_or_null("w:pPr") + alignment = properties.find_child_or_null("w:jc").attributes.get("w:val") numbering = _read_numbering_properties(properties.find_child_or_null("w:numPr")) return _ReadResult.map_results( @@ -121,6 +122,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f style_id=style[0], style_name=style[1], numbering=numbering, + alignment=alignment, )).append_extra() def _read_paragraph_style(properties): diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 98296a1..5964acd 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -76,6 +76,20 @@ class ParagraphTests(object): assert_equal(None, paragraph.style_name) assert_equal([results.warning("Paragraph style with ID Heading1 was referenced but not defined in the document")], result.messages) + @istest + def paragraph_has_no_justification_if_it_has_no_justificiation_properties(self): + paragraph_xml = xml_element("w:p") + paragraph = _read_and_get_document_xml_element(paragraph_xml) + assert_equal(None, paragraph.alignment) + + @istest + def paragraph_has_justification_read_from_paragraph_properties_if_present(self): + justification_xml = xml_element("w:jc", {"w:val": "center"}) + properties_xml = xml_element("w:pPr", {}, [justification_xml]) + paragraph_xml = xml_element("w:p", {}, [properties_xml]) + paragraph = _read_and_get_document_xml_element(paragraph_xml) + assert_equal("center", paragraph.alignment) + @istest def paragraph_has_no_numbering_if_it_has_no_numbering_properties(self): element = xml_element("w:p") -- cgit v1.2.3