The Agent Skills Directory
npx skills add https://smithery.ai/skills/davila7/xlsx除非用户或现有模板另有说明
=B5*(1+$B$6) 而非 =B5*1.05使用 openpyxl 和 pandas 创建、编辑或分析包含公式、格式设置和数据分析的 Excel 电子表格。应用此技能进行电子表格处理。重新计算公式并确保零错误,以获得可发布的输出质量。
使用此技能创建文档时,始终考虑添加科学图表和示意图以增强视觉传达效果。
如果您的文档尚未包含示意图或图表:
对于新文档: 默认应生成科学示意图,以直观地表示文本中描述的关键概念、工作流程、架构或关系。
如何生成示意图:
python scripts/generate_schematic.py "your diagram description" -o figures/output.png
AI 将自动:
何时添加示意图:
有关创建示意图的详细指导,请参阅 scientific-schematics 技能文档。
重新计算公式需要 LibreOffice:您可以假设已安装 LibreOffice,以便使用 recalc.py 脚本重新计算公式值。该脚本在首次运行时自动配置 LibreOffice。
对于数据分析、可视化和基本操作,请使用 pandas,它提供了强大的数据操作能力:
import pandas as pd
# 读取 Excel
df = pd.read_excel('file.xlsx') # 默认:第一个工作表
all_sheets = pd.read_excel('file.xlsx', sheet_name=None) # 所有工作表作为字典
# 分析
df.head() # 预览数据
df.info() # 列信息
df.describe() # 统计信息
# 写入 Excel
df.to_excel('output.xlsx', index=False)
始终使用 Excel 公式,而不是在 Python 中计算值然后硬编码。 这确保了电子表格保持动态和可更新性。
# 错误:在 Python 中计算并硬编码结果
total = df['Sales'].sum()
sheet['B10'] = total # 硬编码 5000
# 错误:在 Python 中计算增长率
growth = (df.iloc[-1]['Revenue'] - df.iloc[0]['Revenue']) / df.iloc[0]['Revenue']
sheet['C5'] = growth # 硬编码 0.15
# 错误:在 Python 中计算平均值
avg = sum(values) / len(values)
sheet['D20'] = avg # 硬编码 42.5
# 正确:让 Excel 计算总和
sheet['B10'] = '=SUM(B2:B9)'
# 正确:将增长率作为 Excel 公式
sheet['C5'] = '=(C4-C2)/C2'
# 正确:使用 Excel 函数计算平均值
sheet['D20'] = '=AVERAGE(D2:D19)'
这适用于所有计算 - 总计、百分比、比率、差异等。电子表格应能在源数据更改时重新计算。
python recalc.py output.xlsx
status 为 errors_found,请检查 error_summary 以获取具体的错误类型和位置#REF!:无效的单元格引用#DIV/0!:除以零#VALUE!:公式中使用了错误的数据类型# 使用 openpyxl 处理公式和格式设置
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment
wb = Workbook()
sheet = wb.active
# 添加数据
sheet['A1'] = 'Hello'
sheet['B1'] = 'World'
sheet.append(['Row', 'of', 'data'])
# 添加公式
sheet['B2'] = '=SUM(A1:A10)'
# 格式设置
sheet['A1'].font = Font(bold=True, color='FF0000')
sheet['A1'].fill = PatternFill('solid', start_color='FFFF00')
sheet['A1'].alignment = Alignment(horizontal='center')
# 列宽
sheet.column_dimensions['A'].width = 20
wb.save('output.xlsx')
# 使用 openpyxl 保留公式和格式设置
from openpyxl import load_workbook
# 加载现有文件
wb = load_workbook('existing.xlsx')
sheet = wb.active # 或 wb['SheetName'] 用于特定工作表
# 处理多个工作表
for sheet_name in wb.sheetnames:
sheet = wb[sheet_name]
print(f"Sheet: {sheet_name}")
# 修改单元格
sheet['A1'] = 'New Value'
sheet.insert_rows(2) # 在第 2 行插入行
sheet.delete_cols(3) # 删除第 3 列
# 添加新工作表
new_sheet = wb.create_sheet('NewSheet')
new_sheet['A1'] = 'Data'
wb.save('modified.xlsx')
由 openpyxl 创建或修改的 Excel 文件包含作为字符串的公式,但不包含计算值。使用提供的 recalc.py 脚本重新计算公式:
python recalc.py <excel_file> [timeout_seconds]
示例:
python recalc.py output.xlsx 30
该脚本:
确保公式正常工作的快速检查:
pd.notna() 检查空值/ 之前检查分母(#DIV/0!)脚本返回包含错误详情的 JSON:
{
"status": "success", // 或 "errors_found"
"total_errors": 0, // 错误总数
"total_formulas": 42, // 文件中的公式数量
"error_summary": { // 仅在发现错误时存在
"#REF!": {
"count": 2,
"locations": ["Sheet1!B5", "Sheet1!C10"]
}
}
}
data_only=True 读取计算值:load_workbook('file.xlsx', data_only=True)data_only=True 打开并保存,公式将被值替换并永久丢失read_only=True,写入时使用 write_only=Truepd.read_excel('file.xlsx', dtype={'id': str})pd.read_excel('file.xlsx', usecols=['A', 'C', 'E'])pd.read_excel('file.xlsx', parse_dates=['date_column'])重要:为 Excel 操作生成 Python 代码时:
对于 Excel 文件本身:
每周安装数
218
来源
首次出现
2026年3月6日
安全审计
安装于
claude-code136
opencode67
codex67
cursor63
gemini-cli56
github-copilot51
Unless otherwise stated by the user or existing template
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
#NAME?:无法识别的公式名称Create, edit, or analyze Excel spreadsheets with formulas, formatting, and data analysis. Apply this skill for spreadsheet processing using openpyxl and pandas. Recalculate formulas and ensure zero errors for publication-quality outputs.
When creating documents with this skill, always consider adding scientific diagrams and schematics to enhance visual communication.
If your document does not already contain schematics or diagrams:
For new documents: Scientific schematics should be generated by default to visually represent key concepts, workflows, architectures, or relationships described in the text.
How to generate schematics:
python scripts/generate_schematic.py "your diagram description" -o figures/output.png
The AI will automatically:
When to add schematics:
For detailed guidance on creating schematics, refer to the scientific-schematics skill documentation.
LibreOffice Required for Formula Recalculation : You can assume LibreOffice is installed for recalculating formula values using the recalc.py script. The script automatically configures LibreOffice on first run
For data analysis, visualization, and basic operations, use pandas which provides powerful data manipulation capabilities:
import pandas as pd
# Read Excel
df = pd.read_excel('file.xlsx') # Default: 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
# Write Excel
df.to_excel('output.xlsx', index=False)
Always use Excel formulas instead of calculating values in Python and hardcoding them. This ensures the spreadsheet remains dynamic and updateable.
# Bad: Calculating in Python and hardcoding result
total = df['Sales'].sum()
sheet['B10'] = total # Hardcodes 5000
# Bad: Computing growth rate in Python
growth = (df.iloc[-1]['Revenue'] - df.iloc[0]['Revenue']) / df.iloc[0]['Revenue']
sheet['C5'] = growth # Hardcodes 0.15
# Bad: Python calculation for average
avg = sum(values) / len(values)
sheet['D20'] = avg # Hardcodes 42.5
# Good: Let Excel calculate the sum
sheet['B10'] = '=SUM(B2:B9)'
# Good: Growth rate as Excel formula
sheet['C5'] = '=(C4-C2)/C2'
# Good: Average using Excel function
sheet['D20'] = '=AVERAGE(D2:D19)'
This applies to ALL calculations - totals, percentages, ratios, differences, etc. The spreadsheet should be able to recalculate when source data changes.
Choose tool : pandas for data, openpyxl for formulas/formatting
Create/Load : Create new workbook or load existing file
Modify : Add/edit data, formulas, and formatting
Save : Write to file
Recalculate formulas (MANDATORY IF USING FORMULAS) : Use the recalc.py script
python recalc.py output.xlsx
Verify and fix any errors :
status is errors_found, check error_summary for specific error types and locations#REF!: Invalid cell references#DIV/0!: Division by zero#VALUE!: Wrong data type in formula#NAME?: Unrecognized formula name# Using openpyxl for formulas and formatting
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment
wb = Workbook()
sheet = wb.active
# Add data
sheet['A1'] = 'Hello'
sheet['B1'] = 'World'
sheet.append(['Row', 'of', 'data'])
# Add formula
sheet['B2'] = '=SUM(A1:A10)'
# Formatting
sheet['A1'].font = Font(bold=True, color='FF0000')
sheet['A1'].fill = PatternFill('solid', start_color='FFFF00')
sheet['A1'].alignment = Alignment(horizontal='center')
# Column width
sheet.column_dimensions['A'].width = 20
wb.save('output.xlsx')
# Using openpyxl to preserve formulas and formatting
from openpyxl import load_workbook
# Load existing file
wb = load_workbook('existing.xlsx')
sheet = wb.active # or wb['SheetName'] for specific sheet
# Working with multiple sheets
for sheet_name in wb.sheetnames:
sheet = wb[sheet_name]
print(f"Sheet: {sheet_name}")
# Modify cells
sheet['A1'] = 'New Value'
sheet.insert_rows(2) # Insert row at position 2
sheet.delete_cols(3) # Delete column 3
# Add new sheet
new_sheet = wb.create_sheet('NewSheet')
new_sheet['A1'] = 'Data'
wb.save('modified.xlsx')
Excel files created or modified by openpyxl contain formulas as strings but not calculated values. Use the provided recalc.py script to recalculate formulas:
python recalc.py <excel_file> [timeout_seconds]
Example:
python recalc.py output.xlsx 30
The script:
Quick checks to ensure formulas work correctly:
pd.notna()/ in formulas (#DIV/0!)The script returns JSON with error details:
{
"status": "success", // or "errors_found"
"total_errors": 0, // Total error count
"total_formulas": 42, // Number of formulas in file
"error_summary": { // Only present if errors found
"#REF!": {
"count": 2,
"locations": ["Sheet1!B5", "Sheet1!C10"]
}
}
}
data_only=True to read calculated values: load_workbook('file.xlsx', data_only=True)data_only=True and saved, formulas are replaced with values and permanently lostread_only=True for reading or write_only=True for writingpd.read_excel('file.xlsx', dtype={'id': str})pd.read_excel('file.xlsx', usecols=['A', 'C', 'E'])pd.read_excel('file.xlsx', parse_dates=['date_column'])IMPORTANT : When generating Python code for Excel operations:
For Excel files themselves :
Weekly Installs
218
Source
First Seen
Mar 6, 2026
Security Audits
Installed on
claude-code136
opencode67
codex67
cursor63
gemini-cli56
github-copilot51
Excel财务建模规范与xlsx文件处理指南:专业格式、零错误公式与数据分析
42,900 周安装