Word文档编辑服务器

一个基于Model Context Protocol (MCP)的服务器,用于创建、编辑和提取Microsoft Word文档(.docx)数据,支持与AI助手无缝集成。

  • by merzano369
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 merzano369/docx-editor-mcp

使用说明

DOCX编辑器MCP服务器

一个强大的 模型上下文协议(MCP)服务器 用于从Microsoft Word文档中创建、编辑和提取数据(.docx).专为与Claude Desktop、Kilocode和其他MCP兼容客户端等AI助手无缝集成而设计。

📋 目录

✨ 特性

文档创建和管理

  • 创建新文档 具有预先配置的专业风格(Times New Roman,14pt,对齐,1.15行距)
  • 加载现有模板 用于修改
  • 保存文档 到任何指定路径

内容编辑

  • 添加标题 自动造型(居中,Times New Roman,16pt)
  • 添加段落 具有可定制的对齐方式(左、中、右、对齐)
  • 插入格式化文本 具有粗体、斜体、自定义字体大小和语言属性
  • 创建项目符号和编号列表 具有自动格式化功能

文档分析和提取

  • 提取所有文档参数 结构化JSON包括:
    • 核心属性(作者、标题、主题、关键字、创建/修改日期)
    • 自定义属性(用户定义的元数据)
    • 文档变量(用于邮件合并和自动化)
    • 节属性(边距、页面大小、方向)
    • 样式定义(字体、颜色、间距、缩进)
    • 编号和列表定义
    • 页眉和页脚内容
    • 表结构

模板生成

  • 应用提取的参数 创建具有相同格式的新文档
  • 设置核心属性 编程(作者、标题、主题等)
  • 设置自定义属性 用于文档元数据

文档结构分析

  • 获取全面的文档结构摘要
  • 列出所有带有级别和文本的标题
  • 清点段落和表格
  • 预览文档内容

📦 安装

先决条件

  • Python 3.10或更高版本
  • pip包管理器

快速开始

  1. 克隆存储库:

    git clone https://github.com/yourusername/docx-editor-mcp.git
    cd docx-editor-mcp
    
  2. 安装依赖项:

    pip install -r requirements.txt
    
  3. 验证安装:

    python -c "from docx import Document; from mcp.server.fastmcp import FastMCP; print('Installation successful!')"
    

⚙️ 配置

了解MCP服务器

重要提示: MCP服务器使用JSON-RPC协议通过stdio进行通信。它们不打算直接从命令行运行以供交互使用。相反,它们必须由MCP客户端启动。

Claude桌面配置

将以下内容添加到您的Claude Desktop配置文件中:

窗户: %APPDATA%\Claude\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "docx-editor": {
      "command": "python",
      "args": ["C:\\path\\to\\docx-editor-mcp\\server.py"]
    }
  }
}

千码/VS码配置

添加到您的VS代码设置或Kilocode配置中:

{
  "mcp.servers": {
    "docx-editor": {
      "command": "python",
      "args": ["/path/to/docx-editor-mcp/server.py"]
    }
  }
}

🔧 可用工具

文档创建和管理

工具 说明
create_document(filename) 使用默认专业样式创建新文档
save_document(filename) 将当前文档保存到指定路径
load_template(filename) 加载现有文档进行修改

内容添加

工具 说明
add_heading(text, level) 添加具有默认样式的标题(级别1-6)
add_heading_custom(text, level, font_size) 添加具有自定义字体大小的标题
add_paragraph(text, alignment, indent_first_line) 添加带有对齐选项的段落
add_formatted_text(paragraph_index, text, bold, italic, font_size, lang) 将样式文本附加到段落中
add_list_item(text, style) 添加项目符号或编号列表项
insert_header_near_text(target_text, header_title, position) 插入相对于现有文本的标题
insert_line_or_paragraph_near_text(target_text, line_text, position) 插入相对于现有文本的段落
insert_numbered_list_near_text(target_text, list_items, position, bullet_type) 插入相对于现有文本的列表
format_text(paragraph_index, start_pos, end_pos, bold, italic...) 设置段落中特定字符范围的格式
search_and_replace(find_text, replace_text) 在整个文档中全局替换文本
delete_paragraph(paragraph_index) 按索引删除段落
create_custom_style(style_name, bold, italic, color, font_size...) 创建自定义段落样式

表格操作

