Error: Unexpected : "}" using docxtpl but can't seem to find where it is in my code. Am I reading the error wrong?

282 Views Asked by At

I'm using Python 3.11.2

The tags I have in my word document are as follows: {{first_name}} {{last name}}

My code is below creating tags to replace in a word document. I kept the tags and variables the same name for simplicity's sake.

from docxtpl import DocxTemplate
from datetime import datetime

first_name, last_name = "John", "Doe"
job_title1, job_title2 = "Financier", "Manager"

doc = DocxTemplate("Resume_Template_John.docx")

context = {'first_name': first_name, 'last_name': last_name, 'job_title1': job_title1, 'job_title2': job_title2}

doc.render(context)

The error I get is in the last line:

Cell In[7], line 1
----> 1 doc.render(context)

File c:\Users\Anonymous\OneDrive\Career\02. Resume\Py_Resume_Builder\Lib\site-packages\docxtpl\template.py:368, in DocxTemplate.render(self, context, jinja_env, autoescape)
    365         jinja_env.autoescape = autoescape
    367 # Body
--> 368 xml_src = self.build_xml(context, jinja_env)
    370 # fix tables if needed
    371 tree = self.fix_tables(xml_src)

File c:\Users\Anonymous\OneDrive\Career\02. Resume\Py_Resume_Builder\Lib\site-packages\docxtpl\template.py:315, in DocxTemplate.build_xml(self, context, jinja_env)
    313 xml = self.get_xml()
    314 xml = self.patch_xml(xml)
--> 315 xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
    316 return xml

File c:\Users\Anonymous\OneDrive\Career\02. Resume\Py_Resume_Builder\Lib\site-packages\docxtpl\template.py:245, in DocxTemplate.render_xml_part(self, src_xml, part, context, jinja_env)
    242         line_number = max(exc.lineno - 4, 0)
    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])', r'<w:p\1', dst_xml)
    247 dst_xml = (dst_xml
...
--> 936 raise rewrite_traceback_stack(source=source)

File :5, in template()

TemplateSyntaxError: unexpected '}'
0

There are 0 best solutions below