Skip to content

Commit 1cca257

Browse files
committed
Add a test and use existing XML patching method
1 parent a449f01 commit 1cca257

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

docxtpl/template.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,12 @@ def render_footnotes(
357357
if jinja_env is None:
358358
jinja_env = Environment()
359359

360-
for part in self.docx.sections[0].part.package.parts:
361-
if part.content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml':
362-
tree = etree.fromstring(part.blob)
363-
for footnote in tree.findall('.//w:t', docx.oxml.ns.nsmap):
364-
if hasattr(footnote, 'text'):
365-
footnote.text = jinja_env.from_string(footnote.text).render(context)
366-
part._blob = etree.tostring(tree)
360+
for section in self.docx.sections:
361+
for part in section.part.package.parts:
362+
if part.content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml':
363+
xml = self.patch_xml(part.blob.decode('utf8'))
364+
xml = self.render_xml_part(xml, part, context, jinja_env)
365+
part._blob = xml
367366

368367
def resolve_listing(self, xml):
369368

tests/footnotes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created : 2024-09-23
4+
5+
@author: Bart Broere
6+
"""
7+
8+
from docxtpl import DocxTemplate
9+
10+
DEST_FILE = "output/footnotes.docx"
11+
12+
tpl = DocxTemplate("templates/footnotes_tpl.docx")
13+
14+
context = {
15+
"a_jinja_variable": "A Jinja variable!"
16+
}
17+
18+
tpl.render(context)
19+
tpl.save(DEST_FILE)

tests/templates/footnotes_tpl.docx

15.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)