Add {% vm %} to merge cell vertically within a loop

This commit is contained in:
Eric Lapouyade 2017-10-15 16:29:41 +02:00
parent 41671a0363
commit 3d96e6e5f1
22 changed files with 19 additions and 3 deletions

View File

@ -1,3 +1,7 @@
0.4.5 (2017-10-15)
------------------
- Add {% vm %} to merge cell vertically within a loop (Thanks to Arthaslixin)
0.4.4 (2017-10-13)
------------------
- use six.iteritems() instead of iteritems for python 3 compatibility

View File

@ -215,6 +215,17 @@ WARNING : unlike replace_pic() method, embdded_dummy.docx MUST exist in the temp
file as the one inserted manually in the docx template.
The replacement occurs in headers, footers and the whole document's body.
Tables
------
You can span table cells in two ways, horizontally (see tests/dynamic_table.py) by using::
{% colspan <number of column to span> %}
or vertically within a for loop (see tests/vertical_merge.py)::
{% vm %}
Jinja custom filters
--------------------

View File

@ -5,7 +5,7 @@ Created : 2015-03-12
@author: Eric Lapouyade
'''
__version__ = '0.4.4'
__version__ = '0.4.5'
from lxml import etree
from docx import Document
@ -88,10 +88,10 @@ class DocxTemplate(object):
# add vMerge
# use {% vm %} to make this table cell and its copies be vertically merged within a {% for %}
pat_vm = r'(<\/w:tcPr>.*)(<\/w:tcPr>)(.*?){%vm%}.*?<w:t>(.*?)(<\/w:t>)'
pat_vm = r'(<\/w:tcPr>.*)(<\/w:tcPr>)(.*?){%\s*vm\s*%}.*?<w:t>(.*?)(<\/w:t>)'
def vMerge(m):
return m.group(1) + '<w:vMerge {% if loop.first %}w:val="restart"{% endif %}/>' + m.group(2) + m.group(3) + "{% if loop.first %}"+ m.group(4) +"{% endif %}" + m.group(5)
pat_num_vm = re.compile(r'{%vm%}')
pat_num_vm = re.compile(r'{%\s*vm\s*%}')
num = len(pat_num_vm.findall(src_xml))
for i in range(0,num):
src_xml = re.sub(pat_vm,vMerge,src_xml)
@ -108,6 +108,7 @@ class DocxTemplate(object):
else:
template = Template(src_xml)
dst_xml = template.render(context)
print dst_xml
dst_xml = dst_xml.replace('{_{','{{').replace('}_}','}}').replace('{_%','{%').replace('%_}','%}')
return dst_xml

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.