Add a test and use existing XML patching method
This commit is contained in:
parent
a449f01f36
commit
1cca257016
@ -357,13 +357,12 @@ class DocxTemplate(object):
|
||||
if jinja_env is None:
|
||||
jinja_env = Environment()
|
||||
|
||||
for part in self.docx.sections[0].part.package.parts:
|
||||
if part.content_type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml':
|
||||
tree = etree.fromstring(part.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)
|
||||
part._blob = etree.tostring(tree)
|
||||
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'))
|
||||
xml = self.render_xml_part(xml, part, context, jinja_env)
|
||||
part._blob = xml
|
||||
|
||||
def resolve_listing(self, xml):
|
||||
|
||||
|
||||
19
tests/footnotes.py
Normal file
19
tests/footnotes.py
Normal 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)
|
||||
BIN
tests/templates/footnotes_tpl.docx
Normal file
BIN
tests/templates/footnotes_tpl.docx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user