工具 说明
add_table(rows, cols, style, alignment) 创建新表
add_table_row(table_index) 向表中添加新行
add_table_column(table_index, width_pt) 向表中添加新列
set_table_cell(table_index, row, col, text, alignment, bold, italic) 设置单元格文本和基本格式
merge_table_cells(table_index, start_row, start_col, end_row, end_col) 合并矩形范围的单元格
set_table_cell_style(table_index, row, col, shading_color, vertical_alignment) 设置单元格背景颜色和对齐方式
set_table_borders(table_index, border_size, border_color) 将自定义边框应用于表
delete_table_row(table_index, row_index) 删除特定行
delete_table_column(table_index, col_index) 删除特定列

参数提取

工具 说明
extract_document_parameters(filename, compact, all_styles) 将所有文档参数提取为JSON
extract_core_properties(filename) 提取元数据(作者、标题、日期等)
extract_custom_properties(filename) 提取用户定义的自定义特性
extract_document_variables(filename) 提取文档变量以实现自动化
extract_section_properties(filename) 提取页边距、页面大小、方向
extract_styles_info(filename, all_styles, compact) 提取样式定义
get_document_structure(filename) 获取标题、段落、表格摘要

模板生成

工具 说明
apply_template_parameters(parameters_json, output_filename) 从JSON参数创建文档
set_core_property(property_name, value) 设置元数据属性
set_custom_property(property_name, value) 设置自定义属性

📖 使用示例

创建新文档

问你的AI助手:

Create a new Word document called "report.docx" with:
- A heading "Annual Report 2024"
- A paragraph about company performance
- A bullet list with key achievements

服务器将执行:

create_document("report.docx")
add_heading("Annual Report 2024", level=1)
add_paragraph("The company has shown remarkable growth this year...")
add_list_item("Revenue increased by 25%", style="List Bullet")
add_list_item("Expanded to 3 new markets", style="List Bullet")
save_document()

提取文档参数

Extract all parameters from "template.docx" and show me the styles used.

返回结构化JSON:

{
  "core_properties": {
    "author": "John Doe",
    "title": "Company Template",
    "created": "2024-01-15T10:30:00"
  },
  "sections": [{
    "margins": {
      "top_mm": 15,
      "bottom_mm": 15,
      "left_mm": 20,
      "right_mm": 20
    },
    "orientation": "portrait"
  }],
  "styles": {
    "paragraph_styles": {
      "Normal": {
        "font": {"name": "Times New Roman", "size_pt": 14},
        "paragraph_format": {"alignment": "JUSTIFY", "line_spacing": 1.15}
      }
    }
  }
}

克隆文档模板

Extract parameters from "template.docx" and create a new document "new_report.docx" with the same formatting.
params = extract_document_parameters("template.docx")
apply_template_parameters(params, "new_report.docx")
# Now add your content...
add_heading("New Report", level=1)
add_paragraph("Your content here...")
save_document()

分析文档结构

Load "document.docx" and show me its structure.

退货:

{
  "headings": [
    {"index": 0, "level": "Heading 1", "text": "Introduction"},
    {"index": 5, "level": "Heading 2", "text": "Methodology"}
  ],
  "paragraphs": [
    {"index": 1, "style": "Normal", "text_preview": "This document describes..."},
    {"index": 2, "style": "Normal", "text_preview": "The following sections..."}
  ],
  "tables_count": 2
}

🛠 技术栈

组件 技术
语言 Python 3.10+
协议 模型上下文协议(MCP)
MCP框架 FastMCP
文档引擎 python docx
沟通 基于stdio的JSON-RPC

依赖项

mcp>=1.0.0
python-docx>=0.8.11

默认文档样式

新文档是使用专业默认样式创建的:

元素 样式
普通文本 Times New Roman,14pt,对齐,行距1.15
标题1 Times New Roman,16pt,居中,无粗体
标题2 Times New Roman,16pt,居中,无粗体
边距 顶部/底部:15mm,左侧/右侧:20mm
第一行缩进 12.7毫米(1.27厘米)

🤝 贡献

欢迎投稿!以下是您可以提供帮助的方式:

入门指南

  1. 分叉存储库
  2. 克隆你的叉子:
    git clone https://github.com/yourusername/docx-editor-mcp.git
    
  3. 创建要素分支:
    git checkout -b feature/amazing-feature
    
  4. 进行更改并提交:
    git commit -m "Add amazing feature"
    
  5. 推到您的分支:
    git push origin feature/amazing-feature
    
  6. 打开拉取请求

贡献指南

  • 遵循PEP 8风格指南
  • 将文档字符串添加到所有新函数中
  • 更新任何新功能的文档
  • 在提交之前彻底测试您的更改

功能请求和Bug报告

  • 打开问题以获取错误报告或功能请求
  • 提供错误的详细描述和复制步骤
  • 包括功能请求示例

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。


Made with ❤️ for the MCP community