Open mdzhatova opened 1 year ago
You are using keys with spaces in your context dictionary. I suspect that this is causing the problem. There are two possible solutions:
Use underscores instead of spaces in your context dictionary:
context = {
'Трите_имена': row['Трите имена'],
...
}
{{ Трите_имена }}
Keep keys of context dictionary unchanged, but quote them in the docx template:
context = {
'Трите имена': row['Трите имена'],
...
}
{{ 'Трите_имена' }}
I'm trying to iterate through an excel file, populate a docxtemplate from each row and save the docs. The excel file and the template are in Bulgarian language. I don't believe that's causing the error but I'm new to python, so I appreciate any help.
Reading the excel file is fine. I can display the results.
Below is my code and the error I get:
import pandas as pd import jinja2 from docxtpl import DocxTemplate
word_template_path = r"C:\Users\mdjat\Documents\ZdrDosieta\ZDRPrilojenieTemplate.docx" excel_path = r"C:\Users\mdjat\Documents\ZdrDosieta\ВИА ЛОГИСТИК_информация за ЗД_за тест.xlsm" doc = DocxTemplate (word_template_path) df = pd.read_excel (excel_path,sheet_name="Sheet1")
for index, row in df.iterrows (): context = {'Трите имена': row['Трите имена'], 'ЕГН': row['ЕГН'], 'Постоянен адрес': row['Постоянен адрес'], 'Населено място': row['Населено място'], 'Лекар': row['Лекар'], 'Длъжност': row['Длъжност'], 'Стаж': row['Стаж']} doc.render (context) output_path = r"C:\Users\mdjat\Documents\ZdrDosieta{row['Трите имена']}.docx" doc.save (output_path)
Traceback (most recent call last): File "c:\Users\mdjat\Documents\ZdrDosieta\zdr dosie python code.py", line 26, in
doc.render (context)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\docxtpl\template.py", line 343, in render
xml_src = self.build_xml(context, jinja_env)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\docxtpl\template.py", line 290, in build_xml
xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\docxtpl\template.py", line 245, in render_xml_part
raise exc
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\docxtpl\template.py", line 238, in render_xml_part
template = Template(src_xml)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\jinja2\environment.py", line 1208, in new
return env.from_string(source, template_class=cls)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\jinja2\environment.py", line 1105, in from_string
return cls.from_code(self, self.compile(source), gs, None)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\jinja2\environment.py", line 768, in compile
self.handle_exception(source=source_hint)
File "C:\Users\mdjat\anaconda3\envs\Streamlit\lib\site-packages\jinja2\environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "", line 8, in template
jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'имена'
I've read posts with similar errors but still don't get how to fix it.
Any help or direction would be greatly appreciated.