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 us
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)