aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-06-08 22:30:06 +0100
committerMichael Williamson <mike@zwobble.org>2014-06-08 22:30:06 +0100
commitb89d6302c0a0f03d7c33082fa4a4a7a277925f8a (patch)
tree048ec4607ee343351ea632aa097d9d65d9ad6719
parentf37fce68a77630a199737959e9d235d0a1263662 (diff)
Read run style name from docx document
-rw-r--r--mammoth/documents.py6
-rw-r--r--mammoth/docx/document_xml.py7
-rw-r--r--tests/docx/document_xml_tests.py22
3 files changed, 27 insertions, 8 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py
index 4438b06..703e436 100644
--- a/mammoth/documents.py
+++ b/mammoth/documents.py
@@ -3,7 +3,7 @@ import dodge
Document = dodge.data_class("Document", ["children"])
Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "style_name", "numbering"])
-Run = dodge.data_class("Run", ["children", "style_id", "is_bold", "is_italic"])
+Run = dodge.data_class("Run", ["children", "style_id", "style_name", "is_bold", "is_italic"])
Text = dodge.data_class("Text", ["value"])
Hyperlink = dodge.data_class("Hyperlink", ["href", "children"])
@@ -23,8 +23,8 @@ document = Document
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, is_bold=None, is_italic=None):
- return Run(children, style_id, bool(is_bold), bool(is_italic))
+def run(children, style_id=None, style_name=None, is_bold=None, is_italic=None):
+ return Run(children, style_id, style_name, bool(is_bold), bool(is_italic))
text = Text
diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py
index 83d82df..247bcbc 100644
--- a/mammoth/docx/document_xml.py
+++ b/mammoth/docx/document_xml.py
@@ -51,6 +51,12 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file):
style_id = properties \
.find_child_or_null("w:rStyle") \
.attributes.get("w:val")
+
+ if style_id is None:
+ style_name = None
+ else:
+ style_name = styles.find_character_style_by_id(style_id).name
+
is_bold = properties.find_child("w:b")
is_italic = properties.find_child("w:i")
@@ -58,6 +64,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file):
.map(lambda children: documents.run(
children=children,
style_id=style_id,
+ style_name=style_name,
is_bold=is_bold,
is_italic=is_italic,
))
diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py
index e8757f5..5c73cc3 100644
--- a/tests/docx/document_xml_tests.py
+++ b/tests/docx/document_xml_tests.py
@@ -96,9 +96,21 @@ class ReadXmlElementTests(object):
@istest
def run_has_style_id_read_from_run_properties_if_present(self):
- style_xml = xml_element("w:rStyle", {"w:val": "Emphasis"})
- run = self._read_run_with_properties([style_xml])
- assert_equal("Emphasis", run.style_id)
+ style_xml = xml_element("w:rStyle", {"w:val": "Heading1Char"})
+
+ styles = Styles({}, {"Heading1Char": Style(style_id="Heading1Char", name="Heading 1 Char")})
+
+ run = self._read_run_with_properties([style_xml], styles=styles)
+ assert_equal("Heading1Char", run.style_id)
+
+ @istest
+ def run_has_style_name_read_from_run_properties_and_styles(self):
+ style_xml = xml_element("w:rStyle", {"w:val": "Heading1Char"})
+
+ styles = Styles({}, {"Heading1Char": Style(style_id="Heading1Char", name="Heading 1 Char")})
+
+ run = self._read_run_with_properties([style_xml], styles=styles)
+ assert_equal("Heading 1 Char", run.style_name)
@istest
def run_is_not_bold_if_bold_element_is_not_present(self):
@@ -120,10 +132,10 @@ class ReadXmlElementTests(object):
run = self._read_run_with_properties([xml_element("w:i")])
assert_equal(True, run.is_italic)
- def _read_run_with_properties(self, properties):
+ def _read_run_with_properties(self, properties, styles=None):
properties_xml = xml_element("w:rPr", {}, properties)
run_xml = xml_element("w:r", {}, [properties_xml])
- return _read_and_get_document_xml_element(run_xml)
+ return _read_and_get_document_xml_element(run_xml, styles=styles)
@istest