Merge branch 'bartbroere-patch-1'

This commit is contained in:
Eric Lapouyade 2024-11-12 14:08:49 +01:00
commit bc92389ee1
3 changed files with 34 additions and 0 deletions

View File

@ -351,6 +351,19 @@ 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 section in self.docx.sections:
for part in section.part.package.parts:
if part.content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml':
xml = self.patch_xml(part.blob.decode('utf-8') if isinstance(part.blob, bytes) else part.blob)
xml = self.render_xml_part(xml, part, context, jinja_env)
part._blob = xml
def resolve_listing(self, xml):
def resolve_text(run_properties, paragraph_properties, m):
@ -483,6 +496,8 @@ class DocxTemplate(object):
self.render_properties(context, jinja_env)
self.render_footnotes(context, jinja_env)
# set rendered flag
self.is_rendered = True

19
tests/footnotes.py Normal file
View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
"""
Created : 2024-09-23
@author: Bart Broere
"""
from docxtpl import DocxTemplate
DEST_FILE = "output/footnotes.docx"
tpl = DocxTemplate("templates/footnotes_tpl.docx")
context = {
"a_jinja_variable": "A Jinja variable!"
}
tpl.render(context)
tpl.save(DEST_FILE)

Binary file not shown.