Python Forum
Сombine (Merge) word documents using python-docx
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Сombine (Merge) word documents using python-docx
#1
Hello all Hand
I'm trying to combine multiple .docx files into one using python-docx.
MKA-20-5778-0-1.docx', 'MKA-20-5967-0-1.docx this is source files, where it will be taken from content.
empty.docx - blank Word sheet.
The result of merging is saved in combined_word_documents.docx. As a result, I get an empty sheet. Why?
from docx import Document
files = ['МКА-20-5778-0-1.docx', 'МКА-20-5967-0-1.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 paragraph in sub_doc.paragraphs:
            text = paragraph.text
            combined_document.add_paragraph(text)
        count += 1

    combined_document.save('combined_word_documents.docx')

combine_word_documents(files)
Reply
#2
(Apr-30-2020, 05:27 AM)Lancellot Wrote: Hello all Hand
I'm trying to combine multiple .docx files into one using python-docx.
MKA-20-5778-0-1.docx', 'MKA-20-5967-0-1.docx this is source files, where it will be taken from content.
empty.docx - blank Word sheet.
The result of merging is saved in combined_word_documents.docx. As a result, I get an empty sheet. Why?
from docx import Document
files = ['МКА-20-5778-0-1.docx', 'МКА-20-5967-0-1.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 paragraph in sub_doc.paragraphs:
            text = paragraph.text
            combined_document.add_paragraph(text)
        count += 1

    combined_document.save('combined_word_documents.docx')

combine_word_documents(files)


I think you can use docxcompose:

from docx import Document
from docxcompose.composer import Composer
   
master = Document("out.docx")
composer = Composer(master)
doc1 = Document("in.docx")
composer.append(doc1)
master.save('out.docx')  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  no module named 'docx' when importing docx MaartenRo 1 705 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Replace a text/word in docx file using Python Devan 4 2,840 Oct-17-2023, 06:03 PM
Last Post: Devan
  Word documents merging crewdk 1 811 Apr-03-2023, 06:32 AM
Last Post: buran
  python-docx: preserve formatting when printing lines Tmagpy 4 2,005 Jul-09-2022, 01:15 AM
Last Post: Tmagpy
  python-docx- change lowercase to bold, italic Tmagpy 0 1,353 Jul-01-2022, 07:25 AM
Last Post: Tmagpy
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,488 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  python-docx regex: replace any word in docx text Tmagpy 4 2,139 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Docx Convert Word Header to Body CaptainCsaba 3 2,694 Jun-02-2021, 01:25 PM
Last Post: Larz60+
  How to add run in paragraph using python-docx? toothedsword 0 2,740 May-12-2021, 10:55 AM
Last Post: toothedsword

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020