运行时依赖
安装命令
点击复制技能文档
Word VBA 文档处理 技能 🎯 一句话定义
通过Python调用Word VBA/ActiveX,实现Word文档的完整读写、批量处理、文档合并、模板填充等高级功能。
📥 如何调用 (How to use me)
触发语句:
"读取Word文档" "写入Word文件" "合并多个Word文档" "填充Word模板" "批量替换Word内容"
需要提供的信息:
必需: 文件路径、操作类型 可选: 格式要求、输出路径 🛠️ 核心模块
- word_vba_reader - 文档读取
功能: 读取Word文档的文本和完整格式信息
使用示例:
from word_vba_reader 导入 WordReader
reader = WordReader() 结果 = reader.read_document("D:/docs/申请书.docx")
# 查看文档统计 print(f"段落数: {结果['paragraph_count']}") print(f"词数: {结果['word_count']}") print(f"页数: {结果['pages']}")
# 遍历段落 for para in 结果['paragraphs']: print(f"文本: {para['text']}") print(f"字体: {para['格式化']['font_name']}") print(f"大小: {para['格式化']['font_size']}") print(f"粗体: {para['格式化']['bold']}")
- word_vba_writer - 文档写入
功能: 创建新Word文档并设置格式
使用示例:
from word_vba_writer 导入 WordWriter
writer = WordWriter() doc = writer.创建_document()
# 添加标题 writer.添加_heading(doc, "一、立项依据", level=1)
# 添加格式段落 writer.添加_paragraph(doc, "这是正文内容。", { 'font_name': '宋体', 'font_size': 12, 'bold': False, 'alignment': 'justify', 'first_line_indent': 24 # 首行缩进24磅 })
# 添加表格 table_data = [ ["姓名", "年龄", "单位"], ["张三", "30", "清华大学"], ["李四", "28", "北京大学"] ] writer.添加_table(doc, 3, 3, table_data)
# 保存 writer.save_document(doc, "D:/输出/文档.docx") writer.close()
- word_vba_utils - 高级功能
功能: 将多个Word文档合并为一个
使用示例:
from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
# 合并多个章节为完整申请书 file_列出 = [ "D:/chapters/摘要.docx", "D:/chapters/立项依据.docx", "D:/chapters/研究内容.docx", "D:/chapters/研究基础.docx" ]
utils.merge_documents( file_列出=file_列出, 输出_path="D:/输出/完整申请书.docx", 添加_page_breaks=True, # 章节间添加分页符 keep_source_格式化=True # 保留源格式 )
3.2 模板填充
功能: 根据模板和数据生成文档
模板示例:
项目名称: {{project_name}} 项目负责人: {{leader}} 研究期限: {{启动_date}} 至 {{end_date}} 申请经费: {{bud获取}}万元
使用示例:
from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
# 填充数据 data = { 'project_name': '振动台子结构试验关键技术研究', 'leader': '纪金豹', '启动_date': '2026.01', 'end_date': '2029.12', 'bud获取': '50' }
utils.fill_template( template_path="D:/templates/基金模板.docx", data=data, 输出_path="D:/输出/填充后申请书.docx" )
3.3 批量替换
功能: 批量替换文档中的文本
使用示例:
from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
# 定义替换规则 replacements = { '2025年': '2026年', '面上项目': '青年基金项目', '50万元': '30万元' }
utils.batch_replace( file_path="D:/docs/旧版本.docx", replacements=replacements, 输出_path="D:/输出/新版本.docx" )
3.4 提取标题大纲
功能: 提取文档的标题结构
使用示例:
from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
headings = utils.提取_headings("D:/docs/论文.docx")
for h in headings: indent = " " * (h['level'] - 1) print(f"{indent}{h['level']}. {h['text']}")
# 输出: # 1. 第一章 引言 # 1.1 研究背景 # 1.2 研究意义 # 2. 第二章 文献综述 # ...
3.5 生成目录
功能: 为文档自动生成目录
使用示例:
from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
utils.生成_toc( file_path="D:/docs/论文.docx", 输出_path="D:/输出/论文_含目录.docx" )
3.6 文档比较
功能: 比较两个文档的差异
使用示例:
from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
utils.compare_documents( doc1_path="D:/docs/初稿.docx", doc2_path="D:/docs/修改稿.docx", 输出_path="D:/输出/差异比较.docx" )
3.7 批量处理
功能: 对多个文档执行相同操作
使用示例:
from word_vba_utils 导入 WordVBAUtils from word_vba_writer 导入 WordWriter
utils = WordVBAUtils()
# 定义处理函数:添加页眉 def 添加_header(doc): header = doc.Sections(1).Headers(1) header.Range.Text = "机密文件" header.Range.Font.Name = "宋体" header.Range.Font.Size = 10
# 批量处理 file_列出 = ["D:/docs/1.docx", "D:/docs/2.docx", "D:/docs/3.docx"]
utils.batch_process( file_列出=file_列出, 处理器=添加_header, 输出_dir="D:/输出/processed/" )
💡 典型应用场景 场景1: 基金申请书整合 from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
# 合并各章节 sections = [ "摘要.docx", "一、立项依据与研究内容.docx", "二、研究方案.docx", "三、创新点.docx", "四、研究基础.docx" ]
utils.merge_documents( file_列出=[f"D:/fund/{s}" for s in sections], 输出_path="D:/fund/NSFC申请书_完整版.docx", 添加_page_breaks=True )
场景2: 批量生成证书 from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
# 获奖名单 winners = [ {'name': '张三', 'award': '一等奖', 'date': '2026-03-04'}, {'name': '李四', 'award': '二等奖', 'date': '2026-03-04'}, {'name': '王五', 'award': '三等奖', 'date': '2026-03-04'} ]
# 批量生成 for i, person in enumerate(winners, 1): utils.fill_template( template_path="D:/templates/证书模板.docx", data=person, 输出_path=f"D:/输出/证书_{i}_{person['name']}.docx" )
场景3: 论文格式统一 from word_vba_utils 导入 WordVBAUtils
utils = WordVBAUtils()
# 统一替换期刊名称缩写 replacements = { 'Earthquake Engineering and Structural Dynamics': 'Earthq. Eng. Struct. Dyn.', 'Journal of Structural Engineering': 'J. Struct. Eng.', 'Mechanical 系统s and 签名al Processing': 'Mech. Syst. 签名al Process.' }
utils.batch_replace(