Skip to content

Instantly share code, notes, and snippets.

@garymanley
Created December 28, 2017 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garymanley/2a4e6530c5624dfcc8a8a44552df4487 to your computer and use it in GitHub Desktop.
Save garymanley/2a4e6530c5624dfcc8a8a44552df4487 to your computer and use it in GitHub Desktop.
Update word document contents page
import win32com.client
def update_toc(docx_file):
word = win32com.client.DispatchEx("Word.Application")
doc = word.Documents.Open(docx_file)
doc.TablesOfContents(1).Update()
doc.Close(SaveChanges=True)
word.Quit()
### End python docx as follows
#filenamedocx = r'C:\Users\garym\Documents\New folder (2)\Running_report'+datetime.datetime.today().strftime('%d%m%y')+'.docx'
#document.save(filenamedocx)
## Then run the update contents page function
update_toc(filenamedocx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment