diff --git a/docs/index.rst b/docs/index.rst index 517c3e8..a742d49 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -263,6 +263,14 @@ You can add an hyperlink to a text by using a Richtext with this syntax:: Put ``rt`` in your context, then use ``{{r rt}}`` in your template +RichTextParagraph +----------------- + +If you want to change paragraph properties, you can use ``RichTextParagraph()`` or ``RP()`` object. +It must be added to the template by using ``{{p }}``. +Have a look to the example here ``tests/richtextparagraph.py``. + + Inline image ------------ diff --git a/docxtpl/richtext.py b/docxtpl/richtext.py index 5959d68..f0f4738 100644 --- a/docxtpl/richtext.py +++ b/docxtpl/richtext.py @@ -47,6 +47,10 @@ class RichText(object): self.xml += text.xml return + # # If nothing to add : just return + # if text is None or text == "": + # return + # If not a string : cast to string (ex: int, dict etc...) if not isinstance(text, (str, bytes)): text = str(text) @@ -76,11 +80,11 @@ class RichText(object): if bold: prop += "" if rtl: - prop += '' + prop += "" if italic: prop += "" if rtl: - prop += '' + prop += "" if underline: if underline not in [ "single", @@ -128,6 +132,7 @@ class RichText(object): def __html__(self): return self.xml + class RichTextParagraph(object): """class to generate Rich Text Paragraphs when using templates variables @@ -149,7 +154,7 @@ class RichTextParagraph(object): # If a RichText is added if not isinstance(text, RichText): text = RichText(text) - + prop = "" if parastyle: prop += '' % parastyle @@ -170,8 +175,6 @@ class RichTextParagraph(object): def __html__(self): return self.xml - - R = RichText RP = RichTextParagraph diff --git a/docxtpl/template.py b/docxtpl/template.py index e873958..d24d691 100644 --- a/docxtpl/template.py +++ b/docxtpl/template.py @@ -161,7 +161,7 @@ class DocxTemplate(object): flags=re.DOTALL, ) src_xml = re.sub( - r"({{[rq]\s.*?}}|{%[rq].\s.*?%})", + r"({{r\s.*?}}|{%r\s.*?%})", r'\1', src_xml, flags=re.DOTALL, @@ -184,18 +184,6 @@ class DocxTemplate(object): % {"y": y} ) src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL) - - # For paragraph level richtext - # replace into xml paragraph containing - # {%q xxx %} or {{q xxx}} template tag - # by {% xxx %} or {{ xx }} without any surrounding tags - # This allow for inline {r }} and paragraph {q }) styling - # This is mandatory to have jinja2 generating correct xml code - pat = ( - r"](?:(?!]).)*({%%|{{)q ([^}%%]*(?:%%}|}})).*?" - - ) - src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL) for y in ["tr", "tc", "p"]: # same thing, but for {#y xxx #} (but not where y == 'r', since that @@ -814,7 +802,9 @@ class DocxTemplate(object): # make sure all template images defined by user were replaced for img_id, replaced in replaced_pics.items(): if not replaced: - raise ValueError("Picture %s not found in the docx template" % img_id) + raise ValueError( + "Picture %s not found in the docx template" % img_id + ) def get_pic_map(self): return self.pic_map diff --git a/tests/templates/richtext_paragraph_tpl.docx b/tests/templates/richtext_paragraph_tpl.docx index 993b466..4ae4501 100644 Binary files a/tests/templates/richtext_paragraph_tpl.docx and b/tests/templates/richtext_paragraph_tpl.docx differ diff --git a/tests/templates/richtext_tpl.docx b/tests/templates/richtext_tpl.docx index be34203..bcf589e 100644 Binary files a/tests/templates/richtext_tpl.docx and b/tests/templates/richtext_tpl.docx differ