docx-templates icon indicating copy to clipboard operation
docx-templates copied to clipboard

Update fields and TOC

Open Triskell2k1 opened this issue 4 years ago • 9 comments

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 <w:updateFields w:val="true" /> in settings.xml to ask user to refresh document (because of fake link to others documents).

Thank's

Triskell2k1 avatar Sep 09 '20 13:09 Triskell2k1

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?

jjhbw avatar Sep 09 '20 13:09 jjhbw

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)

Triskell2k1 avatar Sep 09 '20 15:09 Triskell2k1

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.

jjhbw avatar Sep 10 '20 10:09 jjhbw

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?

chestacio avatar Oct 05 '20 06:10 chestacio

@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!

jjhbw avatar Oct 05 '20 07:10 jjhbw

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 <w:updateFields w:val="true" /> in settings.xml to ask user to refresh document (because of fake link to others documents).)

Triskell2k1 avatar Oct 05 '20 07:10 Triskell2k1

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

jjhbw avatar Oct 05 '20 07:10 jjhbw

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.

Triskell2k1 avatar Oct 05 '20 08:10 Triskell2k1