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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.