Skip to content

Auto refresh Table of Contents using docx #1403

@latheefS

Description

@latheefS

Hi,

I am adding new content to an existing Word document and I want an approach to auto-refresh my document's Table of contents. I've used below code but it is not showing any result and the document created out of it is struck in background process for which i had to kill from task manager.

import win32com.client

def update_toc(docx_file):
word = win32com.client.DispatchEx("Word.Application")
doc = word.Documents.Open(docx_file)
toc_count = doc.TablesOfContents.Count
if toc_count == 1:
toc = doc.TablesOfContents(1)
toc.Update()
print('TOC should have been updated.')
else:
print('TOC has not been updated for sure...')

update_toc(r"C:\Users\Mydocument.docx")

Will someone help me with the details to auto refresh my word document?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions