From 01825a1fa23f44dbf89c774081679af6f1a1ec3c Mon Sep 17 00:00:00 2001 From: Anh Pham Date: Fri, 11 Jan 2019 15:56:03 +1100 Subject: [PATCH] Add getting undeclared variables in the template --- docxtpl/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 5b4890d..9596c6d 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -13,7 +13,7 @@ from docx import Document from docx.opc.oxml import serialize_part_xml, parse_xml import docx.oxml.ns from docx.opc.constants import RELATIONSHIP_TYPE as REL_TYPE -from jinja2 import Template +from jinja2 import Environment, Template, meta from jinja2.exceptions import TemplateError try: from html import escape, unescape @@ -41,6 +41,14 @@ class DocxTemplate(object): HEADER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header" FOOTER_URI = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" + @property + def undeclared_template_variables(self): + xml = self.get_xml() + xml = self.patch_xml(xml) + env = Environment() + parse_content = env.parse(xml) + return meta.find_undeclared_variables(parse_content) + def __init__(self, docx): self.docx = Document(docx) self.crc_to_new_media = {}