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 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.
Syntax: docx.Document(String path)
- String path: It is an optional parameter. It specifies the path of the file to be open. If it is left empty then a new empty document file is created.
And to save the document we will use save() method of the docx module.
Syntax: doc.save(String path_to_document)
- String path_to_document: It is the file name by which document will be saved. You can even put the path to where you want to save it.
Example 1: Opening a new document.
Example 2: Opening a previously created document and again saving it with a different name.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course