From 302f55a861ebca4171ab3ae786f9df987c8edbbb Mon Sep 17 00:00:00 2001 From: Alexeev Nickolay Date: Wed, 13 Feb 2019 19:33:27 +0300 Subject: [PATCH] (feat) implement xml properties and html attributes for "transform" usage --- mammoth/conversion.py | 7 +++++++ mammoth/documents.py | 15 ++++++++++++--- mammoth/docx/body_xml.py | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index d463116..6634da3 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -103,6 +103,9 @@ def children(): return [html.force_write] + content html_path = self._find_html_path_for_paragraph(paragraph) + if paragraph.html_attributes: + for el in html_path.elements: + el.tag.attributes = paragraph.html_attributes return html_path.wrap(children) @@ -124,6 +127,10 @@ def visit_run(self, run, context): if run.is_bold: paths.append(self._find_style_for_run_property("bold", default="strong")) paths.append(self._find_html_path_for_run(run)) + if run.html_attributes: + if isinstance(paths[0], html_paths.HtmlPath): + paths.insert(0, html_paths.element(["span"], fresh=False)) + paths[0].tag.attributes.update(run.html_attributes) for path in paths: nodes = partial(path.wrap, nodes) diff --git a/mammoth/documents.py b/mammoth/documents.py index b00f1bb..bc361af 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -22,6 +22,8 @@ class Paragraph(HasChildren): numbering = cobble.field() alignment = cobble.field() indent = cobble.field() + xml_properties = cobble.field() + html_attributes = cobble.field() @cobble.data @@ -51,6 +53,8 @@ class Run(HasChildren): is_small_caps = cobble.field() vertical_alignment = cobble.field() font = cobble.field() + xml_properties = cobble.field() + html_attributes = cobble.field() @cobble.data class Text(Element): @@ -104,11 +108,12 @@ 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, alignment=None, indent=None): +def paragraph(children, style_id=None, style_name=None, numbering=None, alignment=None, indent=None, xml_properties=None, html_attributes=None): if indent is None: indent = paragraph_indent() - - return Paragraph(children, style_id, style_name, numbering, alignment=alignment, indent=indent) + if html_attributes is None: + html_attributes = {} + return Paragraph(children, style_id, style_name, numbering, alignment=alignment, indent=indent, xml_properties=xml_properties, html_attributes=html_attributes) def paragraph_indent(start=None, end=None, first_line=None, hanging=None): return ParagraphIndent(start=start, end=end, first_line=first_line, hanging=hanging) @@ -124,6 +129,8 @@ def run( is_small_caps=None, vertical_alignment=None, font=None, + xml_properties=None, + html_attributes=None ): if vertical_alignment is None: vertical_alignment = VerticalAlignment.baseline @@ -138,6 +145,8 @@ def run( is_small_caps=bool(is_small_caps), vertical_alignment=vertical_alignment, font=font, + xml_properties=xml_properties, + html_attributes=html_attributes or {}, ) class VerticalAlignment(object): diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index d6ca993..69ce676 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -107,6 +107,7 @@ def add_complex_field_hyperlink(children): is_small_caps=is_small_caps, vertical_alignment=vertical_alignment, font=font, + xml_properties=properties )) def _read_run_style(properties): @@ -132,6 +133,7 @@ def paragraph(element): numbering=numbering, alignment=alignment, indent=indent, + xml_properties=properties, )).append_extra() def _read_paragraph_style(properties):