31 lines
945 B
Python
31 lines
945 B
Python
|
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)
|