关于 word-formatter
word-formatter 是一项文档排版技能,可将 DOCX、TXT 或 Markdown 草稿整理为样式化的 Word 和 PDF 文件。它会分析文档类型,允许选择重组程度,并可添加图表或插图。
主要功能
- 01分析文档类型与内容
- 02提供三种处理级别
- 03商务、现代与学术样式
- 04规划图表和插图
- 05输出 DOCX 与 PDF
适用场景
- 01整理会议记录或报告
- 02将草稿制作为格式化 Word 文件
- 03为数据较多的文档添加图表
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ didixuxu/ didi-skills --skill word-formatter安装后在 Claude Code 中输入/word-formatter确认已加载。
附带文件formatter.pystyles/styles.json
详细介绍
Word 格式化工具
将杂乱的文档转换为格式专业的 Word/PDF 文件,并智能排版、自动生成图片。
适用场景
- 用户提供了需要整理的 Word 文档(.docx)或文本文件
- 用户要求“格式化”“美化”或“重新整理”文档
- 需要为纯文本内容添加视觉元素或图表
- 将笔记或草稿转换为专业文档
工作流
1. Analyze Document → 2. User Chooses Level → 3. Format → 4. Generate Images → 5. Output
第 1 步:分析文档
读取输入文件并检测:
# Content type detection keywords
CONTENT_TYPES = {
"meeting_notes": ["会议", "议程", "参会", "决议", "行动项", "meeting", "agenda"],
"report": ["报告", "总结", "分析", "数据", "report", "analysis", "summary"],
"study_notes": ["笔记", "学习", "知识点", "概念", "notes", "learning"],
"proposal": ["方案", "计划", "目标", "预算", "proposal", "plan", "budget"]
}
# Style mapping
STYLE_MAP = {
"meeting_notes": "business_formal",
"report": "business_formal",
"study_notes": "modern_minimal",
"proposal": "academic"
}
向用户展示分析结果:
检测结果:
- 文档类型: [类型]
- 建议风格: [风格]
- 字数: [X] 字
- 检测到数据: [是/否]
第 2 步:由用户选择处理级别
请用户选择:
| 级别 | 说明 |
|---|---|
| 仅排版 | 保留原文内容,只调整格式样式 |
| 轻度重组 | 添加小标题、调整段落、补充过渡内容 |
| 深度重构 | 可以重写内容、优化表达、补充逻辑 |
第 3 步:应用格式
样式定义
商务正式:
BUSINESS_STYLE = {
"title_font": "Microsoft YaHei",
"title_size": 22,
"heading1_size": 16,
"heading2_size": 14,
"body_size": 11,
"line_spacing": 1.5,
"colors": {
"primary": "#1a365d", # Deep blue
"accent": "#2b6cb0", # Medium blue
"text": "#2d3748" # Dark gray
}
}
简约现代:
MODERN_STYLE = {
"title_font": "PingFang SC",
"title_size": 24,
"heading1_size": 18,
"heading2_size": 14,
"body_size": 11,
"line_spacing": 1.8,
"colors": {
"primary": "#1a202c", # Near black
"accent": "#4a5568", # Gray
"text": "#2d3748"
}
}
学术风格:
ACADEMIC_STYLE = {
"title_font": "SimSun",
"title_size": 18,
"heading1_size": 15,
"heading2_size": 13,
"body_size": 12,
"line_spacing": 1.5,
"colors": {
"primary": "#1a202c",
"accent": "#4a5568",
"text": "#000000"
}
}
文档结构
def structure_document(content, level):
"""
Structure document based on processing level
"""
if level == "format_only":
# Keep original structure, apply styles
return apply_styles(content)
elif level == "light_restructure":
# Add headings, reorder paragraphs, add transitions
sections = detect_sections(content)
sections = add_subheadings(sections)
sections = add_transitions(sections)
return apply_styles(sections)
elif level == "deep_restructure":
# Rewrite unclear parts, add logic, improve flow
sections = detect_sections(content)
sections = rewrite_unclear(sections)
sections = add_missing_logic(sections)
sections = optimize_expression(sections)
return apply_styles(sections)
第 4 步:生成图片
图片决策逻辑
def decide_images(content):
"""
Decide what images to generate based on content
"""
images = []
# Check for data → Charts
if contains_data(content):
data_sections = extract_data(content)
for section in data_sections:
chart_type = suggest_chart_type(section)
images.append({
"type": "chart",
"chart_type": chart_type,
"data": section
})
# Check for concepts → AI illustrations
concepts = extract_key_concepts(content)
if concepts:
images.append({
"type": "illustration",
"concept": concepts[0],
"style": get_document_style()
})
# Add cover image
images.append({
"type": "cover",
"title": get_document_title(content),
"style": get_document_style()
})
return images
图表生成
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['PingFang SC', 'Microsoft YaHei']
def generate_chart(data, chart_type, style):
"""
Generate chart matching document style
"""
colors = style["colors"]
fig, ax = plt.subplots(figsize=(10, 6))
if chart_type == "bar":
ax.bar(data["labels"], data["values"], color=colors["primary"])
elif chart_type == "pie":
ax.pie(data["values"], labels=data["labels"],
colors=[colors["primary"], colors["accent"], "#e2e8f0"])
elif chart_type == "line":
ax.plot(data["x"], data["y"], color=colors["primary"], linewidth=2)
ax.set_title(data["title"], fontsize=14, color=colors["text"])
return fig
AI 插图生成
使用 generate-image 技能:
Prompt template for business style:
"Professional business illustration of [concept],
flat design, corporate blue color scheme,
minimalist, clean background, vector style"
Prompt template for modern style:
"Modern minimalist illustration of [concept],
geometric shapes, black and white with accent color,
clean lines, abstract, professional"
Prompt template for academic style:
"Educational diagram illustrating [concept],
clean and simple, textbook style,
labeled components, neutral colors"
图片确认
向用户展示图片方案:
配图方案:
1. 封面图: [主题描述]
2. 数据图表: [图表类型] - [数据来源段落]
3. 概念插图: [概念名称]
请选择:
A) 全部生成
B) 部分修改 (请指定)
C) 跳过配图
第 5 步:输出文件
生成 Word 文档
from docx import Document
from docx.shared import Pt, Inches, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
def create_word_document(content, images, style):
doc = Document()
# Set page margins
for section in doc.sections:
section.top_margin = Inches(1)
section.bottom_margin = Inches(1)
section.left_margin = Inches(1.25)
section.right_margin = Inches(1.25)
# Add cover image if exists
if images.get("cover"):
doc.add_picture(images["cover"], width=Inches(6))
doc.add_page_break()
# Add content with styles
for element in content:
if element["type"] == "title":
p = doc.add_heading(element["text"], level=0)
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
elif element["type"] == "heading1":
doc.add_heading(element["text"], level=1)
elif element["type"] == "heading2":
doc.add_heading(element["text"], level=2)
elif element["type"] == "paragraph":
p = doc.add_paragraph(element["text"])
p.paragraph_format.line_spacing = style["line_spacing"]
elif element["type"] == "image":
doc.add_picture(element["path"], width=Inches(5))
return doc
转换为 PDF
# Using LibreOffice (cross-platform)
libreoffice --headless --convert-to pdf --outdir [output_dir] [docx_file]
# Or using docx2pdf (requires MS Word on Windows/Mac)
# pip install docx2pdf
from docx2pdf import convert
def export_pdf(docx_path):
pdf_path = docx_path.replace('.docx', '.pdf')
convert(docx_path, pdf_path)
return pdf_path
输出文件命名
def get_output_paths(input_path):
base = os.path.splitext(input_path)[0]
return {
"docx": f"{base}_formatted.docx",
"pdf": f"{base}_formatted.pdf"
}
完整交互流程
User: /word-formatter /path/to/messy_document.docx
Claude: 正在分析文档...
检测结果:
- 文档类型: 工作报告
- 建议风格: 商务正式
- 字数: 2,350 字
- 检测到数据: 是 (3处数据可视化)
请选择处理程度:
A) 仅排版 - 保持原文,调整格式
B) 轻度重组 - 添加小标题,优化结构
C) 深度重构 - 可重写内容,优化表达
User: B
Claude: 好的,将进行轻度重组。正在处理...
配图方案:
1. 封面图: 商务风格的工作报告封面
2. 柱状图: 季度销售数据对比
3. 饼图: 市场份额分布
4. 概念插图: 增长策略示意图
请选择:
A) 全部生成
B) 部分修改
C) 跳过配图
User: A
Claude: 正在生成配图...
[生成图片中...]
完成!输出文件:
- /path/to/messy_document_formatted.docx
- /path/to/messy_document_formatted.pdf
需要调整吗?
依赖项
# Required Python packages
pip install python-docx matplotlib docx2pdf pillow
# For PDF conversion (alternative to docx2pdf)
# macOS: brew install --cask libreoffice
# Ubuntu: sudo apt install libreoffice
错误处理
| 错误 | 处理方式 |
|---|---|
| 文件不存在 | 提示用户检查路径 |
| 格式不受支持 | 仅支持 .docx、.txt、.md |
| 图片生成失败 | 跳过该图片并继续处理 |
| PDF 转换失败 | 仅输出 Word,并提示安装依赖 |
提示
- 输入文档的结构越清晰,排版效果越好
- 在深度重构模式下,Claude 会尝试改善文档逻辑
- 配图会自动匹配文档风格的配色方案
- 可以多次调用以迭代优化
评论
支持 Markdown