Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sat May 10, 2025 9:45 am

hi thanks for dedicating time
i am using python to develop my program
i want to create a Multilevel List like what word it self crate to apply on style. i mean i want the thing that is in attached image
i saw link below that create multilevel list but its actually Numbering based on word paragraph features
https://www.e-iceblue.com/Tutorials/Python/Spire.Doc-for-Python/Program-Guide/List/Python-Create-Various-Types-of-Lists-in-a-Word-Document.html

i think fund sth related in link below but cant convert it to python
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Manage-Word-Headings-to-Form-a-Catalogue-in-C-and-VB.NET.html

i appreciate if you can help me to create Multilevel List for my document that apply on Headings and if later want to add a heading it determine multilevel list and add change numbers automatically
regards

XxLonely
 
Posts: 21
Joined: Sat Mar 30, 2024 5:11 pm

Mon May 12, 2025 7:06 am

Hello,

Thanks for your inquiry.
Please refer to the following python code to achieve your needs. If you have any other questions, please feel free to write back.
Code: Select all
from spire.doc import *
from spire.doc.common import *

#Create Document
document = Document()
section = document.AddSection()
paragraph = section.Paragraphs[0] if section.Paragraphs.Count > 0 else section.AddParagraph()
#Add Heading 1
paragraph = section.AddParagraph()
paragraph.AppendText("Heading1")
paragraph.ApplyStyle(BuiltinStyle.Heading1)
paragraph.ListFormat.ApplyNumberedStyle()
#Add Heading 2
paragraph = section.AddParagraph()
paragraph.AppendText("Heading2")
paragraph.ApplyStyle(BuiltinStyle.Heading2)
#List Style for Headings 2
listSty2 = ListStyle(document, ListType.Numbered)
for i in range(len(listSty2.Levels)):
    listLev = listSty2.Levels[i]
    listLev.UsePrevLevelPattern = True
    listLev.NumberPrefix = "1."
listSty2.Name = "MyStyle2"
document.ListStyles.Add(listSty2)
paragraph.ListFormat.ApplyStyle(listSty2.Name)
#Add List Style 3
listSty3 = ListStyle(document, ListType.Numbered)
for i in range(len(listSty3.Levels)):
    listLev = listSty3.Levels[i]
    listLev.UsePrevLevelPattern = True
    listLev.NumberPrefix = "1.1."
listSty3.Name = "MyStyle3"
document.ListStyles.Add(listSty3)
#Add Heading 3
for  i in range(4):
    paragraph = section.AddParagraph()
    #Append Text
    paragraph.AppendText("Heading3")
    #Apply List Style 3 for Heading 3
    paragraph.ApplyStyle(BuiltinStyle.Heading3)
    paragraph.ListFormat.ApplyStyle(listSty3.Name)
#Save and Launch
document.SaveToFile("output.docx", FileFormat.Docx)


Sincerely,
Tommy
E-iceblue support team
User avatar

Tommy.Tang
 
Posts: 85
Joined: Mon Apr 21, 2025 7:05 am

Return to Spire.Doc