均无用

问题描述:
把多个Word文档合并为一个,保留原来的内容以及全部格式。

方法一:使用扩展库pywin32+Word/WPS
首先使用命令pip install pywin32安装扩展库,如果仍不能使用,可以参考
ps:有的字号会变,或者字体有的这样有的那样

  1. from os.path import abspath
  2. from win32com import client
  3. def main(files, final_docx):
  4. # 启动Word应用程序
  5. word = client.gencache.EnsureDispatch('Word.Application')
  6. word.Visible = True
  7. # 新建空白文档
  8. new_document = word.Documents.Add()
  9. for fn in files:
  10. # 新增一节
  11. sec = new_document.Sections.Add()
  12. # 打开要合并的每个文件
  13. fn = abspath(fn)
  14. temp_document = word.Documents.Open(fn)
  15. # 复制其中的内容到剪切板中
  16. temp_document.Content.Copy()
  17. # 粘贴到新文件的当前节中
  18. sec.PageSetup = temp_document.PageSetup
  19. sec.Range.PasteAndFormat(True)
  20. # 关闭要合并的当前文件
  21. temp_document.Close()
  22. # 保存最终文件,关闭Word应用程序
  23. new_document.SaveAs(final_docx)
  24. new_document.Close()
  25. word.Quit()
  26. main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',
  27. r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',
  28. r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'
  29. ], r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-集合版-2021\合并.docx')

方法二:使用pywin32+Word/WPS
ps:测试为成功,出错

  1. from os.path import abspath
  2. from win32com import client
  3. def main(files, final_docx):
  4. # 启动Word应用程序
  5. word = client.gencache.EnsureDispatch('Word.Application')
  6. word.Visible = True
  7. # 新建空白文档
  8. new_document = word.Documents.Add()
  9. for fn in files:
  10. # 打开要合并的每个文件,复制其中的内容到剪贴板,然后关闭文件
  11. fn = abspath(fn)
  12. temp_document = word.Documents.Open(fn)
  13. word.Selection.WholeStory()
  14. word.Selection.Copy()
  15. temp_document.Close()
  16. #粘萜到新文档的最后
  17. new_document.Range()
  18. word.Selection.Delete()
  19. word.Selection.Paste()
  20. #保存最终文件,关闭Word应用程序
  21. new_document.SaveAs(final_docx)
  22. new_document.Close()
  23. word.Quit()
  24. main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',
  25. r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',
  26. r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'
  27. ], r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-集合版-2021\合并-方法二.docx')

方法三:使用pywin32+Word/WPS
ps:测试发现格式变量,字从宋体变为等线。。。

  1. from os.path import abspath
  2. from win32com import client
  3. def main(files, final_docx):
  4. # 启动Word应用程序
  5. word = client.gencache.EnsureDispatch('Word.Application')
  6. word.Visible = True
  7. # 新建空白文档
  8. new_document = word.Documents.Add()
  9. for fn in files[::1]:
  10. # 打开要合并的每个文件
  11. fn = abspath(fn)
  12. new_document.Application.Selection.Range.InsertFile(fn)
  13. # 保存最终文件,关闭Word应用程序
  14. new_document.SaveAs(final_docx)
  15. new_document.Close()
  16. word.Quit()
  17. main([r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究-开题报告.doc',
  18. r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的技术合同.doc',
  19. r'f:\2020\一中\一中区克拉玛依组油藏调剖适应性研究归档\报告-零散版-2019\一中区克拉玛依组油藏调剖适应性研究的成果报告.doc'
  20. ], 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)