List (1)
Python: Create Various Types of Lists in a Word Document
2023-10-27 00:57:31 Written by support iceblueLists are a powerful organizational tool that can be used to present information in a structured and easy-to-follow manner. Whether you want to create numbered lists, bulleted lists, or even custom lists with specific formatting, Word provides flexible options to suit your needs. By utilizing different list styles, you can improve the readability and visual appeal of your documents, making it simpler for readers to grasp key points and navigate through the content. In this article, you will learn how to programmatically create various types of lists in a Word document in Python using Spire.Doc for Python.
- Create a Numbered List in Word in Python
- Create a Bulleted List in Word in Python
- Create a Multi-Level Numbered List in Word in Python
- Create a Multi-Level Mixed-Type List in Word in Python
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Create a Numbered List in Word in Python
Spire.Doc for Python provides the ListStyle class, which enables you to establish either a numbered list style or a bulleted style. Subsequently, you can utilize the Paragraph.ListFormat.ApplyStyle() method to apply the defined list style to a paragraph. The steps to create a numbered list are as follows.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the numbering type through ListLevel.PatternType property.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object doc = Document() # Add a section section = doc.AddSection() # Create a numbered list style listStyle = ListStyle(doc, ListType.Numbered) listStyle.Name = "numberedList" listStyle.Levels[0].PatternType = ListPatternType.DecimalEnclosedParen listStyle.Levels[0].TextPosition = 20; doc.ListStyles.Add(listStyle) # Add a paragraph paragraph = section.AddParagraph() paragraph.AppendText("Required Web Development Skills:") paragraph.Format.AfterSpacing = 5.0 # Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph() paragraph.AppendText("HTML") paragraph.ListFormat.ApplyStyle("numberedList") paragraph.ListFormat.ListLevelNumber = 0 # Add another four paragraphs and apply the numbered list style to them paragraph = section.AddParagraph() paragraph.AppendText("CSS") paragraph.ListFormat.ApplyStyle("numberedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("JavaScript") paragraph.ListFormat.ApplyStyle("numberedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("Python") paragraph.ListFormat.ApplyStyle("numberedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("MySQL") paragraph.ListFormat.ApplyStyle("numberedList") paragraph.ListFormat.ListLevelNumber = 0 # Save the document to file doc.SaveToFile("output/NumberedList.docx", FileFormat.Docx)
Create a Bulleted List in Word in Python
Creating a bulleted list follows a similar process to creating a numbered list, with the main difference being that you need to specify the list type as "Bulleted" and assign a bullet symbol to it. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Bulleted.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the bullet symbol through ListLevel.BulletCharacter property.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object doc = Document() # Add a section section = doc.AddSection() # Create a bulleted list style listStyle = ListStyle(doc, ListType.Bulleted) listStyle.Name = "bulletedList" listStyle.Levels[0].BulletCharacter = "\u00B7" listStyle.Levels[0].CharacterFormat.FontName = "Symbol" listStyle.Levels[0].TextPosition = 20 doc.ListStyles.Add(listStyle) # Add a paragraph paragraph = section.AddParagraph() paragraph.AppendText("Computer Science Subjects:") paragraph.Format.AfterSpacing = 5.0 # Add a paragraph and apply the bulleted list style to it paragraph = section.AddParagraph() paragraph.AppendText("Data Structure") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 # Add another five paragraphs and apply the bulleted list style to them paragraph = section.AddParagraph() paragraph.AppendText("Algorithm") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("Computer Networks") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("Operating System") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("C Programming") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("Theory of Computations") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 # Save the document to file doc.SaveToFile("output/BulletedList.docx", FileFormat.Docx);
Create a Multi-Level Numbered List in Word in Python
A multi-level list consists of at least two different levels. A certain level of a nested list can be accessed by the ListStyle.Levels[index] property, through which you can set the numbering type and prefix. The following are the steps to create a multi-level numbered list in Word.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list through ListStyle.Levels[index] property, and set the numbering type and prefix.
- Add the list style to the document using Document.ListStyles.Add() method.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.ListFormat.ApplyStyle() method.
- Specify the list level through Paragraph.ListFormat.ListLevelNumber property.
- Save the document to a Word file using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object doc = Document() # Add a section section = doc.AddSection() # Create a numbered list style, specifying number prefix and pattern type of each level listStyle = ListStyle(doc, ListType.Numbered) listStyle.Name = "levelstyle" listStyle.Levels[0].PatternType = ListPatternType.Arabic listStyle.Levels[0].TextPosition = 20.0 listStyle.Levels[1].NumberPrefix = "%1." listStyle.Levels[1].PatternType = ListPatternType.Arabic listStyle.Levels[2].NumberPrefix = "%1.%2." listStyle.Levels[2].PatternType = ListPatternType.Arabic doc.ListStyles.Add(listStyle) # Add a paragraph paragraph = section.AddParagraph() paragraph.AppendText("Here's a Multi-Level Numbered List:") paragraph.Format.AfterSpacing = 5.0 # Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph() paragraph.AppendText("The first item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 0 # Add another five paragraphs and apply the numbered list stype to them paragraph = section.AddParagraph() paragraph.AppendText("The second item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("The first sub-item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 1 paragraph = section.AddParagraph() paragraph.AppendText("The second sub-item") paragraph.ListFormat.ContinueListNumbering() paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() paragraph.AppendText("A sub-sub-item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 2 paragraph = section.AddParagraph() paragraph.AppendText("The third item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 0 # Save the document to file doc.SaveToFile("output/MultilevelNumberedList.docx", FileFormat.Docx)
Create a Multi-Level Mixed-Type List in Word in Python
To combine number and symbol bullet points in a multi-level list, create separate list styles (numbered and bulleted) and apply them to different paragraphs. The detailed steps are as follows.
- Create a Document object.
- Add a section using Document.AddSection() method.
- Create a numbered list style and a bulleted list style.
- Add several paragraphs to the document using Section.AddParagraph() method.
- Apply different list style to different paragraphs using Paragraph.ListFormat.ApplyStyle() method.
- Save the document to a Word file using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object doc = Document() # Add a section section = doc.AddSection() # Create a numbered list style numberedListStyle = ListStyle(doc, ListType.Numbered) numberedListStyle.Name = "numberedStyle" numberedListStyle.Levels[0].PatternType = ListPatternType.Arabic numberedListStyle.Levels[0].TextPosition = 20 numberedListStyle.Levels[1].PatternType = ListPatternType.LowLetter doc.ListStyles.Add(numberedListStyle) # Create a bulleted list style bulletedListStyle = ListStyle(doc, ListType.Bulleted) bulletedListStyle.Name = "bulltedStyle" bulletedListStyle.Levels[2].BulletCharacter = "\u002A" bulletedListStyle.Levels[2].CharacterFormat.FontName = "Symbol" doc.ListStyles.Add(bulletedListStyle) # Add a paragraph paragraph = section.AddParagraph() paragraph.AppendText("Here's a Multi-Level Mixed List:") paragraph.Format.AfterSpacing = 5.0 # Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph() paragraph.AppendText("The first item") paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph.ListFormat.ListLevelNumber = 0 # Add the other five paragraphs and apply different list stype to them paragraph = section.AddParagraph() paragraph.AppendText("The first sub-item") paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph.ListFormat.ListLevelNumber = 1 paragraph = section.AddParagraph() paragraph.AppendText("The second sub-item") paragraph.ListFormat.ListLevelNumber = 1 paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph = section.AddParagraph() paragraph.AppendText("The first sub-sub-item") paragraph.ListFormat.ApplyStyle("bulltedStyle") paragraph.ListFormat.ListLevelNumber = 2 paragraph = section.AddParagraph() paragraph.AppendText("The second sub-sub-item") paragraph.ListFormat.ApplyStyle("bulltedStyle") paragraph.ListFormat.ListLevelNumber = 2 paragraph = section.AddParagraph() paragraph.AppendText("The second item") paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph.ListFormat.ListLevelNumber = 0 # Save the document to file doc.SaveToFile("output/MultilevelMixedList.docx", FileFormat.Docx)
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.