Skip to content

Commit 9392b06

Browse files
author
Eric Lapouyade
committed
add RichTextParagraph class
1 parent a48a6a9 commit 9392b06

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ You can add an hyperlink to a text by using a Richtext with this syntax::
263263

264264
Put ``rt`` in your context, then use ``{{r rt}}`` in your template
265265

266+
RichTextParagraph
267+
-----------------
268+
269+
If you want to change paragraph properties, you can use ``RichTextParagraph()`` or ``RP()`` object.
270+
It must be added to the template by using ``{{p <var> }}``.
271+
Have a look to the example here ``tests/richtextparagraph.py``.
272+
273+
266274
Inline image
267275
------------
268276

docxtpl/richtext.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def add(
4747
self.xml += text.xml
4848
return
4949

50+
# # If nothing to add : just return
51+
# if text is None or text == "":
52+
# return
53+
5054
# If not a string : cast to string (ex: int, dict etc...)
5155
if not isinstance(text, (str, bytes)):
5256
text = str(text)
@@ -76,11 +80,11 @@ def add(
7680
if bold:
7781
prop += "<w:b/>"
7882
if rtl:
79-
prop += '<w:bCs/>'
83+
prop += "<w:bCs/>"
8084
if italic:
8185
prop += "<w:i/>"
8286
if rtl:
83-
prop += '<w:iCs/>'
87+
prop += "<w:iCs/>"
8488
if underline:
8589
if underline not in [
8690
"single",
@@ -128,6 +132,7 @@ def __str__(self):
128132
def __html__(self):
129133
return self.xml
130134

135+
131136
class RichTextParagraph(object):
132137
"""class to generate Rich Text Paragraphs when using templates variables
133138
@@ -149,7 +154,7 @@ def add(
149154
# If a RichText is added
150155
if not isinstance(text, RichText):
151156
text = RichText(text)
152-
157+
153158
prop = ""
154159
if parastyle:
155160
prop += '<w:pStyle w:val="%s"/>' % parastyle
@@ -170,8 +175,6 @@ def __str__(self):
170175
def __html__(self):
171176
return self.xml
172177

173-
174-
175178

176179
R = RichText
177180
RP = RichTextParagraph

docxtpl/template.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def cellbg(m):
161161
flags=re.DOTALL,
162162
)
163163
src_xml = re.sub(
164-
r"({{[rq]\s.*?}}|{%[rq].\s.*?%})",
164+
r"({{r\s.*?}}|{%r\s.*?%})",
165165
r'</w:t></w:r><w:r><w:t xml:space="preserve">\1</w:t></w:r><w:r><w:t xml:space="preserve">',
166166
src_xml,
167167
flags=re.DOTALL,
@@ -184,18 +184,6 @@ def cellbg(m):
184184
% {"y": y}
185185
)
186186
src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)
187-
188-
# For paragraph level richtext
189-
# replace into xml paragraph containing
190-
# {%q xxx %} or {{q xxx}} template tag
191-
# by {% xxx %} or {{ xx }} without any surrounding <w:p> tags
192-
# This allow for inline {r <var> }} and paragraph {q <var> }) styling
193-
# This is mandatory to have jinja2 generating correct xml code
194-
pat = (
195-
r"<w:p[ >](?:(?!<w:p[ >]).)*({%%|{{)q ([^}%%]*(?:%%}|}})).*?</w:p>"
196-
197-
)
198-
src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)
199187

200188
for y in ["tr", "tc", "p"]:
201189
# same thing, but for {#y xxx #} (but not where y == 'r', since that
@@ -814,7 +802,9 @@ def _replace_pics(self):
814802
# make sure all template images defined by user were replaced
815803
for img_id, replaced in replaced_pics.items():
816804
if not replaced:
817-
raise ValueError("Picture %s not found in the docx template" % img_id)
805+
raise ValueError(
806+
"Picture %s not found in the docx template" % img_id
807+
)
818808

819809
def get_pic_map(self):
820810
return self.pic_map
-6.11 KB
Binary file not shown.

tests/templates/richtext_tpl.docx

-2.62 KB
Binary file not shown.

0 commit comments

Comments
 (0)