'How to read "Table of Contents" from MS Word Document using win32com client?
Recently i have started working on python and MS word. I tried to update the Table of contents in a .docx document using win32com.client python module(pywin32).
def update_toc(docx_file):
word = win32com.client.DispatchEx("Word.Application")
doc = word.Documents.Open(docx_file)
doc.TablesOfContents(1).Update()
print(doc.TablesOfContents)
doc.Close(SaveChanges=True)
word.Quit()
Now i am trying to look how to read the updated Table of Contents using win32com.client
Q1: Please share your suggestions or references on how to read the Table of Contents.
Q2: In the above code block doc.Close(SaveChanges=True) takes around 35 secs to perform the task. Is there any performance improvement in the Close block we can achieve?
Update (inputs from @DS_London):
To read the Table of Contents
doc.TablesOfContents(1).Range()
The above code returns the Table of Contents as a single String.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
