用到的几个模块,pip install一下
import os
from docx import Document
from docxcompose.composer import Composer
road_docx = r'E:\zhuomian20201023\yuanshi'/原始文件夹
road_all = r'E:\zhuomian20201023\hebinhou'/合并后的文件夹
original_docx_path = road_docx
new_docx_path = f'{road_all}/activity_name.docx'
all_file_path = []
for file_name in os.listdir(original_docx_path):
all_file_path.append(f'{original_docx_path}\{file_name}')
print (all_file_path[0])
print (all_file_path[1])
print (all_file_path[2])
first_document = Document(all_file_path[0])
first_document.add_page_break()
middle_new_docx = Composer(first_document)
for index, word in enumerate(all_file_path[1:]):
try:
word_document = Document(word)
if index != len(all_file_path) - 2:
word_document.add_page_break()
middle_new_docx.append(word_document)
middle_new_docx.save(new_docx_path)
except:
pass