'accessing/modifying Table of contents of a docx through python

I need to change the font style of TOC content in the MS-Word document. This I want to do automatically through a python script.

I am able to access the TOC table, I can iterate over that as well .. but not able to access the font and size of the contents. My code is written in Python. Can someone please help. I want to change the font/size to a particular font / size.

Please help on how to do this : Here is my code :

enter image description here

from pydoc import doc
from docx.api import Document
from docx import Document
from docx.shared import Pt
from docx.shared import Inches
from datetime import date
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml import OxmlElement, ns
from docx.oxml.shared import OxmlElement # Necessary Import
from docx.oxml.shared import qn
from docx.oxml.ns import qn
from docx.text.paragraph import Paragraph
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE
from docx2python import docx2python
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from pathlib import Path
import diff_match_patch
from docx.enum.text import WD_BREAK
from docx.text.run import Run
from docx.oxml.text.run import CT_R
from docx.enum.style import WD_STYLE
import re
import mmap
import win32com.client
import inspect, os
import calendar
import tkinter as tk
from tkinter import filedialog
import time
import lxml
from calendar import month_name
from docx.enum.text import WD_COLOR_INDEX
from docx.enum.text import WD_LINE_SPACING
from docx.shared import RGBColor
import docx

root = tk.Tk()
root.withdraw()

file = filedialog.askopenfilename()
filepath = os.path.dirname(os.path.abspath(file))
document = Document(file)

def tocM():
    body_elements = document._body._body
    rs = body_elements.xpath('.//w:r')
    table_of_content = [r for r in rs if r.style == "Hyperlink"]
    for i in table_of_content:
        print("1111");
        print(i.text)
        print(i)
        print("2222");
    return;


tocM()

I have attached the snapshot of the docx file on which I am working. Please help.

Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source