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.
TOC issue document_INPUT.docx (81.2 KB)
TOC_issue_document_OUTPUT.docx (71.8 KB)
here are input and output files. We have tried suggested code with upgraded aspose version, but still facing same issue.
@Sairam_Jyosula
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28835
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Can you please share update on this ticket.
@Sairam_Jyosula The issue is currently in the queue for analysis. We will keep you updated and let you know once the issue is resolved or we have more information for you.
Hi Team, Can you share update on this issue ?. I see ticket status as closed, but i do not see any solution.
@Sairam_Jyosula The issue is already resolved in the current codebase. The fix will be included into the next 26.1 version of Aspose.Words. We will keep you updated and let you know once it is published.
Thanks for the confirmation. As 25.12 version is yet to be released and release of 26.1 will take more time, Can you share updated code to resolve this issue ? or can this be rolled out as part of 25.12 version ?
@Sairam_Jyosula Unfortunately, release preparations of 25.12 version has been already started and adding this fix to 25.12 version is not currently possible. Please accept our apologies for your inconvenience.
Can you share updated code to resolve this issue ?
@Sairam_Jyosula It is not quite clear what you mean. The issue has been fixed in the internal Aspose.Words code. There is no way to fix the problem using public Aspose.Words API. After the fix is available no code changes will be required on your side.
Can you confirm 26.1 release date?
@Sairam_Jyosula There is no fixed release date. Usually new version is published in the first decade of each month. Though there might be slight shift due to Christmas and New Year holydays.
thanks for the confirmation. To progress on our current task, we need piece of code to resolve this issue ASAP. As we are waiting for resolution of this issue since 25 days and release of 26.1 will take more time, Can you check and confirm whether is it possible to solve this issue intermittently with 25.11 or 25.12 version code (with additional steps). Once 26.1 is released, we will upgrade our version.
@Sairam_Jyosula Unfortunately, I did not manage to find a workaround of this issue using the current version of Aspose.Words. So you should wait for the actual fix. Please accept our apologies for your inconvenience.
The issues you have found earlier (filed as WORDSNET-28835) have been fixed in this Aspose.Words for .NET 26.1 update also available on NuGet.