2

I want to combine multiple docx file and save it in another docx file. I not only want to copy all the text, but also it's formatting(runs). eg. bold, italics, underline, bullets, etc.

I am Groot
  • 43
  • 4
  • I think that you might be able to find some inspiration from this question: https://stackoverflow.com/questions/24872527/combine-word-document-using-python-docx – Marcus May 25 '21 at 14:16

1 Answers1

0

If you need to copy the contents of just one docx file to another, you can use this

from docx import Document
from docxcompose.composer import Composer

# main docx file
master = Document(r"path\of\main.docx")
composer = Composer(master)
# doc1 is the docx file getting copied
doc1 = Document(r"file\to\be\copied.docx")
composer.append(doc1)
composer.save(r"path\of\combined.docx")

If you have multiple docx files to be copied, you can try like this

def copy_docx(main_docx, docx_list):
    master = Document(main_docx)
    composer = Composer(master)
    for index, file in enumerate(docx_list):
        file = Document(file)
        composer.append(file)
    composer.save(r"path\of\combined.docx")


main_docx = "path\of\main.docx"
docx_list = [r"path\of\docx1.docx",
             r"path\of\docx2.docx",
             r"path\of\docx3.docx"]

copy_docx(main_docx, docx_list)