guigrpa / docx-templates

Template-based docx report creation
MIT License
910 stars 145 forks source link

Update fields and TOC #150

Open Triskell2k1 opened 4 years ago

Triskell2k1 commented 4 years ago

When (after) creating report, it will be interresting to update all fields to have it up to date. An intermediary solution, if not possible could be to force adding in settings.xml to ask user to refresh document (because of fake link to others documents).

Thank's

jjhbw commented 4 years ago

Not sure what you mean here. Can you elaborate? Maybe provide an example of what the API would look like?

Why not re-render the original template with new data?

Triskell2k1 commented 4 years ago

Will try to provide a concrete example later, but to explain. Automatic field (Table of contents for example) which are present in template are not utomatically updated on instanciated document and get "template" value (need to update it manually after open : ctrl A + F9 shortcut when document opened in word)

jjhbw commented 4 years ago

Hmmm I think I know what you mean, but I'll await the concrete example first.

It seems you already know broadly what needs to happen. If that's the case, feel free to open a pull request.

chestacio commented 4 years ago

Any update about this? Depending of the content, my generated docx's have more or less pages than the original template so it's TOC shows outdated page numbers. How can I update (or re-render) the TOC of the generated docx's?

jjhbw commented 4 years ago

@chestacio I think you need to do this manually, see: https://contentsparks.com/38990/update-your-table-of-contents-in-word/

I'm not sure this is feasible purely by editing the document's XML, which is what we're doing. I think there's a lot of magic happening in MS Word to update the TOC.

I'm afraid this is out of scope of this library. If you have any ideas they're obviously welcome!

Triskell2k1 commented 4 years ago

Sorry, i have been overstaffed and don't take time ... So there is a (dummy) sample that generate a doc with a toc + 3 titles (1/page), but the TOC was not updated. Instanciated templage generator : https://lanumismatique.eu/docx-templates Template used : http://lanumismatique.eu/ressources/templateChap.docx

@ijhvw, I am also note sure its possible with xml, an alternative solution that I saw is by addind in settings.xml to ask user to refresh document (because of fake link to others documents).)

jjhbw commented 4 years ago

The <w:updateFields w:val="true" /> sounds interesting. Do you have a link where this is discussed?

Triskell2k1 commented 4 years ago

https://stackoverflow.com/questions/58479000/how-to-update-fields-in-ms-word-with-python-docx https://www.tinybutstrong.com/forum.php?thr=3513

Without parsing the document and "manually" update TOC in xml, I haven't find another way to do it.