Join Word Office Documents with docx

We can easily join more file docx with Python and python-docx module. We have some difficulties with files containing images. In these cases we should transform the files in pdf and the join them in one pdf. In the last post we showed that code, but we will come back to this soon.

1. Import docx 2. Import glob 3. Create an istance of Document 4. Iterate the docx files in the folder to be joined 5. Iterate the element.body of each istance of Document(files) 6. Append the element.body of each Document in the first istance (line 3) 7. Save the file

from docx import Document # pip install python-docx
import glob

doc = Document()
for f in glob.glob("*.docx"):
	for e in Document(f).element.body:
		doc.element.body.append(e)

doc.save("Full.docx")

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.