xlsx
anthropics/skills
每当电子表格文件作为主要输入或输出时,均可使用此技能。 这意味着任何用户希望:打开、读取、编辑或修复现有 .xlsx、.xlsm、.xltx、.csv 或 .tsv 文件的任务(例如,添加列、计算公式、设置格式、制作图表、清理杂乱数据); 从头开始创建新的电子表格,或基于其他数据源创建电子表格;或在表格文件格式之间进行转换。当用户通过名称或路径引用电子表格文件时(即使是随口一提,例如“我下载文件夹里的那个 xlsx 文件”),且希望对该文件进行操作或生成输出时,应特别触发此技能。 此外,当需要清理或重组杂乱的表格数据文件(如格式错误的行、位置错误的表头、垃圾数据)以生成规范的电子表格时,也应触发此任务。最终交付物必须是电子表格文件。 当主要交付成果是 Word 文档、HTML 报告、独立的 Python 脚本、数据库管道或 Google 表格 API 集成时,即使涉及表格数据,也请勿触发此任务。
...展开全部XLSX 创建、编辑和分析
openpyxl、pandas和markitdown已预装——请勿先运行pip install;直接编写脚本并导入即可。仅当导入失败(或缺少markitdown命令)时,才需使用pip install 安装缺失的包。
下文中的脚本路径均为相对于本技能目录的相对路径。
所有输出的要求
- 除非用户另有说明,否则全文均应使用专业字体(Arial、Times New Roman)。
- 公式错误为零。只要
recalc.py报告errors_found,绝不发布。若认为错误并非由你引入,请提供证明:使用data_only=True加载原始文件并检查该单元格。你引入的错误与继承的错误完全相同。 - 使用公式,切勿硬编码结果。应编写
sheet['B10'] = '=SUM(B2:B9)',而非直接使用 Python 计算出的总和。当输入数据发生变化时,工作表必须重新计算。 - 严格遵循用户的需求规格说明。精确的标签名称、精确的列标题,以及用户明确指定的公式。即使再优雅,如果重新设计后计算了其他内容,也算失败。
- 将每个假设和硬编码的数值记录在读者能看到的地方——例如单元格注释,或表格末尾的相邻单元格。 如有真实来源,请注明(
来源:公司 10-K 报告,2024 财年,第 45 页,收入说明,[SEC EDGAR 网址]);若数据来自用户,请明确说明。 - 为他人创建的待填写工作簿,需附带简短说明,标明哪些单元格需要编辑,并提供一行包含实际值的示例行,以展示预期格式。切勿在受托编辑的文件中添加此类示例行。
- 编辑现有文件:必须完全遵循其既定规范。这些规范优先于本文中的所有指南。首先找出指定的输入单元格——通常通过独特的字体颜色、填充色或阴影进行标记——仅在这些单元格中输入数据,并保持所有现有公式不变。
重新计算(只要文件包含公式,此步骤必不可少)
openpyxl 将公式写入为字符串,且不缓存值。在您重新计算之前,对于读取缓存值的任何工具(如pandas、
load_workbook(data_only=True) 以及大多数预览器),每个
公式单元格都会被读取为None。
python scripts/recalc.py 输出。xlsx [timeout_seconds] # 默认 30
LibreOffice 会计算每个公式,文件将就地重写,您将获得 JSON 数据:
status(success|errors_found)、total_formulas、total_errors,以及一个
error_summary,其中列出了每种错误类型最多 100 个单元格(locations_truncated表示被
截断的数量——请相信total_errors 的数值,而非列表的长度)。 修正它指出的问题并再次运行。
如果 JSON 中出现error键而非status 键,则表示未进行任何重新计算,
且只有这种情况会返回非零值——errors_found返回 0,因此切勿将正常退出视为
工作簿无误。
绿色重算结果仅证明公式能正常计算,并不意味着结果正确。范围偏移一个单元格 或引用了错误的行,都会生成一个看似干净、无错误但数值错误的文件。 在构建整个表格之前,请先编写 2–3 个公式,并检查它们是否能返回预期的值。
如果一个链接到其他文件的的工作簿使用 openpyxl 重新保存后
再进行重新计算,这些链接将会丢失。 此类公式的写法为='[1]Returns Analysis'!$B$2—— 其中的[1]是
工作簿外部引用列表中的索引,指代磁盘上的一个独立文件,而非工作表。
该文件在此处很少存在,因此单元格的缓存值是保存其
数据的唯一途径。openpyxl 在保存时会清除该值;随后 LibreOffice 必须
实际解析该引用,但会失败,写入#NAME? 错误,并删除所有链接。recalc.py在这种状态下拒绝运行
——在覆盖保存之前,请先将这些单元格的值从原始文件中复制出来(--force选项可强制覆盖,
并接受数据丢失)。
选择能通过验证的公式
LibreOffice 实现的函数比 Excel 少,而它无法求值的函数会变成
一个硬编码的#NAME?错误,并被永久写入您交付的文件中。
- 建议优先使用 Excel 2007 时代的
函数——SUMIFS、INDEX、MATCH、IFERROR、SUMPRODUCT——这些函数无需前缀。 - 有六个 2007 年后的函数可以使用,但必须加上
_xlfn.前缀,因为 openpyxl 会将公式原样写入 XML,而 Excel 存储 2007 年后的函数名称时会添加前缀(其用户界面会隐藏该前缀):_xlfn.TEXTJOIN、_xlfn.CONCAT、_xlfn.IFS、_xlfn.SWITCH、_xlfn.MAXIFS、_xlfn.MINIFS。若直接使用,每个都会返回#NAME?错误。 - 切勿使用
XLOOKUP、XMATCH、SORT、FILTER、UNIQUE或SEQUENCE。运行时的 LibreOffice 无论使用何种前缀都无法对其进行求值。 较新的版本虽然可以评估这些函数,但它们属于溢出数组函数,而由 openpyxl 编写的文件不包含溢出元数据,因此只有范围的左上角单元格会获得值——且recalc.py报告截断结果的total_errors 为 0。请使用INDEX/MATCH进行查找,并在写入单元格之前使用 Python 进行排序、筛选和去重。 - LibreOffice 无法解析的公式在写回时会被转换为小写——这是除
#NAME?错误外的一个快速识别标志。
openpyxl 的注意事项
- 读取模型需要进行两次加载。
设置 data_only=True会返回已缓存的值但公式已丢失;默认设置则会返回不含值的公式字符串。一次操作无法同时获得这两种结果。 - 如果保存文件
,data_only=True会导致数据丢失。该工作簿中将不再包含任何公式,因此保存操作会将所有公式永久替换为字面量。 - 在 openpyxl 刚写入的文件上
设置 data_only=True会导致所有位置返回None—— 请先运行recalc.py。(结果为""的公式在读取时也会返回None。) - 合并单元格:仅写入左上角锚点。该范围内的其他所有单元格均为
MergedCell,其.value属性为只读。 - 除非在
load_workbook函数中传入keep_vba=True,否则.xlsm 文件将丢失其宏。 - 跨工作表引用中,若工作表名称包含空格,必须加引号:
='Assumptions Inputs'!$B$5。若不加引号,将计算结果为#VALUE!。
财务模型
除非用户另有说明,或现有文件已有其他处理方式。
颜色:硬编码输入和情景杠杆采用蓝色文本 (0,0,255) · 公式采用黑色 ·
链接到其他工作表的单元格为绿色 (0,128,0) · 链接到其他文件的单元格为红色 (255,0,0) ·
关键假设及用户需填写的单元格采用黄色填充 (255,255,0)。
数字:货币格式$#,##0,单位名称显示在表头(收入 ($mm))· 零
显示为-,百分比中亦如此 ($#,##0;($#,##0);-) · 负数用括号表示 ·
百分比0.0%, 以分数形式存储(0.15显示为15.0%;存储15则显示为
1500.0%) · 估值倍数0.0x· 年份以文本形式显示(“2024”,绝不显示为2,024)。
结构:每个假设都位于单独的标有标签的单元格中,由使用该假设的公式引用
(=B5*(1+$B$6),绝不使用=B5*1.05) · 所有预测期间的公式保持一致,因为
行中单个单元格的编辑是最常见的隐性错误 · 保护可能为零的分母。
依赖项
openpyxl、pandas、markitdown(pip,预安装——仅在导入失败或缺少命令时才需安装)· LibreOffice(soffice,通过scripts/office/soffice.py 自动配置为沙箱环境)
XLSX creation, editing, and analysis
openpyxl,pandas, andmarkitdownare preinstalled — do not runpip installfirst; write the script and import directly. Only if an import fails (or themarkitdowncommand is missing):pip installthe missing package.
Script paths below are relative to this skill's directory.
Requirements for every output
- Professional font (Arial, Times New Roman) throughout, unless the user says otherwise.
- Zero formula errors. Never ship while
recalc.pyreportserrors_found. If you think an error predates you, prove it: load the original withdata_only=Trueand look at that cell. An error you introduced looks exactly like one you inherited. - Use formulas, never hardcoded results. Write
sheet['B10'] = '=SUM(B2:B9)', not the Python-computed total. The sheet must recalculate when its inputs change. - Follow the user's spec literally. Exact tab names, exact column headers, and the formula they spelled out. A redesign that computes something else fails, however elegant.
- Document every assumption and hardcoded number where the reader will see it — a cell comment, or an adjacent cell at a table's end. Cite a real source when one exists (
Source: Company 10-K, FY2024, Page 45, Revenue Note, [SEC EDGAR URL]); when the number came from the user, say so plainly. - A workbook you create for someone to fill in needs a short legend naming which cells to edit, and one example row of realistic values showing the expected format. Never add such a row to a file you were asked to edit.
- Editing an existing file: match its conventions exactly. They override every guideline here. Find its designated input cells first — a distinct font color, fill, or shading marks them — write only there, and leave every existing formula untouched.
Recalculate (mandatory whenever the file contains formulas)
openpyxl writes formulas as strings with no cached values. Until you recalculate, every
formula cell reads back as None to anything reading cached values — pandas,
load_workbook(data_only=True), and most previewers.
python scripts/recalc.py output.xlsx [timeout_seconds] # default 30
LibreOffice computes every formula, the file is rewritten in place, and you get JSON:
status (success | errors_found), total_formulas, total_errors, and an
error_summary naming up to 100 cells per error type (locations_truncated says how many it
withheld — trust total_errors, not the length of the list). Fix what it names and run it
again. JSON with an error key instead of a status means nothing was recalculated, and
only that case exits non-zero — errors_found exits 0, so never treat a clean exit as a clean
workbook.
A green recalc proves your formulas evaluate, not that they are right. An off-by-one range or a reference to the wrong row yields a clean, error-free file with wrong numbers. Write 2–3 formulas first and check they pull the values you expect, before building out a grid.
A workbook that links to another file loses those links if you re-save it with openpyxl and
then recalculate. Such a formula reads ='[1]Returns Analysis'!$B$2 — the [1] is an index
into the workbook's external-reference list, naming a separate file on disk, not a sheet.
That file is rarely present here, so the cell's cached value is the only thing holding its
data. openpyxl strips that value on save; LibreOffice then has to resolve the reference for
real, fails, writes #NAME?, and deletes every link. recalc.py refuses to run in that state
— copy those cells' values out of the original before you save over them (--force overrides,
and accepts the loss).
Choosing formulas that survive verification
LibreOffice implements fewer functions than Excel, and one it cannot evaluate becomes a
literal #NAME? baked into the file you deliver.
- Prefer Excel-2007-era functions —
SUMIFS,INDEX,MATCH,IFERROR,SUMPRODUCT— which need no prefix. - Six post-2007 functions work, but only with an
_xlfn.prefix, because openpyxl writes your formula into the XML verbatim and Excel stores post-2007 names prefixed (its UI hides the prefix):_xlfn.TEXTJOIN,_xlfn.CONCAT,_xlfn.IFS,_xlfn.SWITCH,_xlfn.MAXIFS,_xlfn.MINIFS. Written bare, each yields#NAME?. - Never use
XLOOKUP,XMATCH,SORT,FILTER,UNIQUE, orSEQUENCE. The runtime's LibreOffice cannot evaluate them under any prefix. Newer builds do evaluate them, but they are spilling array functions and an openpyxl-written file has no spill metadata, so only the top-left cell of the range gets a value — andrecalc.pyreportstotal_errors: 0on the truncated result. UseINDEX/MATCHfor lookups, and sort, filter, and de-duplicate in Python before writing the cells. - A formula LibreOffice could not parse is written back lowercased — a quick tell beside a
#NAME?.
openpyxl gotchas
- Reading a model takes two loads.
data_only=Trueyields cached values with the formulas gone; the default yields formula strings with no values. One pass cannot give you both. data_only=Trueis destructive if you save. That workbook has no formulas left, so saving replaces every one with a literal — permanently.data_only=Trueon a file openpyxl just wrote returnsNoneeverywhere — runrecalc.pyfirst. (A formula whose result is""also reads back asNone.)- Merged cells: write the top-left anchor only. Every other cell in the range is a
MergedCellwhose.valueis read-only. .xlsmloses its macros unless you passkeep_vba=Truetoload_workbook.- A sheet name containing a space must be quoted in a cross-sheet reference:
='Assumptions Inputs'!$B$5. Unquoted, it evaluates to#VALUE!.
Financial models
Unless the user says otherwise, or the existing file already does something else.
Color: blue text (0,0,255) for hardcoded inputs and scenario levers · black for formulas ·
green (0,128,0) for links to another sheet · red (255,0,0) for links to another file ·
yellow fill (255,255,0) for key assumptions and cells the user should fill in.
Numbers: currency $#,##0, with the unit named in the header (Revenue ($mm)) · zeros
render as -, including in percentages ($#,##0;($#,##0);-) · negatives in parentheses ·
percentages 0.0%, stored as fractions (0.15 renders 15.0%; storing 15 renders
1500.0%) · valuation multiples 0.0x · years as text ("2024", never 2,024).
Structure: every assumption in its own labeled cell, referenced by the formulas that use it
(=B5*(1+$B$6), never =B5*1.05) · formulas consistent across every projection period, since a
lone edited cell mid-row is the commonest silent error · guard denominators that can be zero.
Dependencies
openpyxl, pandas, markitdown (pip, preinstalled — install only if an import fails or the command is missing) · LibreOffice (soffice, auto-configured for sandboxed environments via scripts/office/soffice.py)





首页
