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?
docxfiles are not simple text files. They have a more complex and involved specification for their data. Simply concatenating that data is unlikely to produce a useful result. It sounds like your next step is to look into thatpython-docxmodule to see how you can use it to read data from your files, as well as write data to a file. Along the way you'd also have to define how exactly you want to "combine" that data.