Friday, December 27, 2024
Google search engine
HomeLanguagesWorking with Lists – Python .docx Module

Working with Lists – Python .docx Module

Prerequisite: Working with .docx module

Word documents contain formatted text wrapped within three object levels. The Lowest level- run objects, middle level- paragraph objects, and highest level- document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module. Pip command to install this module is:

pip install python-docx

Python docx module allows users to manipulate docs by either manipulating the existing one or creating a new empty document and manipulating it. It is a powerful tool as it helps you to manipulate the document to a very large extent. There are two types of lists:

  • Ordered List
  • Unordered List

To add an ordered/unordered list in a Word document there are styles available in the .add_paragraph() method of the document object.

Syntax: doc.add_paragraph(String s, style=None)

Parameters:

  • String s: It is the string data that is to be added as a paragraph. This string can contain newline character ‘\n’, tabs ‘\t’ or a carriage return character ‘\r’.
  • Style: It is used to set style.

Ordered List

Styles to add an ordered list are:

Sr. No.

Style Name

Description

1.

List Number

It adds an ordered list in the word document.

2.

List Number 2

It adds an ordered list with a single tab indentation in the word document.

3.

List Number 3

It adds an ordered list with double tab indentation in the word document.

Note: Each list-point is considered as a paragraph in a list so you have to add each point as a new paragraph with the same style name.

 

Example 1: Adding an ordered list in the Word document.

Python3




# Import docx NOT python-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding list of style name 'List Number'
doc.add_heading('Style: List Number', 3)
# Adding points to the list named 'List Number'
doc.add_paragraph('The first item in an ordered list.',
                  style='List Number')
  
doc.add_paragraph('The second item in an ordered list.'
                  style='List Number')
  
doc.add_paragraph('The third item in an ordered list.'
                  style='List Number')
  
# Adding list of style name 'List Number 2'
doc.add_heading('Style: List Number 2', 3)
# Adding points to the list named 'List Number 2'
doc.add_paragraph('The first item in an ordered list.',
                  style='List Number 2')
  
doc.add_paragraph('The second item in an ordered list.',
                  style='List Number 2')
  
doc.add_paragraph('The third item in an ordered list.'
                  style='List Number 2')
  
# Adding list of style name 'List Number 3'
doc.add_heading('Style: List Number 3', 3)
# Adding points to the list named 'List Number 3'
doc.add_paragraph('The first item in an ordered list.',
                  style='List Number 3')
  
doc.add_paragraph('The second item in an ordered list.',
                  style='List Number 3')
  
doc.add_paragraph('The third item in an ordered list.',
                  style='List Number 3')
  
# Now save the document to a location 
doc.save('gfg.docx')


Output:

Document gfg.docx

Unordered List

Styles to add an unordered list are:

Sr. No.

Style Name

Description

1.

List Bullet

It adds an unordered list in the word document.

2.

List Bullet 2

It adds an unordered list with a single tab indentation in the word document.

3.

List Bullet 3

It adds an unordered list with a double tab indentation in the word document.

Note: Each list-point is considered as a paragraph in a list so you have to add each point as a new paragraph with the same style name.

 

Example 2: Adding an unordered list in the Word document.

Python3




# Import docx NOT python-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Add a Title to the document 
doc.add_heading('GeeksForGeeks', 0)
  
# Adding list of style name 'List Bullet'
doc.add_heading('Style: List Bullet', 3)
# Adding points to the list named 'List Number'
doc.add_paragraph('The first item in an unordered list.'
                  style='List Bullet')
  
doc.add_paragraph('The second item in an unordered list.',
                  style='List Bullet')
  
doc.add_paragraph('The third item in an unordered list.',
                  style='List Bullet')
  
# Adding list of style name 'List Bullet 2'
doc.add_heading('Style: List Bullet 2', 3)
# Adding points to the list named 'List Number'
doc.add_paragraph('The first item in an unordered list.',
                  style='List Bullet 2')
  
doc.add_paragraph('The second item in an unordered list.',
                  style='List Bullet 2')
  
doc.add_paragraph('The third item in an unordered list.'
                  style='List Bullet 2')
  
# Adding list of style name 'List Bullet 3'
doc.add_heading('Style: List Bullet 3', 3)
# Adding points to the list named 'List Number'
doc.add_paragraph('The first item in an unordered list.',
                  style='List Bullet 3')
  
doc.add_paragraph('The second item in an unordered list.',
                  style='List Bullet 3')
  
doc.add_paragraph('The third item in an unordered list.',
                  style='List Bullet 3')
  
# Now save the document to a location 
doc.save('gfg.docx')


Output:

Document gfg.docx

RELATED ARTICLES

Most Popular

Recent Comments