TOC Font size change

I am try to change font size of a Table of contents with this. But it is not working.

for field in doc.range.fields :
                    
    if (field.type == aw.fields.FieldType.FIELD_HYPERLINK) :
                        
        hyperlink = field.as_field_hyperlink()
        if (hyperlink.sub_address != None and hyperlink.sub_address.find("_Toc") == 0) :
            try:   

                tocItem = field.start.get_ancestor(aw.NodeType.PARAGRAPH).as_paragraph()  
                # print("runs",tocItem.runs)
                for run in tocItem.runs:
                    run = run.as_run()
                    if run.font.bold == True:
                        run.font.bold == True
                    run.font.size = 11

@Mark030

To change the font size of the entries in a Table of Contents (TOC) using Aspose.Words for Python, you should modify the TOC styles rather than directly changing the font size of the runs in the TOC entries. Each level of the TOC is formatted using specific styles (TOC1, TOC2, etc.), and you can adjust these styles to achieve the desired font size.

Here’s how you can do it:

  1. Access the styles for the TOC levels.
  2. Modify the font size for the desired TOC style.

Here’s a code example demonstrating how to change the font size of the first level of the TOC:

import aspose.words as aw

# Load your document
doc = aw.Document("your_document.docx")

# Access the style for the first level of the TOC
toc1_style = doc.styles[aw.StyleIdentifier.TOC1]

# Change the font size
toc1_style.font.size = 11  # Set the desired font size

# Update the TOC to reflect changes
doc.update_fields()

# Save the modified document
doc.save("modified_document.docx")

In this example, we first load the document and then access the TOC1 style, which corresponds to the first level of the TOC. We set the font size to 11 points and then update the fields in the document to ensure the changes are applied. Finally, we save the modified document.

Make sure to adjust the style identifier (TOC1, TOC2, etc.) based on which level of the TOC you want to modify. This approach ensures that all entries formatted with that style will reflect the new font size.

If you have any further questions or need additional assistance, feel free to ask!

Thanks, This works.

Just an additional inquiry, How do you update the numbering of a TOC. For example I want to change it to BOLD

@Mark030 I am afraid there is no direct way to change page number formatting separately from TOC item text. The only way is to change font of PAGEREF field inside the TOC. but after the next update page numbers formatting will be reset again:

doc = aw.Document("C:\\Temp\\in.docx")

toc1 = doc.styles.get_by_style_identifier(aw.StyleIdentifier.TOC1)
toc1.font.color = pydraw.Color.blue

toc2 = doc.styles.get_by_style_identifier(aw.StyleIdentifier.TOC2)
toc2.font.color = pydraw.Color.yellow

doc.update_fields()

# Make page ref fields bold.
for f in doc.range.fields:
    if f.type == aw.fields.FieldType.FIELD_PAGE_REF:
        current_node = f.start
        while current_node != None and current_node != f.end :
            if current_node.node_type == aw.NodeType.RUN:
                current_node.as_run().font.bold = True
            current_node = current_node.next_sibling

doc.save("C:\\Temp\\out.docx")