[Python] How to combine multiple docx files into a single in python

Discussão em 'Python' iniciado por Stack, Fevereiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I combined multiple text files into a single text file using simple code:

    with open("Combined_file.txt", 'w') as f1:
    for indx1, fil1 in enumerate(files_to_combine):
    with open(files_to_combine[indx1], 'r') as f2:
    for line1 in f2:
    f1.write(line1)
    f1.write("\n")


    files_to_combine is a list containing files to combine into a single file Combined_file.txt. I want to combine MS Word .docx files similar to above and looked at this answer https://stackoverflow.com/a/48925828 using python-docx module. But I couldn't figure out how to open and save a docx file under top for loop of above code, since with open construct won't work here. Also, if the source docx file contains an image, can it be copied using above code and the answer code?

    Continue reading...

Compartilhe esta Página