mcpField
X
xlsx

xlsx

github.com/stvlynn文档处理12196 次安装32 周前更新
代码仓库

关于 xlsx

xlsx 是面向 Excel 电子表格任务的处理技能,适用于数据分析、报表制作、财务建模和批量文件处理。它规定了公式重算与输出验证流程,并要求交付文件中不含公式错误。

主要功能

  1. 01分析和清洗表格数据
  2. 02生成自动化报表
  3. 03创建带公式的电子表格
  4. 04批量处理 Excel 文件
  5. 05重算并检查公式错误
  6. 06保留现有模板格式

适用场景

  1. 01汇总数据并生成统计报告
  2. 02制作带公式的销售报表
  3. 03建立带色彩编码的财务预测模型
  4. 04清洗并合并多个 Excel 文件

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/stvlynn/dingtalk-wukong-skills --skill xlsx

安装后在 Claude Code 中输入/xlsx确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

requirements.txt
docs/
00-SKILL-完整操作指南.md
scripts/
recalc.py

详细介绍

Excel 电子表格处理专家 Real Skill

一句话说明

专业的 Excel 处理工具,强制执行零公式错误标准,支持数据分析、报表生成和财务建模。

Excel Spreadsheet Mastery - 零错误公式标准,数据分析、自动生成报表、财务建模与色彩编码。


使用场景

适用:

  • 数据分析和统计报告
  • 自动生成 Excel 报表
  • 财务预测模型(色彩编码)
  • 批量处理多个 Excel 文件
  • 数据清洗和格式转换

不适用:

  • 实时协同编辑(使用 Google Sheets / 使用 Google Sheets 进行实时协作)
  • 复杂宏操作(使用 VBA / 使用 VBA 执行复杂宏操作)

核心流程

用户需求 → 判断任务类型 → 选择工具 → 执行操作 → 公式重算 → 验证输出
User request → Task classification → Tool selection → Execute → Recalc formulas → Validate

任务类型判断 (Task Classification):

Data Analysis (数据分析)
  • Simple analysis → pandas(读取、分析、可视化)
  • Statistical reports → pandas + matplotlib/seaborn
  • Data cleaning → pandas 转换
Spreadsheet Creation (创建电子表格)
  • With formulas → openpyxl + recalc.py(必须)
  • Without formulas → pandas(速度更快)
  • Financial models → openpyxl + 色彩编码 + recalc.py
Editing Existing (编辑已有文件)
  • Data updates → openpyxl(保留公式)
  • Template modifications → 完全匹配现有格式

⚠️ 关键:使用公式后,必须运行 python scripts/recalc.py output.xlsx


任务完成标准

