diff --git a/CHANGES.rst b/CHANGES.rst index 88d3861..6d30e3d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/docs/index.rst b/docs/index.rst index b9b9d68..ffe40b4 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 ------------------ diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 5244a3b..ae6f821 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -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('.*?(|]*>)','',m.group(0),flags=re.DOTALL) src_xml = re.sub(r'{%(?:(?!%}).)*|{{(?:(?!}}).)*',striptags,src_xml,flags=re.DOTALL) diff --git a/setup.py b/setup.py index e66b15c..4cf4093 100644 --- a/setup.py +++ b/setup.py @@ -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=[ diff --git a/tests/test_files/cellbg.docx b/tests/test_files/cellbg.docx index 89411a2..4668431 100644 Binary files a/tests/test_files/cellbg.docx and b/tests/test_files/cellbg.docx differ diff --git a/tests/test_files/order.docx b/tests/test_files/order.docx index fd8ed59..8e2daf0 100644 Binary files a/tests/test_files/order.docx and b/tests/test_files/order.docx differ diff --git a/tests/test_files/richtext.docx b/tests/test_files/richtext.docx index 4fa6764..f87cdbb 100644 Binary files a/tests/test_files/richtext.docx and b/tests/test_files/richtext.docx differ diff --git a/tests/test_files/subdoc.docx b/tests/test_files/subdoc.docx index 0d5ff32..02c6f0b 100644 Binary files a/tests/test_files/subdoc.docx and b/tests/test_files/subdoc.docx differ