omri_saadon omri_saadon - 1 year ago 304
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


For example in pywin32 library I did the following:

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

docstart = d.wordDoc.Content.Start
self.word.Visible = True
docend = d.wordDoc.Content.End - 1
location = d.wordDoc.Range(docstart, docend).Copy()

But I need to do it while using Python
library instead of

Answer Source

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download