Hi Team, Currently using Aspose words python to modify docx files. As marked above, We have noticed missing tab leaders for some of the titles. We are using below code to update TOC fields.
If we manually follow below procedure, issue is getting resolved. So seems like no required information is missed at the time of TOC update.
References tab > Update table > update full table
Can you please check and advise on how to resolve this issue programmatically using python.
TOC FIELD CODE : TOC \f C \t “Heading 1,1, Schedule 1 L1,1, Schedule 1 L2,1, Standard L1,1” GUID=0c755931-16f8-4294-852d-9b7b0f5402e8
CODE:
try:
# Get all fields in the document
field_count = doc.range.fields.count
if field_count > 0:
logging.info(f"Before TOC update: Document contains > {field_count} fields.")
for i in range(field_count):
field = doc.range.fields[i]
if field:
if field.type == aw.fields.FieldType.FIELD_TOC:
field.update()
else:
logging.info("No fields found in the document.")
except Exception as e:
logging.error(f"could not update TOC of the document because of following error: {e}")
doc.update_page_layout()
@Sairam_Jyosula
Summary
When a Table of Contents (TOC) is refreshed programmatically with Aspose.Words for Python, the field text is regenerated. If the TOC paragraph style does not contain a right‑aligned tab stop with a dot leader, the regenerated entries will appear without the leader dots.
Why it happens
field.update() (or doc.update_fields()) only rebuilds the TOC entries; it does not modify the paragraph style that defines the tab stops.
- In a Word document the dot leaders are created by a right‑aligned tab stop with
TabLeader.DOTS that is part of the TOC styles (TOC, TOC 1, TOC 2, …). If those styles were edited or were missing the tab stop, the updated TOC will show plain text.
- The UI command References → Table of Contents → Update Table → Update entire table also re‑applies the default TOC style, which is why the leaders re‑appear when you do it manually.
What to do next
- Update all fields (including the TOC) and the layout.
import aspose.words as aw
doc = aw.Document("input.docx")
doc.update_fields() # rebuilds TOC text
doc.update_page_layout() # recalculates layout
doc.save("output.docx")
- Ensure the TOC style contains the dot‑leader tab stop. If you use the default TOC style or a custom one, add the required tab stop before (or after) the field update.
import aspose.words as aw
doc = aw.Document("input.docx")
# Choose the style that is applied to the TOC entries (e.g., "TOC", "TOC 1", "TOC 2")
toc_style = doc.styles["TOC"] # change name if needed
# Calculate the position of the right margin
page_setup = doc.sections[0].page_setup
tab_position = page_setup.page_width - page_setup.right_margin
# Clear existing tab stops and add a right‑aligned tab with dot leader
toc_style.paragraph_format.tab_stops.clear()
toc_style.paragraph_format.tab_stops.add(
aw.TabStop(tab_position, aw.TabAlignment.RIGHT, aw.TabLeader.DOTS)
)
# Re‑update fields and layout
doc.update_fields()
doc.update_page_layout()
doc.save("output.docx")
- If the document contains several TOC styles (e.g., “TOC 1”, “TOC 2”), repeat the style‑modification step for each of them.
for style_name in ["TOC", "TOC 1", "TOC 2", "TOC 3"]:
if style_name in doc.styles:
style = doc.styles[style_name]
style.paragraph_format.tab_stops.clear()
style.paragraph_format.tab_stops.add(
aw.TabStop(tab_position, aw.TabAlignment.RIGHT, aw.TabLeader.DOTS)
)
doc.update_fields()
doc.update_page_layout()
doc.save("output.docx")
- Validate – open the saved file in Microsoft Word and verify that the TOC entries now show the dot leaders.
Note
The above approach follows the general behavior of Word and Aspose.Words as described in the official documentation for working with TOC fields. The specific issue of missing leaders after a programmatic update is not directly covered in the retrieved evidence, so you may need to test the suggested code in your environment and adjust the style name or tab position as required.
If the problem persists, consider checking the version of Aspose.Words you are using (newer releases contain the TabStop, TabAlignment, and TabLeader classes) and consult the Aspose.Words for Python via .NET API reference for any version‑specific nuances.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
@Sairam_Jyosula Could you please attach your problematic input and output documents here for testing? We will check the issue and provide you more information.