omri_saadon omri_saadon - 2 months ago 38
Python Question

combine word document using python docx

I have few word files that each have specific content. I would like for a snippet that show me or help me to figure out how to combine the word files into one file, while using Python

docx
library.

For example in pywin32 library I did the following:

rng = self.doc.Range(0, 0)
for d in data:
time.sleep(0.05)

docstart = d.wordDoc.Content.Start
self.word.Visible = True
docend = d.wordDoc.Content.End - 1
location = d.wordDoc.Range(docstart, docend).Copy()
rng.Paste()
rng.Collapse(0)
rng.InsertBreak(win32.constants.wdPageBreak)


But I need to do it while using Python
docx
library instead of
win32.client

Answer

If your needs are simple, something like this might work:

source_document = Document('source.docx')
target_document = Document()

for paragraph in source_document.paragraphs:
    text = paragraph.text
    target_document.add_paragraph(text)

There are additional things you can do, but that should get you started.

It turns out that copying content from one Word file to another is quite complex in the general case, involving things like reconciling styles present in the source document that may be conflicting in the target document for example. So it's not a feature we're likely to be adding in the next year, say.