better xml code cleaning around Jinja2 tags

This commit is contained in:
Eric Lapouyade 2015-11-05 09:30:23 +01:00
parent 630dfe996f
commit 67fd432dbd
8 changed files with 19 additions and 7 deletions

View File

@ -1,3 +1,7 @@
0.1.8 (2015-11-05)
------------------
- better xml code cleaning around Jinja2 tags
0.1.7 (2015-09-09)
------------------
- python 3 support

View File

@ -38,6 +38,8 @@ You save the document as a .docx file (xml format) : it will be your .docx templ
Now you can use python-docx-template to generate as many word documents you want from this .docx template and context variables you will associate.
Note : python-docx-template as been tested with MS Word 97, it may not work with other version.
Jinja2-like syntax
------------------

View File

@ -26,18 +26,16 @@ class DocxTemplate(object):
return self.docx
def get_xml(self):
return etree.tostring(self.docx._element.body, encoding='unicode', pretty_print=True)
# Be careful : pretty_print MUST be set to False, otherwise patch_xml() won't work properly
return etree.tostring(self.docx._element.body, encoding='unicode', pretty_print=False)
def write_xml(self,filename):
with open(filename,'w') as fh:
fh.write(self.get_xml())
def patch_xml(self,src_xml):
# strip all xml tags inside {% %} and {{ }}
# that Microsoft word can insert into xml code for this part of the document
# A essayer : src_xml = re.sub(r'(?<={)(<[^>]*>)+(?=[\{%])|(?<=[%\}])(<[^>]*>)+(?=\})','',src_xml,flags=re.DOTALL)
# strip all xml tags inside {% %} and {{ }} that MS word can insert into xml source
src_xml = re.sub(r'(?<={)(<[^>]*>)+(?=[\{%])|(?<=[%\}])(<[^>]*>)+(?=\})','',src_xml,flags=re.DOTALL)
def striptags(m):
return re.sub('</w:t>.*?(<w:t>|<w:t [^>]*>)','',m.group(0),flags=re.DOTALL)
src_xml = re.sub(r'{%(?:(?!%}).)*|{{(?:(?!}}).)*',striptags,src_xml,flags=re.DOTALL)

View File

@ -23,8 +23,16 @@ News
%(CHANGES)s
""" % read('README', 'CHANGES')
def get_version(pkg):
path = os.path.join(os.path.dirname(__file__),pkg,'__init__.py')
with open(path) as fh:
m = re.search(r'^__version__\s*=\s*[\'"]([^\'"]+)[\'"]',fh.read(),re.M)
if m:
return m.group(1)
raise RuntimeError("Unable to find __version__ string in %s." % path)
setup(name='docxtpl',
version='0.1.7',
version=get_version('docxtpl'),
description='Python docx template engine',
long_description=long_description,
classifiers=[

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.