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.
Style Name Description
No.
1. List Number It adds an ordered list in the word document.
List Number It adds an ordered list with a single tab indentation in the
2.
2 word document.
List Number It adds an ordered list with double tab indentation in the word
3.
3 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.