Edit docx

This is simple script to edit the microsoft word document
This was much helpful when I was writing a report for a project.
I had to edit the figure numbers for roughly 500 images.
As usually Python came to rescue. 😄

For this you need to install docx library
pip install python-docx

In [1]:
import docx
In [2]:
doc = docx.Document('Report_020318.docx')
In [3]:
# Starting figure number
starting_num = 176
# There were total 350 images to be renamed
n = 350
In [4]:
for paragraph in doc.paragraphs:
    for i in range(n):
        if 'Fig.'+str(starting_num+i) in paragraph.text:
            paragraph.text = paragraph.text.replace('Fig.'+str(starting_num+i),'Fig.'+str(starting_num+i+100))

And save the document

In [5]:

Above code is too much hard coded, so may not be useful for you, but moral of the post is you can access the docx files text and edit and save the docx file

Some other commands may be useful

To check number of the paragraphs

In [ ]:

Each paragraph can be accessed by

In [ ]:

More details about this can be found here

Below cell is for notebook styling, you can ignore it

In [8]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))