Skip to content
Related Articles
Open in App
Not now

Related Articles

Working with Documents – Python .docx Module

Improve Article
Save Article
  • Last Updated : 03 Jan, 2021
Improve Article
Save Article

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)

Parameter:

  • 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)

Parameter:

  • 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.

Python3




# Import docx NOT python-docx
import docx
  
# Create an instance of a word document
doc = docx.Document()
  
# Now save the document to a location 
doc.save('gfg.docx')

Output:

Example 2: Opening a previously created document and again saving it with a different name.

Python3




# Import docx NOT python-docx
import docx
  
# Opening a previously created document
doc = docx.Document('gfg.docx')
  
# Now save the document to a location 
doc.save('gfg-copy.docx')

Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!