I am trying to automate population of word template using python. The library I am using is docxtpl to help me do that. I am getting this expected token ‘end of print statement’ got ‘Size’ error and don’t know where to look for help. It’s some sort of jinja2 error which I haven’t come across before. Any help will be much appreciated.
The code is below:
import jinja2
from docxtpl import DocxTemplate
from jinja2 import Template
doc= DocxTemplate(path to the doc)
context= {“name”:”ABCD”}
doc.render(context)
doc.save(populated_template.docx)
Tried the above and many other solutions and nothing has worked. Stuck on this issue for hours. Have installed and uninstalled packages to help alleviate the issue but with no luck.
Full traceback below:
TemplateSyntaxError Traceback (most recent call last)
<ipython-input-21-20a405221771> in <module>
----> 1 doc.render(context)
C:\Anaconda3\lib\site-packages\docxtpl\template.py in render(self, context, jinja_env, autoescape)
341
342 # Body
--> 343 xml_src = self.build_xml(context, jinja_env)
344
345 # fix tables if needed
C:\Anaconda3\lib\site-packages\docxtpl\template.py in build_xml(self, context, jinja_env)
288 xml = self.get_xml()
289 xml = self.patch_xml(xml)
--> 290 xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
291 return xml
292
C:\Anaconda3\lib\site-packages\docxtpl\template.py in render_xml_part(self, src_xml, part, context, jinja_env)
243 exc.docx_context = map(lambda x: re.sub(r'<[^>]+>', '', x),
244 src_xml.splitlines()[line_number:(line_number + 7)])
--> 245 raise exc
246 dst_xml = re.sub(r'\n<w:p([ >])', r'<w:p\1', dst_xml)
247 dst_xml = (dst_xml
C:\Anaconda3\lib\site-packages\docxtpl\template.py in render_xml_part(self, src_xml, part, context, jinja_env)
236 template = jinja_env.from_string(src_xml)
237 else:
--> 238 template = Template(src_xml)
239 dst_xml = template.render(context)
240 except TemplateError as exc:
C:\Anaconda3\lib\site-packages\jinja2\environment.py in __new__(cls, source, block_start_string, block_end_string, variable_start_string, variable_end_string, comment_start_string, comment_end_string, line_statement_prefix, line_comment_prefix, trim_blocks, lstrip_blocks, newline_sequence, keep_trailing_newline, extensions, optimized, undefined, finalize, autoescape, enable_async)
1029 enable_async,
1030 )
-> 1031 return env.from_string(source, template_class=cls)
1032
1033 @classmethod
C:\Anaconda3\lib\site-packages\jinja2\environment.py in from_string(self, source, globals, template_class)
939 globals = self.make_globals(globals)
940 cls = template_class or self.template_class
--> 941 return cls.from_code(self, self.compile(source), globals, None)
942
943 def make_globals(self, d):
C:\Anaconda3\lib\site-packages\jinja2\environment.py in compile(self, source, name, filename, raw, defer_init)
636 return self._compile(source, filename)
637 except TemplateSyntaxError:
--> 638 self.handle_exception(source=source_hint)
639
640 def compile_expression(self, source, undefined_to_none=True):
C:\Anaconda3\lib\site-packages\jinja2\environment.py in handle_exception(self, source)
830 from .debug import rewrite_traceback_stack
831
--> 832 reraise(*rewrite_traceback_stack(source=source))
833
834 def join_path(self, template, parent):
C:\Anaconda3\lib\site-packages\jinja2\_compat.py in reraise(tp, value, tb)
26 def reraise(tp, value, tb=None):
27 if value.__traceback__ is not tb:
---> 28 raise value.with_traceback(tb)
29 raise value
30
<unknown> in template()
TemplateSyntaxError: expected token 'end of print statement', got 'Size'