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.
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
-


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