Skip to content

Commit

Permalink
[WIP] Support rendering variables in footnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
bartbroere committed Sep 17, 2024
1 parent 0607e71 commit 0061b55
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docxtpl/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,23 @@ def render_properties(
template = jinja_env.from_string(initial)
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):

Expand Down Expand Up @@ -483,6 +500,8 @@ def render(

self.render_properties(context, jinja_env)

self.render_footnotes(context, jinja_env)

# set rendered flag
self.is_rendered = True

Expand Down

0 comments on commit 0061b55

Please sign in to comment.