均无用
问题描述:
把多个Word文档合并为一个,保留原来的内容以及全部格式。
方法一:使用扩展库pywin32+Word/WPS
首先使用命令pip install pywin32安装扩展库,如果仍不能使用,可以参考
ps:有的字号会变,或者字体有的这样有的那样
from os.path import abspathfrom win32com import clientdef main(files, final_docx):# 启动Word应用程序word = client.gencache.EnsureDispatch('Word.Application')word.Visible = True# 新建空白文档new_document = word.Documents.Add()for fn in files:# 新增一节sec = new_document.Sections.Add()# 打开要合并的每个文件fn = abspath(fn)temp_document = word.Documents.Open(fn)# 复制其中的内容到剪切板中temp_document.Content.Copy()# 粘贴到新文件的当前节中sec.PageSetup = temp_document.PageSetupsec.Range.PasteAndFormat(True)# 关闭要合并的当前文件temp_document.Close()# 保存最终文件,关闭Word应用程序new_document.SaveAs(final_docx)new_document.Close()word.Quit()main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'], r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-集合版-2021\合并.docx')
方法二:使用pywin32+Word/WPS
ps:测试为成功,出错
from os.path import abspathfrom win32com import clientdef main(files, final_docx):# 启动Word应用程序word = client.gencache.EnsureDispatch('Word.Application')word.Visible = True# 新建空白文档new_document = word.Documents.Add()for fn in files:# 打开要合并的每个文件,复制其中的内容到剪贴板,然后关闭文件fn = abspath(fn)temp_document = word.Documents.Open(fn)word.Selection.WholeStory()word.Selection.Copy()temp_document.Close()#粘萜到新文档的最后new_document.Range()word.Selection.Delete()word.Selection.Paste()#保存最终文件,关闭Word应用程序new_document.SaveAs(final_docx)new_document.Close()word.Quit()main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'], r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-集合版-2021\合并-方法二.docx')
方法三:使用pywin32+Word/WPS
ps:测试发现格式变量,字从宋体变为等线。。。
from os.path import abspathfrom win32com import clientdef main(files, final_docx):# 启动Word应用程序word = client.gencache.EnsureDispatch('Word.Application')word.Visible = True# 新建空白文档new_document = word.Documents.Add()for fn in files[::1]:# 打开要合并的每个文件fn = abspath(fn)new_document.Application.Selection.Range.InsertFile(fn)# 保存最终文件,关闭Word应用程序new_document.SaveAs(final_docx)new_document.Close()word.Quit()main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'], r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-集合版-2021\合并.docx')
方法四: 使用python-docx扩展库和docxcompose扩展库
ps:测试无用,格式变
from docx import Document
from docxcompose.composer import Composer
# import docx
def main(files, final_docx):
# 新建空白文档
new_document = Document()
composer = Composer(new_document)
for fn in files:
composer.append(Document(fn))
Composer.save(final_docx)
main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',
r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',
r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'
], r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-集合版-2021\合并.docx')
# from docx import Document
# files = [r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',
# r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',
# r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'
# ]
# def combine_word_documents(files):
# merged_document = Document()
# for index, fn in enumerate(files):
# sub_doc = Document(fn)
# # Don't add a page break if you've reached the last file.
# # if index < len(files)-1:
# # sub_doc.add_page_break()
# for element in sub_doc.element.body:
# merged_document.element.body.append(element)
# merged_document.save('merged.docx')
# combine_word_documents(files)
