Table of contents doesn't update page numbers correctly

I have a document I’m opening and trying to update the TOC in, but it seems like the page numbers of the table of contents don’t get updated correctly… Am I doing something wrong? I’ve tried reordering the update_ function calls, and duplicating them too.

aw_doc = aw.Document(document_bytesio)
aw_doc.update_page_layout()
aw_doc.update_table_layout()
aw_doc.update_list_labels()
aw_doc.update_fields()

with tempfile.NamedTemporaryFile(
    mode="w+b", prefix="risk_report_", suffix=".docx"
) as temp:
    temp_filename = temp.name
    aw_doc.save(temp_filename)
    temp.seek(0)
    final_document = temp.read()

The page numbers seem to overshoot somehow, so I don’t think it’s the TOC pushing the page contents up and not updating accordingly.

I’m unable to share the document as it contains sensitive data, however I can say updating the TOC manually in Word after it’s been edited by aspose.words for python does indeed correctly update the page numbers.

@ichicoro MS Word documents are flow by their nature, so they does not have any information about document layout. Consumer applications, like MS Word or OpenOffice, reflows the document’s content into pages on the fly. Aspose.Words has it’s own layout engine for this.
The problem on your side might occur because fonts required for building document layout are not available in your environment. To build an accurate document layout the fonts are required. If Aspose.Words cannot find the fonts used in the document the fonts are substituted . This might lead into the document layout difference due to difference in font metrics and as a result incorrect page numbers in the TOC.
Please see our documentation to learn where Aspose.Words looks for fonts:
https://docs.aspose.com/words/python-net/specifying-truetype-fonts-location/

Also, in your code you call aw_doc.update_table_layout(). This method is deprecated and is not recommended to be used unless you are sure in positive effect. The method was the first attempt of table grid calculation algorithm implementation. Currently another implementation is used internally by Aspose.Words, so there is no need to call additional methods to calculate table layout. Calling aw_doc.update_table_layout() method might have negative effect.

Calling aw_doc.update_list_labels() method is also not required unless you need to access list labels using Paragraph.list_lebel property.

If after providing the required fonts and removing aw_doc.update_table_layout() call the problem is still there, please attach the problematic document here for testing. We will check the issue and provide you more information.