[WIP] Support rendering variables in footnotes

This commit is contained in:
Bart Broere 2024-09-17 15:09:29 +02:00 committed by GitHub
parent 0607e7175d
commit 0061b556ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -351,6 +351,23 @@ class DocxTemplate(object):
rendered = template.render(context)
setattr(self.docx.core_properties, prop, rendered)
def render_footnotes(
self, context: Dict[str, Any], jinja_env: Optional[Environment] = None
) -> None:
if jinja_env is None:
jinja_env = Environment()
for k, v in self.docx.sections[0].part.related_parts.items():
if v.content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml':
import xml.etree.ElementTree as ET
tree = ET.fromstring(v.blob)
for footnote in tree.findall('.//w:t', docx.oxml.ns.nsmap):
if hasattr(footnote, 'text'):
footnote.text = jinja_env.from_string(footnote.text).render(context)
for part in self.docx.sections[0].part.related_parts[k].package.parts:
if part.partname == v.partname:
part._blob = ET.tostring(tree, encoding='unicode').encode('utf8')
def resolve_listing(self, xml):
def resolve_text(run_properties, paragraph_properties, m):
@ -483,6 +500,8 @@ class DocxTemplate(object):
self.render_properties(context, jinja_env)
self.render_footnotes(context, jinja_env)
# set rendered flag
self.is_rendered = True