Python Forum
Verify if .docx table of contents is updated as per document sections or not
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Verify if .docx table of contents is updated as per document sections or not
#1
Problem Statement: Verify if the Microsoft docx word document Table of Content is updated according to all Headings and sections available in the document and display final output as TOC updated or not updated.

Solution: I was searching through docx package handling of TOC but as per internet "python-docx" package lacks the possibility to verify if TOC (table of content) is updated or not.
I found below code on the internet which just updates TOC using win32com. The problem is, I am trying to find out if I can verify whether the TOC is updated or not before using 'doc.TablesOfContents(1).Update()' code.

Request you to guide.

import win32com.client
import inspect, os

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()

def main():
    script_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    file_name = 'doc_with_toc.docx'
    file_path = os.path.join(script_dir, file_name)
    update_toc(file_path)

if __name__ == "__main__":
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 712 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  How to verify widening conversion? quazirfan 1 601 Apr-08-2023, 07:04 AM
Last Post: Gribouillis
  Mysql Workbench table not updated CatBall 2 1,045 Feb-13-2023, 05:37 PM
Last Post: CatBall
  Use module docx to get text from a file with a table Pedroski55 8 5,802 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  python-docx regex: replace any word in docx text Tmagpy 4 2,139 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Remove Blank Lines from docx table and paragraphs bsudhirk001 1 3,619 Feb-14-2021, 12:38 AM
Last Post: Larz60+
  live updated json file Nyanpasu 2 2,200 Aug-22-2020, 04:41 PM
Last Post: Nyanpasu
  Verify input as integer and not blank GMCobraz 3 2,085 Jun-22-2020, 02:47 PM
Last Post: pyzyx3qwerty
  Multiple items of a matrix has been updated. Yoki91 4 2,246 May-19-2020, 06:29 PM
Last Post: Yoki91
  How to verify the give number is valid Mekala 3 2,356 May-16-2020, 02:40 PM
Last Post: anbu23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020