From 1220f7bef882b2242bb1ff5890fbf91aa165f394 Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Thu, 21 Oct 2021 13:37:13 -0400 Subject: [PATCH] Allow embedded templates in Word documents --- crc/services/jinja_service.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crc/services/jinja_service.py b/crc/services/jinja_service.py index 5cb352ad..3f2d039a 100644 --- a/crc/services/jinja_service.py +++ b/crc/services/jinja_service.py @@ -1,8 +1,7 @@ -from docxtpl import DocxTemplate, Listing, InlineImage -from jinja2 import Environment, DictLoader - from docx.shared import Inches +from docxtpl import DocxTemplate, Listing, InlineImage from io import BytesIO +from jinja2 import Environment, DictLoader import copy @@ -41,16 +40,17 @@ Cool Right? # The rest of this is for using Word documents as Jinja templates # def make_template(self, binary_stream, context, image_file_data=None): - # TODO: Move this into the jinja_service? + templates = context doc = DocxTemplate(binary_stream) doc_context = copy.deepcopy(context) doc_context = self.rich_text_update(doc_context) doc_context = self.append_images(doc, doc_context, image_file_data) - jinja_env = Environment(autoescape=True) + jinja_env = Environment(loader=DictLoader(templates), autoescape=True) + try: doc.render(doc_context, jinja_env) except Exception as e: - print (e) + print(e) target_stream = BytesIO() doc.save(target_stream) target_stream.seek(0) # move to the beginning of the stream.