必须满足(缺一不可):

  • ✅ 零公式错误 (Zero Formula Errors)(#REF!, #DIV/0!, #VALUE!, #N/A, #NAME? = 0)
  • Excel 文件可正常打开
  • 数据完整准确
  • 格式规范(数字格式、对齐、边框)
  • 财务模型使用行业标准色彩编码(如适用)

质量评级:

  • ⭐⭐⭐⭐⭐ 优秀 - 零错误 + 色彩编码 + 完整文档
  • ⭐⭐⭐ 及格 - 零错误 + 数据正确
  • ⭐ 失败 - 存在公式错误

参考资料(供 AI 使用)

类型路径说明
核心文档docs/00-SKILL-完整操作指南.md完整 Excel 处理指南(约 300 行)
工具脚本scripts/recalc.py公式重算脚本(需要 LibreOffice)

关键原则(AI 必读 / 关键原则)

1. ⚠️ 零公式错误(必须)

交付的每个 Excel 文件都必须确保零公式错误:

  • 不得有 #REF!(引用无效)
  • 不得有 #DIV/0!(除以零)
  • 不得有 #VALUE!(数据类型错误)
  • 不得有 #N/A(未找到查找结果)
  • 不得有 #NAME?(无法识别的公式名称)

验证流程:

python scripts/recalc.py output.xlsx
# Check JSON output: status should be "success"
# If "errors_found", fix and recalculate
2. 使用公式,而非硬编码值

关键:始终使用 Excel 公式,而不是在 Python 中进行计算。

❌ 错误 - 硬编码:

total = df['Sales'].sum()
sheet['B10'] = total  # Hardcodes 5000

✅ 正确 - 使用公式:

sheet['B10'] = '=SUM(B2:B9)'  # Dynamic formula

原因:源数据发生变化时,电子表格仍可动态更新。

3. 财务建模色彩编码

行业标准色彩惯例(除非用户另有指定):

from openpyxl.styles import Font

# Blue text (0000FF): User inputs and assumptions
ws['A1'].font = Font(color='0000FF')

# Black text (000000): ALL formulas and calculations
ws['B1'].font = Font(color='000000')

# Green text (008000): Links within same workbook
ws['C1'].font = Font(color='008000')

# Red text (FF0000): External file links
ws['D1'].font = Font(color='FF0000')

# Yellow background (FFFF00): Key assumptions
from openpyxl.styles import PatternFill
ws['E1'].fill = PatternFill(start_color='FFFF00', fill_type='solid')
4. 数字格式标准
from openpyxl.styles import numbers

# Years: Format as text strings "2024" (not numbers)
ws['A1'].number_format = '@'  # Text format
ws['A1'] = "'2024"  # Force text

# Currency: $#,##0 + specify units in header
ws['B1'].number_format = '$#,##0'

# Zeros: Format as "-"
ws['C1'].number_format = '$#,##0;($#,##0);"-"'

# Percentages: 0.0% (one decimal)
ws['D1'].number_format = '0.0%'

# Multiples: 0.0x for valuation multiples
ws['E1'].number_format = '0.0"x"'

# Negative numbers: Use parentheses (123) not minus -123
ws['F1'].number_format = '#,##0;(#,##0)'
5. 保留现有模板

修改现有文件时:

  • 仔细研究并完全匹配现有格式、样式和惯例
  • 对已有固定格式的文件,绝不强行套用标准格式
  • 现有模板惯例始终优先于这些指南

快速命令参考

使用 Pandas 进行数据分析
import pandas as pd

# Read Excel (single sheet or all)
df = pd.read_excel('file.xlsx')  # First sheet
all_sheets = pd.read_excel('file.xlsx', sheet_name=None)  # All sheets as dict

# Analyze
df.head()       # Preview data
df.info()       # Column info
df.describe()   # Statistics
df.groupby('Category')['Sales'].sum()  # Group by

# Write Excel
df.to_excel('output.xlsx', index=False)
创建带公式的 Excel
from openpyxl import Workbook
from openpyxl.styles import Font

wb = Workbook()
ws = wb.active

# Data with formulas
ws['A1'] = 'Revenue'
ws['A2'] = 1000
ws['A3'] = 1200
ws['A4'] = 'Total'
ws['B4'] = '=SUM(A2:A3)'  # Use formula, not hardcoded value

# Color coding
ws['A2'].font = Font(color='0000FF')  # Blue: User input
ws['B4'].font = Font(color='000000')  # Black: Formula

wb.save('output.xlsx')
重算公式(必须)
# After saving file with formulas
python scripts/recalc.py output.xlsx

# Output JSON shows:
# - status: "success" or "errors_found"
# - error_summary: Details of any errors
# - total_errors: Count of formula errors
使用色彩编码进行财务建模
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill

wb = Workbook()
ws = wb.active

# Headers
ws['A1'] = 'Year'
ws['B1'] = 'Revenue ($mm)'
ws['C1'] = 'Growth %'

# Data with color coding
ws['A2'] = "'2024"  # Force text for year
ws['B2'] = 100
ws['B2'].font = Font(color='0000FF')  # Blue: User input

ws['C2'] = 0.15  # Growth assumption
ws['C2'].font = Font(color='0000FF')
ws['C2'].fill = PatternFill(start_color='FFFF00', fill_type='solid')  # Yellow: Key assumption
ws['C2'].number_format = '0.0%'

# Formulas
ws['B3'] = '=B2*(1+C2)'  # Revenue calculation
ws['B3'].font = Font(color='000000')  # Black: Formula

wb.save('financial_model.xlsx')

# MUST recalculate
import subprocess
subprocess.run(['python', 'scripts/recalc.py', 'financial_model.xlsx'])
错误处理
import subprocess
import json

# Run recalc and check results
result = subprocess.run(
    ['python', 'scripts/recalc.py', 'output.xlsx'],
    capture_output=True,
    text=True
)

data = json.loads(result.stdout)

if data['status'] == 'errors_found':
    print(f"Found {data['total_errors']} errors:")
    for error_type, count in data['error_summary'].items():
        print(f"  {error_type}: {count}")
    # Fix errors and recalculate
else:
    print("✅ Zero formula errors - file ready!")

安装依赖

Python 依赖
pip install -r requirements.txt
# Includes: openpyxl, pandas, xlrd, openpyxl
系统依赖(LibreOffice)
# macOS
brew install --cask libreoffice

# Ubuntu/Debian
sudo apt-get install libreoffice

# Windows: Download from https://www.libreoffice.org/

注意:首次运行时,recalc.py 会自动配置 LibreOffice。


常见场景示例

场景 1:创建带自动汇总的销售报告
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import Font

# Step 1: Create data with pandas
data = {
    'Product': ['A', 'B', 'C'],
    'Q1': [1000, 800, 1200],
    'Q2': [1100, 850, 1300],
    'Q3': [1050, 900, 1250],
    'Q4': [1200, 950, 1400]
}
df = pd.DataFrame(data)
df.to_excel('sales.xlsx', index=False, startrow=1)

# Step 2: Add formulas with openpyxl
wb = load_workbook('sales.xlsx')
ws = wb.active

# Add title
ws['A1'] = '2024 Sales Report ($000s)'
ws['A1'].font = Font(bold=True, size=14)

# Add totals row
last_row = len(df) + 2
ws[f'A{last_row}'] = 'Total'
ws[f'A{last_row}'].font = Font(bold=True)

for col in ['B', 'C', 'D', 'E']:
    ws[f'{col}{last_row}'] = f'=SUM({col}2:{col}{last_row-1})'
    ws[f'{col}{last_row}'].font = Font(color='000000', bold=True)

wb.save('sales.xlsx')

# Step 3: Recalculate
import subprocess
subprocess.run(['python', 'scripts/recalc.py', 'sales.xlsx'])
场景 2:三年财务预测
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment

wb = Workbook()
ws = wb.active

# Headers
headers = ['Metric', '2024', '2025', '2026']
for col, header in enumerate(headers, start=1):
    ws.cell(1, col, header)
    ws.cell(1, col).font = Font(bold=True)
    ws.cell(1, col).alignment = Alignment(horizontal='center')

# Assumptions (Blue + Yellow background)
ws['A2'] = 'Base Revenue'
ws['B2'] = 10000
ws['B2'].font = Font(color='0000FF')
ws['B2'].fill = PatternFill(start_color='FFFF00', fill_type='solid')

ws['A3'] = 'Growth Rate'
ws['B3'] = 0.15
ws['B3'].font = Font(color='0000FF')
ws['B3'].fill = PatternFill(start_color='FFFF00', fill_type='solid')
ws['B3'].number_format = '0.0%'

# Calculations (Black formulas)
ws['A5'] = 'Projected Revenue'
ws['B5'] = '=B2'
ws['B5'].font = Font(color='000000')

ws['C5'] = '=B5*(1+$B$3)'
ws['C5'].font = Font(color='000000')

ws['D5'] = '=C5*(1+$B$3)'
ws['D5'].font = Font(color='000000')

# Number formatting
for col in ['B', 'C', 'D']:
    ws[f'{col}5'].number_format = '$#,##0'

wb.save('projection.xlsx')

# MUST recalculate
import subprocess
subprocess.run(['python', 'scripts/recalc.py', 'projection.xlsx'])
场景 3:批量处理多个 Excel 文件
import pandas as pd
import glob

all_data = []

for file in glob.glob('data/*.xlsx'):
    df = pd.read_excel(file)
    df['source_file'] = file
    all_data.append(df)

combined = pd.concat(all_data, ignore_index=True)
combined.to_excel('combined_report.xlsx', index=False)

print(f"Processed {len(all_data)} files, total {len(combined)} rows")

评论

支持 Markdown