aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-09-27 17:21:48 +0100
committerMichael Williamson <mike@zwobble.org>2014-09-27 17:21:48 +0100
commit9fb300452f2e1734e531abde614b8513d0edd093 (patch)
tree380df590deb0efee43ae902107d47716d761033b
parentc8bfc778472b644d56a5694553bec8621159c4ce (diff)
Read vertical alignment from document.xml
-rw-r--r--mammoth/documents.py19
-rw-r--r--mammoth/docx/document_xml.py7
-rw-r--r--tests/docx/document_xml_tests.py10
3 files changed, 32 insertions, 4 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py
index 49b2396..e7a46c4 100644
--- a/mammoth/documents.py
+++ b/mammoth/documents.py
@@ -3,7 +3,10 @@ import dodge
Document = dodge.data_class("Document", ["children", "footnotes"])
Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "style_name", "numbering"])
-Run = dodge.data_class("Run", ["children", "style_id", "style_name", "is_bold", "is_italic", "is_underline"])
+Run = dodge.data_class("Run", [
+ "children", "style_id", "style_name", "is_bold", "is_italic",
+ "is_underline", "vertical_alignment",
+])
Text = dodge.data_class("Text", ["value"])
Hyperlink = dodge.data_class("Hyperlink", ["href", "children"])
Table = dodge.data_class("Table", ["children"])
@@ -30,8 +33,18 @@ def document(children, footnotes=None):
def paragraph(children, style_id=None, style_name=None, numbering=None):
return Paragraph(children, style_id, style_name, numbering)
-def run(children, style_id=None, style_name=None, is_bold=None, is_italic=None, is_underline=None):
- return Run(children, style_id, style_name, bool(is_bold), bool(is_italic), bool(is_underline))
+def run(children, style_id=None, style_name=None, is_bold=None, is_italic=None, is_underline=None, vertical_alignment=None):
+ if vertical_alignment is None:
+ vertical_alignment = VerticalAlignment.baseline
+ return Run(
+ children, style_id, style_name, bool(is_bold), bool(is_italic),
+ bool(is_underline), vertical_alignment,
+ )
+
+class VerticalAlignment(object):
+ baseline = "baseline"
+ superscript = "superscript"
+ subscript = "subscript"
text = Text
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index e909a86..cdc870a 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -70,7 +70,11 @@ def _create_reader(numbering, content_types, relationships, styles, footnote_ele
style_name = None
else:
style_name = styles.find_character_style_by_id(style_id).name
-
+
+ vertical_alignment = properties \
+ .find_child_or_null("w:vertAlign") \
+ .attributes.get("w:val")
+
is_bold = properties.find_child("w:b")
is_italic = properties.find_child("w:i")
is_underline = properties.find_child("w:u")
@@ -83,6 +87,7 @@ def _create_reader(numbering, content_types, relationships, styles, footnote_ele
is_bold=is_bold,
is_italic=is_italic,
is_underline=is_underline,
+ vertical_alignment=vertical_alignment,
))
diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py
index f95419b..4c1f330 100644
--- a/tests/docx/document_xml_tests.py
+++ b/tests/docx/document_xml_tests.py
@@ -143,6 +143,16 @@ class ReadXmlElementTests(object):
run = self._read_run_with_properties([xml_element("w:u")])
assert_equal(True, run.is_underline)
+ @istest
+ def run_has_baseline_vertical_alignment_if_vertical_alignment_element_is_not_present(self):
+ run = self._read_run_with_properties([])
+ assert_equal(documents.VerticalAlignment.baseline, run.vertical_alignment)
+
+ @istest
+ def run_has_vertical_alignment_read_from_vertical_alignment_element(self):
+ run = self._read_run_with_properties([xml_element("w:vertAlign", {"w:val": "superscript"})])
+ assert_equal(documents.VerticalAlignment.superscript, run.vertical_alignment)
+
def _read_run_with_properties(self, properties, styles=None):
properties_xml = xml_element("w:rPr", {}, properties)
run_xml = xml_element("w:r", {}, [properties_xml])