From 9a4dd4e28c28a011ec67569fc9201ecc9b9fb5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20MAUROY?= Date: Sat, 9 Nov 2019 12:14:24 +0100 Subject: [PATCH] Adds the ability to add a RichText object to another RichText object --- docxtpl/__init__.py | 4 ++++ tests/richtext.py | 7 +++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index d21da97..bba13a9 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -625,6 +625,10 @@ class RichText(object): font=None, url_id=None): + # If a RichText is added + if isinstance(text, RichText): + self.xml += text.xml + return # If not a string : cast to string (ex: int, dict etc...) if not isinstance(text, (six.text_type, six.binary_type)): diff --git a/tests/richtext.py b/tests/richtext.py index a791574..49132f6 100644 --- a/tests/richtext.py +++ b/tests/richtext.py @@ -9,7 +9,7 @@ from docxtpl import DocxTemplate, RichText tpl=DocxTemplate('templates/richtext_tpl.docx') -rt = RichText('an exemple of ') +rt = RichText() rt.add('a rich text', style='myrichtextstyle') rt.add(' with ') rt.add('some italic', italic=True) @@ -38,8 +38,11 @@ rt.add('superscript', superscript=True) rt.add(' and some') rt.add('subscript', subscript=True) +rt_embedded = RichText('an example of ') +rt_embedded.add(rt) + context = { - 'example' : rt, + 'example' : rt_embedded, } tpl.render(context)