Table of contents Trigger Update table

In MS word there is a function to update Table of contents
It has two options Update page numbers only and Update entire table.
How to trigger each of them?

@Mark030

Can you please clarify if you are looking for a code example to update the table of contents in a Word document using Aspose.Words for Python, or are you asking for a general explanation of the functionality?

Yeah, code sample for both

@Mark030 You should simply call Document.update_fields and Document.update_page_layout method to update TOC in the document. Please see our documentation for more information:
https://docs.aspose.com/words/python-net/working-with-table-of-contents/

Just an additional Inquiry,
How to change the TOC settings to only show two levels?

@Mark030 You can configure TOC using the TOC field switches. Please see Microsoft documentation for more information:
https://support.microsoft.com/en-gb/office/field-codes-toc-table-of-contents-field-1f538bc4-60e6-4854-9f64-67754d78d05c

So what is the syntax for updating the switches in aspose to an existing TOC.
doc.UpdateTOC()?

@Mark030 You can use the following code:

doc = aw.Document("C:\\Temp\\in.docx")

# chnage TOC fields
for f in doc.range.fields:
    if f.type == aw.fields.FieldType.FIELD_TOC :
        toc = f.as_field_toc()
        toc.heading_level_range = "1-2"
        toc.update()

doc.save("C:\\Temp\\out.docx")

This does not work.
When I update toc using

doc.update_fields()
doc.update_page_layout()

It also does not update the pages in the TOC

It is just messing the Table of contents, I have to update the TOC manually in the document to fix it.

@Mark030 Could you please attach your input and output documents here for our reference? We will check the issue and provide you more information.

Is there a secure way of sending you the files?

@Mark030 It is safe to attach documents in the forum. only you as a topic owner and Aspose staff can access the attachments.

Out.docx (163.8 KB)

Input.docx (68.1 KB)

Here you go.

So if I update the output document TOC manually it fixes the issue. So wondering why it I am unable to fix it using the two syntax

@Mark030 Thank you for additional information. I cannot reproduce the problem using the following simple code:

doc = aw.Document("C:\\Temp\\in.docx")

# chnage TOC fields
for f in doc.range.fields:
    if f.type == aw.fields.FieldType.FIELD_TOC :
        toc = f.as_field_toc()
        toc.heading_level_range = "1-2"
        toc.update()

doc.update_fields()
doc.update_page_layout()

doc.save("C:\\Temp\\out.docx")

out.docx (62.2 KB)

I just used the above and same thing is happening.

doc = aw.Document("Compare\FlowChart\Input.docx")


for f in doc.range.fields:
    if f.type == aw.fields.FieldType.FIELD_TOC :
        toc = f.as_field_toc()
        toc.heading_level_range = "1-2"
        toc.update()

doc.update_fields()
doc.update_page_layout()

doc.save("Compare\FlowChart\Out.docx")

Input.docx (68.1 KB)

Out.docx (63.1 KB)

@Mark030 Which version of Aspose.Words do you use? As I can see the attached output document was not produced by Aspose.Words or it was postprocessed by some another tool.

@Mark030 I see the newly attached document has been produced by Aspose.Words for Python 24.6. I cannot reproduce the problem on my side using the latest 24.8 version of Aspose.Words. Here is the produced output:
out.docx (62.2 KB)

Do you mean this?
aspose-words 24.6.0

@Mark030 yes, you are using 24.6 version on your side. I used the latest 24.8 version on my side.