I'm trying to generate a Word document with data based on a template using python-docx. For the most part, I've managed to work out something. However, my document has some conditions that I am trying to fulfil, namely I need to generate certain data only in the last page.
Thing is, I don't know what the "last page" is as that depends on the amount of data added into the template, so it could be 1, 2, or 50. Python-docx, like a lot of other similar libraries like Reportlab, has no function to indicate what is the last page (at least, none that I found). So, after loads of research, I came across something like a function that can be created in Microsoft Word that I implemented in my template:
{ IF PAGE=NUMPAGES testVar }
and I have managed to make it so that testVar only displays on the last page.
Now, the issue is that I need to replace testVar with an actual calculated value through Python before the document gets generated. I previously tried:
theFooter = section.footer.tables[0]
theFooter.cell(0,2).text = 1000
(the aforementioned function is in a table in the footer in the template)
And while it replaces the testVar with 1000, the docx function in the template was also replaced. So now I'm stuck, trying to figure out how to replace testVar without removing the docx function so that my 100 value is displayed only on the last page, wherever that may be.
A crop of the template in question
(The 100 value is just a placeholder variable, corresponding to testVar)
How can I do this exactly? I have hunted through Stack Overflow, and came across some similar issues but they're ultimately different from what I need.