2024-09-03 09:36:18 +08:00

31 lines
945 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)