Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rendering variables in footnotes #563

Merged
merged 8 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docxtpl/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,19 @@ def render_properties(
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('utf8'))
bartbroere marked this conversation as resolved.
Show resolved Hide resolved
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):
Expand Down Expand Up @@ -483,6 +496,8 @@ def render(

self.render_properties(context, jinja_env)

self.render_footnotes(context, jinja_env)

# set rendered flag
self.is_rendered = True

Expand Down
19 changes: 19 additions & 0 deletions tests/footnotes.py
Original file line number Diff line number Diff line change
@@ -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 added tests/templates/footnotes_tpl.docx
Binary file not shown.