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
|
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
|
Inline image
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
|||||||
@ -47,6 +47,10 @@ class RichText(object):
|
|||||||
self.xml += text.xml
|
self.xml += text.xml
|
||||||
return
|
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 a string : cast to string (ex: int, dict etc...)
|
||||||
if not isinstance(text, (str, bytes)):
|
if not isinstance(text, (str, bytes)):
|
||||||
text = str(text)
|
text = str(text)
|
||||||
@ -76,11 +80,11 @@ class RichText(object):
|
|||||||
if bold:
|
if bold:
|
||||||
prop += "<w:b/>"
|
prop += "<w:b/>"
|
||||||
if rtl:
|
if rtl:
|
||||||
prop += '<w:bCs/>'
|
prop += "<w:bCs/>"
|
||||||
if italic:
|
if italic:
|
||||||
prop += "<w:i/>"
|
prop += "<w:i/>"
|
||||||
if rtl:
|
if rtl:
|
||||||
prop += '<w:iCs/>'
|
prop += "<w:iCs/>"
|
||||||
if underline:
|
if underline:
|
||||||
if underline not in [
|
if underline not in [
|
||||||
"single",
|
"single",
|
||||||
@ -128,6 +132,7 @@ class RichText(object):
|
|||||||
def __html__(self):
|
def __html__(self):
|
||||||
return self.xml
|
return self.xml
|
||||||
|
|
||||||
|
|
||||||
class RichTextParagraph(object):
|
class RichTextParagraph(object):
|
||||||
"""class to generate Rich Text Paragraphs when using templates variables
|
"""class to generate Rich Text Paragraphs when using templates variables
|
||||||
|
|
||||||
@ -149,7 +154,7 @@ class RichTextParagraph(object):
|
|||||||
# If a RichText is added
|
# If a RichText is added
|
||||||
if not isinstance(text, RichText):
|
if not isinstance(text, RichText):
|
||||||
text = RichText(text)
|
text = RichText(text)
|
||||||
|
|
||||||
prop = ""
|
prop = ""
|
||||||
if parastyle:
|
if parastyle:
|
||||||
prop += '<w:pStyle w:val="%s"/>' % parastyle
|
prop += '<w:pStyle w:val="%s"/>' % parastyle
|
||||||
@ -170,8 +175,6 @@ class RichTextParagraph(object):
|
|||||||
def __html__(self):
|
def __html__(self):
|
||||||
return self.xml
|
return self.xml
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
R = RichText
|
R = RichText
|
||||||
RP = RichTextParagraph
|
RP = RichTextParagraph
|
||||||
|
|||||||
@ -161,7 +161,7 @@ class DocxTemplate(object):
|
|||||||
flags=re.DOTALL,
|
flags=re.DOTALL,
|
||||||
)
|
)
|
||||||
src_xml = re.sub(
|
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">',
|
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,
|
src_xml,
|
||||||
flags=re.DOTALL,
|
flags=re.DOTALL,
|
||||||
@ -184,18 +184,6 @@ class DocxTemplate(object):
|
|||||||
% {"y": y}
|
% {"y": y}
|
||||||
)
|
)
|
||||||
src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)
|
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"]:
|
for y in ["tr", "tc", "p"]:
|
||||||
# same thing, but for {#y xxx #} (but not where y == 'r', since that
|
# 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
|
# make sure all template images defined by user were replaced
|
||||||
for img_id, replaced in replaced_pics.items():
|
for img_id, replaced in replaced_pics.items():
|
||||||
if not replaced:
|
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):
|
def get_pic_map(self):
|
||||||
return self.pic_map
|
return self.pic_map
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user