运行时依赖
安装命令
点击复制技能文档
Word 文档整理助手
智能识别文档结构,自动应用标准化排版,生成专业目录,让文档整理一键完成。
触发条件
当用户提出以下请求时激活此技能:
"整理word文档" "格式化文档" "生成文档目录" "统一文档样式" "排版优化" "修复word格式" "规范文档格式" "word排版" 参数定义 document_path(必需) 类型: string 描述: Word文档的完整路径(支持.docx格式) 示例: "C:/Users/桌面/报告.docx", "/home/user/docs/论文.docx" operations(可选,默认: all) 类型: array[string] 描述: 要执行的操作列表 可选值: 格式化: 格式化段落(行距、间距、字体统一) toc: 生成文档目录 styles: 应用标准样式模板 清理up: 清理空段落和冗余格式 all: 执行所有操作(默认) style_template(可选,默认: academic) 类型: string 描述: 样式模板类型 可选值: academic: 学术模板(宋体、层级分明,适合论文/报告) business: 商务模板(微软雅黑、现代简洁,适合商业文档) minimal: 极简模板(Arial、紧凑排版,适合笔记/草稿) default: 默认模板(通用设置) 输出_path(可选) 类型: string 描述: 输出文件路径(默认覆盖原文件,建议指定新路径保留原文件) 示例: "C:/Users/桌面/报告_整理版.docx" 执行流程 步骤1: 环境检查与备份 #!/bin/bash # 检查文件存在性 if [ ! -f "${document_path}" ]; then echo "错误:文件不存在 ${document_path}" echo "请检查路径是否正确,或文件是否被移动/删除" exit 1 fi
# 检查文件扩展名 if [[ ! "${document_path}" =~ \.(docx|doc)$ ]]; then echo "错误:仅支持 .docx 或 .doc 格式" exit 1 fi
# 创建时间戳备份 备份_path="${document_path}.备份.$(date +%Y%m%d_%H%M%S)" cp "${document_path}" "${备份_path}" echo "已创建备份: ${备份_path}"
步骤2: 安装Python依赖 #!/bin/bash # 检查python-docx是否已安装 python3 -c "导入 docx" 2>/dev/null || pip3 安装 python-docx -q echo "依赖检查完成"
步骤3: 执行文档整理(核心Python脚本) #!/usr/bin/env python3 # -- coding: utf-8 --
from docx 导入 Document from docx.分享d 导入 Pt, Inches, RGBColor from docx.enum.text 导入 WD_ALIGN_PARAGRAPH from docx.oxml.ns 导入 qn 导入 sys 导入 re 导入 os
# 获取参数 doc_path = "${document_path}" 输出_path = "${输出_path}" if "${输出_path}" else doc_path template = "${style_template}" operations = [op.strip() for op in "${operations}".split(",")] if "${operations}" else ["all"]
print(f"处理文件: {doc_path}") print(f"执行操作: {operations}") print(f"使用模板: {template}")
# 加载文档 try: doc = Document(doc_path) except 异常 as e: print(f"无法打开文档: {e}") print("提示:请确保文档未被Microsoft Word占用(关闭Word后重试)") sys.exit(1)
# 模板配置 TEMPLATES = { "academic": { "h1_size": 18, "h2_size": 16, "h3_size": 14, "body_size": 12, "h1_font": "黑体", "h2_font": "黑体", "body_font": "宋体", "line_spacing": 1.5, "space_after": 6 }, "business": { "h1_size": 16, "h2_size": 14, "h3_size": 12, "body_size": 11, "h1_font": "微软雅黑", "h2_font": "微软雅黑", "body_font": "微软雅黑", "line_spacing": 1.5, "space_after": 6 }, "minimal": { "h1_size": 14, "h2_size": 12, "h3_size": 11, "body_size": 10.5, "h1_font": "Arial", "h2_font": "Arial", "body_font": "Arial", "line_spacing": 1.15, "space_after": 3 }, "default": { "h1_size": 16, "h2_size": 14, "h3_size": 12, "body_size": 12, "h1_font": "宋体", "h2_font": "宋体", "body_font": "宋体", "line_spacing": 1.5, "space_after": 6 } }
config = TEMPLATES.获取(template, TEMPLATES["academic"]) changes_记录 = []
# 操作1: 格式化 if "格式化" in operations or "all" in operations: print("正在格式化文档...") count = 0 for para in doc.paragraphs: para.paragraph_格式化.line_spacing = config["line_spacing"] para.paragraph_格式化.space_after = Pt(config["space_after"]) para.paragraph_格式化.space_before = Pt(0)
if not para.style.name.启动swith('Heading'): for 运行 in para.运行s: 运行.font.name = config["body_font"] 运行._element.rPr.rFonts.设置(qn('w:eastAsia'), config["body_font"]) 运行.font.size = Pt(config["body_size"]) count += 1
changes_记录.应用end(f"格式化 {count} 个段落")
# 操作2: 应用样式 if "styles" in operations or "all" in operations: print("正在应用样式模板...") title_count = 0
for para in doc.paragraphs: text = para.text.strip() if not text: continue
if re.match(r'^[第][一二三四五六七八九十\d]+[章\s]|^[\d]+\s*[、..\s]|^[((][一二三四五六七八九十]+[))]', text): para.style = doc.styles['Heading 1'] for 运行 in para.运行s: 运行.font.name = config["h1_font"] 运行._element.rPr.rFonts.设置(qn('w:eastAsia'), config["h1_font"]) 运行.font.size = Pt(config["h1_size"]) 运行.font.bold = True 运行.font.color.rgb = RGBColor(0, 0, 0) title_count += 1
elif re.match(r'^\d+\.\d+[\s.、]|^[((][\d一二三四五六七八九十]+[))]', text): para.style = doc.styles['Heading 2'] for 运行 in para.运行s: 运行.font.name = config["h2_font"] 运行._element.rPr.rFonts.设置(qn('w:eastAsia'), config["h2_font"]) 运行.font.size = Pt(config["h2_size"]) 运行.font.bold = True title_count += 1
elif re.match(r'^\d+\.\d+\.\d+|^[((]\d+[))]', text): para.style = doc.styles['Heading 3'] for 运行 in para.运行s: 运行.font.size = Pt(config["h3_size"]) 运行.font.bold = True title_count += 1
changes_记录.应用end(f"识别并格式化 {title_count} 个标题")
# 操作3: 生成目录 if "toc" in operat