add RichTextParagraph class
This commit is contained in:
parent
a48a6a96b2
commit
9392b06da4
@ -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 <var> }}``.
|
||||
Have a look to the example here ``tests/richtextparagraph.py``.
|
||||
|
||||
|
||||
Inline image
|
||||
------------
|
||||
|
||||
|
||||
@ -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 += "<w:b/>"
|
||||
if rtl:
|
||||
prop += '<w:bCs/>'
|
||||
prop += "<w:bCs/>"
|
||||
if italic:
|
||||
prop += "<w:i/>"
|
||||
if rtl:
|
||||
prop += '<w:iCs/>'
|
||||
prop += "<w:iCs/>"
|
||||
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 += '<w:pStyle w:val="%s"/>' % parastyle
|
||||
@ -170,8 +175,6 @@ class RichTextParagraph(object):
|
||||
def __html__(self):
|
||||
return self.xml
|
||||
|
||||
|
||||
|
||||
|
||||
R = RichText
|
||||
RP = RichTextParagraph
|
||||
|
||||
@ -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'</w:t></w:r><w:r><w:t xml:space="preserve">\1</w:t></w:r><w:r><w:t xml:space="preserve">',
|
||||
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 <w:p> tags
|
||||
# This allow for inline {r <var> }} and paragraph {q <var> }) styling
|
||||
# This is mandatory to have jinja2 generating correct xml code
|
||||
pat = (
|
||||
r"<w:p[ >](?:(?!<w:p[ >]).)*({%%|{{)q ([^}%%]*(?:%%}|}})).*?</w:p>"
|
||||
|
||||
)
|
||||
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
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user