parent
528c3b1ca5
commit
9946c74fdc
@ -1,3 +1,7 @@
|
|||||||
|
0.10.3 (2020-10-15)
|
||||||
|
-------------------
|
||||||
|
- \n \t and \f are now accepted in simple context string (#307, #312)
|
||||||
|
|
||||||
0.10.1 (2020-08-23)
|
0.10.1 (2020-08-23)
|
||||||
-------------------
|
-------------------
|
||||||
- Remove extension testing (#297)
|
- Remove extension testing (#297)
|
||||||
|
|||||||
@ -7,7 +7,7 @@ Created : 2015-03-12
|
|||||||
import functools
|
import functools
|
||||||
import io
|
import io
|
||||||
|
|
||||||
__version__ = '0.10.2'
|
__version__ = '0.10.3'
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from docx import Document
|
from docx import Document
|
||||||
@ -27,6 +27,7 @@ import six
|
|||||||
import binascii
|
import binascii
|
||||||
import os
|
import os
|
||||||
import zipfile
|
import zipfile
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
NEWLINE_XML = '</w:t><w:br/><w:t xml:space="preserve">'
|
NEWLINE_XML = '</w:t><w:br/><w:t xml:space="preserve">'
|
||||||
NEWPARAGRAPH_XML = '</w:t></w:r></w:p><w:p><w:r><w:t xml:space="preserve">'
|
NEWPARAGRAPH_XML = '</w:t></w:r></w:p><w:p><w:r><w:t xml:space="preserve">'
|
||||||
@ -234,8 +235,33 @@ class DocxTemplate(object):
|
|||||||
.replace('}_}', '}}')
|
.replace('}_}', '}}')
|
||||||
.replace('{_%', '{%')
|
.replace('{_%', '{%')
|
||||||
.replace('%_}', '%}'))
|
.replace('%_}', '%}'))
|
||||||
|
dst_xml = self.resolve_listing(dst_xml)
|
||||||
return dst_xml
|
return dst_xml
|
||||||
|
|
||||||
|
def resolve_listing(self, xml):
|
||||||
|
xml = xml.replace('\n', NEWLINE_XML)
|
||||||
|
xml = xml.replace('\f', PAGE_BREAK)
|
||||||
|
|
||||||
|
def resolve_run(paragraph_properties, m):
|
||||||
|
run_properties = re.search(r'<w:rPr>.*</w:rPr>', m[0])
|
||||||
|
run_properties = run_properties[0] if run_properties else ''
|
||||||
|
xml = m[0].replace('\t', '</w:t></w:r>'
|
||||||
|
'<w:r>%s<w:tab/></w:r>'
|
||||||
|
'<w:r>%s<w:t xml:space="preserve">' % (run_properties, run_properties))
|
||||||
|
return xml.replace('\a', '</w:t></w:r></w:p><w:p>%s<w:r>%s<w:t>' % (paragraph_properties, run_properties))
|
||||||
|
|
||||||
|
def resolve_paragraph(m):
|
||||||
|
paragraph_properties = re.search(r'<w:pPr>.*</w:pPr>', m[0])
|
||||||
|
paragraph_properties = paragraph_properties[0] if paragraph_properties else ''
|
||||||
|
|
||||||
|
p_resolve_run = partial(resolve_run, paragraph_properties)
|
||||||
|
|
||||||
|
return re.sub(r'<w:r(?: [^>]*)?>.*?</w:r>', p_resolve_run, m[0])
|
||||||
|
|
||||||
|
xml = re.sub(r'<w:p(?: [^>]*)?>.*?</w:p>', resolve_paragraph, xml)
|
||||||
|
|
||||||
|
return xml
|
||||||
|
|
||||||
def build_xml(self, context, jinja_env=None):
|
def build_xml(self, context, jinja_env=None):
|
||||||
xml = self.get_xml()
|
xml = self.get_xml()
|
||||||
xml = self.patch_xml(xml)
|
xml = self.patch_xml(xml)
|
||||||
|
|||||||
@ -6,7 +6,7 @@ context = {
|
|||||||
'myvar': R(
|
'myvar': R(
|
||||||
'"less than" must be escaped : <, this can be done with RichText() or R()'
|
'"less than" must be escaped : <, this can be done with RichText() or R()'
|
||||||
),
|
),
|
||||||
'myescvar': 'It can be escaped with a "|e" jinja filter in the template too : < ',
|
'myescvar': 'Without using Richtext, you can simply escape text with a "|e" jinja filter in the template : < ',
|
||||||
'nlnp': R(
|
'nlnp': R(
|
||||||
'Here is a multiple\nlines\nstring\aand some\aother\aparagraphs\aNOTE: the current character styling is removed'
|
'Here is a multiple\nlines\nstring\aand some\aother\aparagraphs\aNOTE: the current character styling is removed'
|
||||||
),
|
),
|
||||||
@ -14,6 +14,11 @@ context = {
|
|||||||
'the listing\nwith\nsome\nlines\nand special chars : <>&\f ... and a page break'
|
'the listing\nwith\nsome\nlines\nand special chars : <>&\f ... and a page break'
|
||||||
),
|
),
|
||||||
'page_break': R('\f'),
|
'page_break': R('\f'),
|
||||||
|
'new_listing': """With the latest version of docxtpl,
|
||||||
|
there is no need to use Listing objects anymore.
|
||||||
|
Just use \\n for newline,\n\\t\t for tabulation and \\f for ...\f...page break
|
||||||
|
One can also use special chars : <>& but you have then to add "|e" jinja filter in the template
|
||||||
|
""",
|
||||||
}
|
}
|
||||||
|
|
||||||
tpl.render(context)
|
tpl.render(context)
|
||||||
|
|||||||
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user