Read and Write .docx file with python

I have a folder containing several .docx files with names [Code2001.docx, Code2002.docx... Code2154.docx].

I’m trying to write a script that will:

  1. Open each .docx file
  2. Append one line to the document; “This is checked”
  3. Save the .docx-file to another folder, named “Code2001_checked”

After searching I’ve only managed to get the filename with the loop:

import os

for files in os.listdir("."):
    if files.endswith(".docx"):
        print filename

I also found this: docx module but the documentation is poor to continue.

Any suggestions on how to finish this script?

from docx import *
document = opendocx("document.doc")
body = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
body.append(paragraph('Appending this.'))

The second line may need to chance depending on where in the file you are going to append the text. To finish this, you will need to use the savedocx() function, and there is an example of its usage in the root of the project.

Leave a Comment