Adds the ability to add a RichText object to another RichText object

This commit is contained in:
Sébastien MAUROY 2019-11-09 12:14:24 +01:00
parent 74fbb615bb
commit 9a4dd4e28c
2 changed files with 9 additions and 2 deletions

View File

@ -625,6 +625,10 @@ class RichText(object):
font=None, font=None,
url_id=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 a string : cast to string (ex: int, dict etc...)
if not isinstance(text, (six.text_type, six.binary_type)): if not isinstance(text, (six.text_type, six.binary_type)):

View File

@ -9,7 +9,7 @@ from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/richtext_tpl.docx') tpl=DocxTemplate('templates/richtext_tpl.docx')
rt = RichText('an exemple of ') rt = RichText()
rt.add('a rich text', style='myrichtextstyle') rt.add('a rich text', style='myrichtextstyle')
rt.add(' with ') rt.add(' with ')
rt.add('some italic', italic=True) rt.add('some italic', italic=True)
@ -38,8 +38,11 @@ rt.add('superscript', superscript=True)
rt.add(' and some') rt.add(' and some')
rt.add('subscript', subscript=True) rt.add('subscript', subscript=True)
rt_embedded = RichText('an example of ')
rt_embedded.add(rt)
context = { context = {
'example' : rt, 'example' : rt_embedded,
} }
tpl.render(context) tpl.render(context)