Fix bug when using more than one {{r }} or {%r %} in the same run

This commit is contained in:
Eric Lapouyade 2017-06-21 00:15:43 +02:00
parent 00db8d49c8
commit dac4425a1f
16 changed files with 26 additions and 1 deletions

View File

@ -1,3 +1,7 @@
0.3.8 (2017-06-20)
------------------
- Fix bug when using more than one {{r }} or {%r %} in the same run
0.3.7 (2017-06-13)
------------------
- Fix git tag v0.3.6 was in fact for 0.3.5 package version

View File

@ -5,7 +5,7 @@ Created : 2015-03-12
@author: Eric Lapouyade
'''
__version__ = '0.3.7'
__version__ = '0.3.8'
from lxml import etree
from docx import Document
@ -67,6 +67,9 @@ class DocxTemplate(object):
return re.sub(r'(<w:tcPr[^>]*>)',r'\1<w:shd w:val="clear" w:color="auto" w:fill="{{%s}}"/>' % m.group(2), cell_xml)
src_xml = re.sub(r'(<w:tc[ >](?:(?!<w:tc[ >]).)*){%\s*cellbg\s+([^%]*)\s*%}(.*?</w:tc>)',cellbg,src_xml,flags=re.DOTALL)
# avoid {{r and {%r tags to strip MS xml tags too far
src_xml = re.sub(r'({{r.*?}}|{%r.*?%})',r'</w:t></w:r><w:r><w:t>\1</w:t></w:r><w:r><w:t>',src_xml,flags=re.DOTALL)
for y in ['tr', 'tc', 'p', 'r']:
# replace into xml code the row/paragraph/run containing {%y xxx %} or {{y xxx}} template tag
# by {% xxx %} or {{ xx }} without any surronding <w:y> tags :

18
tests/richtext_and_if.py Normal file
View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
'''
Created : 2015-03-26
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('test_files/richtext_and_if_tpl.docx')
context = {
'foobar': RichText('Foobar!', color='ff0000')
}
tpl.render(context)
tpl.save('test_files/richtext_and_if.docx')

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.