better xml code cleaning around Jinja2 tags
This commit is contained in:
parent
630dfe996f
commit
67fd432dbd
@ -1,3 +1,7 @@
|
|||||||
|
0.1.8 (2015-11-05)
|
||||||
|
------------------
|
||||||
|
- better xml code cleaning around Jinja2 tags
|
||||||
|
|
||||||
0.1.7 (2015-09-09)
|
0.1.7 (2015-09-09)
|
||||||
------------------
|
------------------
|
||||||
- python 3 support
|
- python 3 support
|
||||||
|
|||||||
@ -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.
|
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
|
Jinja2-like syntax
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
|||||||
@ -26,18 +26,16 @@ class DocxTemplate(object):
|
|||||||
return self.docx
|
return self.docx
|
||||||
|
|
||||||
def get_xml(self):
|
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):
|
def write_xml(self,filename):
|
||||||
with open(filename,'w') as fh:
|
with open(filename,'w') as fh:
|
||||||
fh.write(self.get_xml())
|
fh.write(self.get_xml())
|
||||||
|
|
||||||
def patch_xml(self,src_xml):
|
def patch_xml(self,src_xml):
|
||||||
# strip all xml tags inside {% %} and {{ }}
|
# strip all xml tags inside {% %} and {{ }} that MS word can insert into xml source
|
||||||
# that Microsoft word can insert into xml code for this part of the document
|
src_xml = re.sub(r'(?<={)(<[^>]*>)+(?=[\{%])|(?<=[%\}])(<[^>]*>)+(?=\})','',src_xml,flags=re.DOTALL)
|
||||||
|
|
||||||
# A essayer : src_xml = re.sub(r'(?<={)(<[^>]*>)+(?=[\{%])|(?<=[%\}])(<[^>]*>)+(?=\})','',src_xml,flags=re.DOTALL)
|
|
||||||
|
|
||||||
def striptags(m):
|
def striptags(m):
|
||||||
return re.sub('</w:t>.*?(<w:t>|<w:t [^>]*>)','',m.group(0),flags=re.DOTALL)
|
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)
|
src_xml = re.sub(r'{%(?:(?!%}).)*|{{(?:(?!}}).)*',striptags,src_xml,flags=re.DOTALL)
|
||||||
|
|||||||
10
setup.py
10
setup.py
@ -23,8 +23,16 @@ News
|
|||||||
%(CHANGES)s
|
%(CHANGES)s
|
||||||
""" % read('README', 'CHANGES')
|
""" % 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',
|
setup(name='docxtpl',
|
||||||
version='0.1.7',
|
version=get_version('docxtpl'),
|
||||||
description='Python docx template engine',
|
description='Python docx template engine',
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
classifiers=[
|
classifiers=[
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user