combine word document using python docx
Create an empty document (empty.docx) and add your two documents to this. On each loop of the iteration over the files, add a page break if necessary.
On completion save the new file that contains your two combined files.
from docx import Document
files = ['file1.docx', 'file2.docx']
def combine_word_documents(files):
combined_document = Document('empty.docx')
count, number_of_files = 0, len(files)
for file in files:
sub_doc = Document(file)
# Don't add a page break if you've
# reached the last file.
if count < number_of_files - 1:
sub_doc.add_page_break()
for element in sub_doc._document_part.body._element:
combined_document._document_part.body._element.append(element)
count += 1
combined_document.save('combined_word_documents.docx')
combine_word_documents(files)
The alternative approach to merge two documents including all the styles is to use python library docxcompose ( https://pypi.org/project/docxcompose/) . We do not need to explicitly define the styling and we do not have to read the document paragraph by paragraph and append it to the master document. The usage of the python docxcompose is shown in the below code
#Importing the required packages
from docxcompose.composer import Composer
from docx import Document as Document_compose
#filename_master is name of the file you want to merge the docx file into
master = Document_compose(filename_master)
composer = Composer(master)
#filename_second_docx is the name of the second docx file
doc2 = Document_compose(filename_second_docx)
#append the doc2 into the master using composer.append function
composer.append(doc2)
#Save the combined docx with a name
composer.save("combined.docx")
If you want to merge multiple documents into one docx file you can use the below function
#Filename_master is the name of the file you want to merge all the document into
#files_list is a list containing all the filename of the docx file to be merged
def combine_all_docx(filename_master,files_list):
number_of_sections=len(files_list)
master = Document_compose(filename_master)
composer = Composer(master)
for i in range(0, number_of_sections):
doc_temp = Document_compose(files_list[i])
composer.append(doc_temp)
composer.save("combined_file.docx")
#For Example
#filename_master="file1.docx"
#files_list=["file2.docx","file3.docx","file4.docx",file5.docx"]
#Calling the function
#combine_all_docx(filename_master,files_list)
#This function will combine all the document in the array files_list into the file1.docx and save the merged document into combined_file.docx
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.
I've adjusted the example above to work with the latest version of python-docx (0.8.6 at the time of writing). Note that this just copies the elements (merging styles of elements is more complicated to do):
from docx import Document
files = ['file1.docx', 'file2.docx']
def combine_word_documents(files):
merged_document = Document()
for index, file in enumerate(files):
sub_doc = Document(file)
# Don't add a page break if you've reached the last file.
if index < len(files)-1:
sub_doc.add_page_break()
for element in sub_doc.element.body:
merged_document.element.body.append(element)
merged_document.save('merged.docx')
combine_word_documents(files)