diff --git a/CHANGES.rst b/CHANGES.rst index 39efbc1..a86cdd5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +0.1.10 (2016-2-11) +------------------ +- render() accepts optionnal jinja_env argument : + useful to set custom filters and other things + 0.1.9 (2016-1-18) ----------------- - better subdoc management : accept tables diff --git a/docs/index.rst b/docs/index.rst index 4ae68fe..387231c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -118,6 +118,21 @@ If you want to add dynamically changeable style, you have to use both : the `{{r You can change color, bold, italic, size and so on, but the best way is to use Microsoft Word to define your own *caracter* style ( Home tab -> modify style -> manage style button -> New style, select ‘Character style’ in the form ), see exemple in `tests/richtext.py` +Jinja custom filters +-------------------- + +``render()`` accepts ``jinja_env`` optionnal argument : you may pass a jinja environment object. +By this way you will be able to add some custom jinja filters:: + + from docxtpl import DocxTemplate + import jinja2 + + doc = DocxTemplate("my_word_template.docx") + context = { 'company_name' : "World company" } + jinja_env = jinja2.Environment() + jinja_env.filters['myfilter'] = myfilterfunc + doc.render(context,jinja_env) + doc.save("generated_doc.docx") Examples -------- diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 01b1ff1..5d29d32 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -59,16 +59,19 @@ class DocxTemplate(object): return src_xml - def render_xml(self,src_xml,context): - template = Template(src_xml) + def render_xml(self,src_xml,context,jinja_env=None): + if jinja_env: + template = jinja_env.from_string(src_xml) + else: + template = Template(src_xml) dst_xml = template.render(context) dst_xml = dst_xml.replace('{_{','{{').replace('}_}','}}').replace('{_%','{%').replace('%_}','%}') return dst_xml - def build_xml(self,context): + def build_xml(self,context,jinja_env=None): xml = self.get_xml() xml = self.patch_xml(xml) - xml = self.render_xml(xml, context) + xml = self.render_xml(xml, context, jinja_env) return xml def map_xml(self,xml): @@ -76,8 +79,8 @@ class DocxTemplate(object): body = root.body root.replace(body,etree.fromstring(xml)) - def render(self,context): - xml = self.build_xml(context) + def render(self,context,jinja_env=None): + xml = self.build_xml(context,jinja_env) self.map_xml(xml) def new_subdoc(self): diff --git a/tests/test_files/cellbg.docx b/tests/test_files/cellbg.docx index b265148..606cee7 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 68ccc5c..afba0a7 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 e4c3aa6..50f772f 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 a4e389a..2466f8a 100644 Binary files a/tests/test_files/subdoc.docx and b/tests/test_files/subdoc.docx differ