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 extend.
Now, to use the python-docx module you have to import it as docx.
# Import docx NOT python-docx import docx
Then to create an instance of the word document. We will use the Document() method of the docx module.
And to save the document we will use save() method of the docx module.
Example 1: Opening a new document.