当前位置: 首页 > article >正文

文档整合自动化

主要功能是按照JSON文件(Sort.json)中指定的顺序合并多个Word文档(.docx),并清除文档中的所有超链接。最终输出合并后的文档名为"sorted_按章节顺序.docx"。

主要分为几个部分:

初始化配置
  1. 定义超链接清除函数(处理段落+表格)
  2. 获取当前工作目录
读取排序规则
  1. 解析Sort.json文件
  2. 构建完整文件路径列表
文件验证
  1. 检查JSON中所有文件是否存在
  2. 输出缺失文件警告

主流程:读取JSON,验证文件,合并文档

环境配置步骤:

安装好 Python 和成功配置相应的环境变量,我的 Python 版本为 3.8.2
需要安装 win32com、docx、docxcompose,分别输入以下代码安装

pip install pypiwin32
pip install python-docx
pip install docxcompose
💡 提示:安装后可通过 python -c "import win32com; print('成功')" 验证

代码简略版:

!/usr/bin/python3.6
# -*- coding: utf-8 -*-
"""
@Time    :24-12 10:07
@Software: PyCharm
@Project :Merge files001
"""
import os
import json
from docx import Document
from docxcompose.composer import Composer# 清除文档中的所有超链接
def remove_hyperlinks(doc):for para in doc.paragraphs:for run in para.runs:# 通过run的XML属性查看是否为超链接if 'hyperlink' in run._r.xml:run._r.getparent().remove(run._r) for table in doc.tables:for row in table.rows:for cell in row.cells:for para in cell.paragraphs:for run in para.runs:if 'hyperlink' in run._r.xml:run._r.getparent().remove(run._r) 
# 获取当前工作目录( cwd )
cwd = os.getcwd()
# 读取JSON文件并获取排序信息
def get_order_from_json(json_path):# 读取 JSON 文件,获取文件列表with open(json_path, 'r', encoding='utf-8') as f:data = json.load(f)return [os.path.join(cwd, 'Word_Test', file_name) for file_name in data['file_order']]
# 调用函数读取排序信息
json_path = 'Sort.json'  #JSON文件路径
ordered_files = get_order_from_json(json_path)# 使用排序后的文件列表进行合并
def combine_all_docx_ordered(filename_master, files_list_ordered):# 确保文件列表不为空if not files_list_ordered:print("没有文档可供合并。")returntry:master = Document(filename_master) remove_hyperlinks(master)  except Exception as e:print(f"无法打开主文档{filename_master}{e}")return# 在循环之前添加一个分页符,合并后的文档从第二页开始。master.add_page_break()  composer = Composer(master)# 如果文件列表中只有一个文件,即主文档自身,直接保存即可if len(files_list_ordered) == 1:print("只有一个文档,无需合并。")master.save("single_doc.docx")returnfor doc_temp_path in files_list_ordered[1:]:  try:doc_temp = Document(doc_temp_path)  remove_hyperlinks(doc_temp) except Exception as e:print(f"无法打开文档 {doc_temp_path}:{e}")continuedoc_temp.add_page_break() composer.append(doc_temp) # 保存合并后的文档try:composer.save("sorted_按章节顺序.docx")print("合并后的文档已保存。")except Exception as e:print(f"保存合并文档时出错: {e}")# 验证JSON中的文件是否存在
def verify_files_existence(files_paths):existing_files = []missing_files = []for file_path in files_paths:if os.path.exists(file_path):existing_files.append(file_path)else:missing_files.append(file_path)return existing_files, missing_files
# 验证文件并处理不存在的文件
existing_files, missing_files = verify_files_existence(ordered_files)if missing_files:print("以下文件在JSON中指定但未找到:")for missing_file in missing_files:print(missing_file)
else:# 合并文档# 调用新的函数进行合并combine_all_docx_ordered(ordered_files[0], ordered_files)print("————按JSON排序合并完成————")

创建 Sort.json 文件,其中按自定义顺序存储需合并的 Word 文档名称。
运行脚本后,程序将按 JSON 定义的顺序自动合并文档。

http://www.lryc.cn/news/2397747.html

相关文章:

  • 微软markitdown PDF/WORD/HTML文档转Markdown格式软件整合包下载
  • 科普:Linux `su` 切换用户后出现 `$` 提示符,如何排查和解决?
  • BayesFlow:基于神经网络的摊销贝叶斯推断框架
  • NodeJS全栈开发面试题讲解——P9性能优化(Node.js 高级)
  • NVMe IP现状扫盲
  • 5G-A时代与p2p
  • 基于FPGA的DES加解密系统verilog实现,包含testbench和开发板硬件测试
  • 基于生产-消费模式,使用Channel进行文件传输(Tcp方式)
  • tortoisegit 使用rebase修改历史提交
  • Python----目标检测(《用于精确目标检测和语义分割的丰富特征层次结构》和R-CNN)
  • Ansible 进阶 - Roles 与 Inventory 的高效组织
  • 极简以太彩光网络解决方案4.0正式发布,“彩光”重构园区网络极简之道
  • 国芯思辰| 霍尔电流传感器AH811为蓄电池负载检测系统安全护航
  • TortoiseSVN账号切换
  • 2025年05月28日Github流行趋势
  • 精益数据分析(91/126):商业模式与阶段匹配的指标体系构建
  • 篇章五 数据结构——链表(一)
  • 一文清晰理解目标检测指标计算
  • 【MySQL】索引下推减少回表次数
  • Artificial Analysis2025年Q1人工智能发展六大趋势总结
  • DeepSeek模型高级应用:提示工程与Few-shot学习实战指南
  • Android高级开发第三篇 - JNI异常处理与线程安全编程
  • 企业级应用狂潮:从Spotify到LinkedIn的Llama实战手册
  • 高效管理 Python 项目的 UV 工具指南
  • QT中子线程触发主线程弹窗并阻塞等待用户响应
  • 初识vue3(vue简介,环境配置,setup语法糖)
  • HarmonyOS NEXT~鸿蒙开发工具CodeGenie:AI驱动的开发效率革命
  • LeetCode-链表操作题目
  • 【ARM】MDK浏览信息的生成对于构建时间的影响
  • Python模块中__all__变量失效问题深度解析