Python-docx: Merge multiple word documents?

Created on 24 Feb 2017  路  8Comments  路  Source: python-openxml/python-docx

I've been playing around with this package to see if it's possible to combine multiple word documents with a simple script. It seems like it should be possible given the objects, but I can't find an intuitive way to do this.

Basically, I have a folder with .docx files like 01.docx, 02.docx, 03.docx etc. I'd just like to join these together in a single document as if I had "copy/pasted" each one in succession.

I've tried appending the paragraphs attribute of one to another, but this doesn't seem to work, and add_paragraph only accepts a string, not a Paragraph object. Any idea on the best way to accomplish this?

ps: if this works then I am eternally grateful...this is a pretty nice package for people who are writing a dissertation, but whos advisor won't let them use Latex ;-)

Most helpful comment

I created a fork of python-docx that provides easy access to images when
r eading a DOCx file.聽 It might help you figure out what you need.
https://github.com/DKWoods/python-docx

All 8 comments

I sort-of got this working by looping through paragraph objects and running add_paragraph(p.text, p.style) over and over. This got the text (more or less) there, but all of the images were removed and table formatting was also stripped. I'm starting to sense this is asking more of this package than it was designed for...is that true ;-)

Well, maybe not more than it was designed for, but more than it's built for so far :)

You could probably get as far as getting the tables copied over with just what's available, but the images would take a bit of digging into the internals.

If you're keen to keep going, this will give you an idea how to get the tables out in the order they appear:

http://stackoverflow.com/questions/42093013/processing-objects-in-order-in-docx

Basically I'd take the same approach you mentioned. Go through the block items (paragraphs + tables) start to finish, and add a facsimile to the target document. There are other possible approaches, but I think keeping it straightforward is a good place to start. Then it's just a case of decomposing each object "copy" far enough to suit the API and your purposes.

hmmm, methinks this will be more trouble than it's worth for this one particular use-case :) I'm going to pass on this for now but I'll reopen the issue if I try doing this in the future so that I can post whatever solution I come up with. Thanks for the help!

I have a workaround solution, the following is an example to merge doc1 and doc2:

doc1 = docx.Document('1.docx')
doc2 = docx.Document('2.docx')
doc_new = docx.Document()
for element in doc1.element.body:
    doc_new.element.body.append(element)
for element in doc2.element.body:
    doc_new.element.body.append(element)
doc_new.save('new.docx')

@xlhu19
It works actually,
but header and footer has gone in the doc1 or doc2,

I would recommend

doc1 = docx.Document('1.docx')
doc2 = docx.Document('2.docx')
for element in doc2.element.body:
    doc1 .element.body.append(element)
doc1.save('new.docx')

that would be useful In case doc1 or doc2 has the same template

Those approaches won't copy the images from one document to the other. Any fix for that?

Those approaches won't copy the images from one document to the other. Any fix for that?

When I use this method I got broken word file. You the same?

I created a fork of python-docx that provides easy access to images when
r eading a DOCx file.聽 It might help you figure out what you need.
https://github.com/DKWoods/python-docx

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kndidox picture kndidox  路  5Comments

ssuzen picture ssuzen  路  6Comments

JiyhonLiu picture JiyhonLiu  路  3Comments

carlosmtz2106 picture carlosmtz2106  路  7Comments

evbo picture evbo  路  3Comments