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

成都软件网站开发网络平台营销的特点

成都软件网站开发,网络平台营销的特点,电子商务网站建设侧重点,什么是网站域名主要功能是按照JSON文件(Sort.json)中指定的顺序合并多个Word文档(.docx),并清除文档中的所有超链接。最终输出合并后的文档名为"sorted_按章节顺序.docx"。 主要分为几个部分: 初始化配置 定…

主要功能是按照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.dtcms.com/wzjs/806415.html

相关文章:

  • 访问不了服务器的网站网站后台管理系统模板 html
  • 国内做服装的网站有哪些做医药商城网站的公司
  • 企业网站公司单位有哪些网站建设价格差异多少
  • 上海龙雨建设工程有限公司网站建设银行招聘官方网站
  • 景洪网站建设网站做弹窗广告吗
  • 自己服务器可以做网站6617网址导航彩票网站大全
  • 网站提交入口大全wordpress发布模块支持5.x
  • 网站开发方案模板好的网站建设方案
  • 阿里云可以建设网站吗用js做简单的网站页面
  • 公司网站如何在百度上能搜索到做计算机项目的网站
  • 网站中新颖的功能怎么自建一个网站
  • 教你如何建设网站阿里去wordpress标签导航
  • 梅州建站联系方式做音乐网站首页要求
  • html5网站app开发wordpress站点app开发
  • 宁波静态网站建设免费网络软件
  • 做任务网站排行榜提供网站建设找哪家公司好
  • 网站logo怎么改wordpress4.5发布模块
  • 马鞍山网站建设与制作广东上海专业网站建设公司排名
  • 广西南宁网站策划天猫店铺购买平台
  • 做网站一般都需要什么功能龙岩iot开发福建小程序建设
  • 夏天做哪些网站致富免费建网站视频教程
  • 免费建网站网址网站制作的企业有哪些
  • 百度哪个网站做贸易产品网络推广怎样做
  • 清除wordpress数据库中多余的seo做什么网站赚钱
  • 中粮我买网是哪个公司做的网站做景观要知道哪些网站
  • 深圳专业网站建设排名网站打开太慢什么原因
  • 福建省两学一做网站wordpress用插件注册
  • 安徽工业大学两学一做网站有什么比较好的做简历的网站
  • 海门市城乡建设局网站门户网站什么意思举例子
  • 网站搭建代码大全wordpress链接mysql