31 lines
945 B
Python
Raw Normal View History

2024-09-03 09:36:18 +08:00
import os
from docx import Document
from docxcompose.composer import Composer
def combine_docx(master, sub,index):
if not os.path.exists(sub): # 待合并文件必须存在
return False
if not master.endswith('.docx') or not sub.endswith('.docx'): # 主文件必须是docx格式可以不存在
return False
if os.path.exists(master):
doc_master = Document(master)
doc_master.add_page_break()
cp = Composer(doc_master)
cp.append(Document(sub))
else:
# master不存在则sub直接给master
doc_master = Document(sub)
doc_master.save(master)
return True
if __name__ == '__main__':
master = 'C:\\Users\\Administrator\\Desktop\\招标文件\\output1\\test.docx'
sub = 'C:\\Users\\Administrator\\Desktop\\招标文件\\output1\\zbfile.docx'
index = 2 # 假设你要在第二个元素位置插入
combine_docx(master, sub,index)