{%- xxx -%} merging feature

This commit is contained in:
Eric Lapouyade 2020-04-14 22:51:36 +02:00
parent ccac13390a
commit 97be69b950
3 changed files with 22 additions and 0 deletions

View File

@ -109,6 +109,11 @@ class DocxTemplate(object):
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,flags=re.DOTALL) src_xml,flags=re.DOTALL)
# {%- will merge with previous paragraph text
src_xml = re.sub(r'</w:t>(?:(?!</w:t>).)*?{%-','{%',src_xml,flags=re.DOTALL)
# -%} will merge with next paragraph text
src_xml = re.sub(r'-%}(?:(?!<w:t[ >]).)*?<w:t[^>]*?>','%}',src_xml,flags=re.DOTALL)
for y in ['tr', 'tc', 'p', 'r']: for y in ['tr', 'tc', 'p', 'r']:
# replace into xml code the row/paragraph/run containing # replace into xml code the row/paragraph/run containing
# {%y xxx %} or {{y xxx}} template tag # {%y xxx %} or {{y xxx}} template tag

17
tests/merge_paragraph.py Normal file
View File

@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
'''
Created : 2015-03-12
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate
tpl=DocxTemplate('templates/merge_paragraph_tpl.docx')
context = {
'living_in_town' : True,
}
tpl.render(context)
tpl.save('output/merge_paragraph.docx')

Binary file not shown.