xlsx
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseXLSX creation, editing, and analysis
XLSX文件的创建、编辑与分析
| Task | Approach |
|---|---|
| Create or edit with formulas/formatting | |
| Bulk data in or out | |
| Quick look at a sheet | |
| Read a model (formulas and values) | two |
,openpyxl, andpandasare preinstalled — do not runmarkitdownfirst; write the script and import directly. Only if an import fails (or thepip installcommand is missing):markitdownthe missing package.pip install
Script paths below are relative to this skill's directory.
| 任务 | 处理方式 |
|---|---|
| 使用公式/格式创建或编辑 | |
| 批量数据导入/导出 | |
| 快速查看工作表内容 | |
| 读取模型(包含公式和数值) | 两次调用 |
、openpyxl和pandas已预安装 — 无需先执行markitdown;直接编写脚本并导入即可。仅当导入失败(或pip install命令缺失)时,再执行markitdown安装缺失的包。pip install
下方脚本路径均相对于本Skill的目录。
Requirements for every output
所有输出的要求
- Professional font (Arial, Times New Roman) throughout, unless the user says otherwise.
- Zero formula errors. Never ship while reports
recalc.py. If you think an error predates you, prove it: load the original witherrors_foundand look at that cell. An error you introduced looks exactly like one you inherited.data_only=True - Use formulas, never hardcoded results. Write , not the Python-computed total. The sheet must recalculate when its inputs change.
sheet['B10'] = '=SUM(B2:B9)' - 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 (); when the number came from the user, say so plainly.
Source: Company 10-K, FY2024, Page 45, Revenue Note, [SEC EDGAR URL] - 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.
- 全程使用专业字体(Arial、Times New Roman),除非用户另有说明。
- 零公式错误。当报告
recalc.py时,绝不能交付文件。若你认为错误是原有文件自带的,请验证:使用errors_found加载原始文件并查看对应单元格。你引入的错误与继承的错误表现完全一致。data_only=True - 使用公式,而非硬编码结果。应编写,而非Python计算得出的总计值。表格必须能在输入内容改变时重新计算。
sheet['B10'] = '=SUM(B2:B9)' - 严格遵循用户的规格要求。使用精确的工作表名称、列标题以及用户指定的公式。无论设计多么优雅,若计算结果与要求不符,即为失败。
- 记录所有假设和硬编码数值,且需让读者可见——可添加单元格批注,或在表格末尾的相邻单元格中注明。若数值来自真实来源,请引用(例如);若数值来自用户,则直接说明。
来源:公司2024财年10-K报告,第45页,收入说明,[SEC EDGAR URL] - 为他人创建的待填写工作簿需包含简短图例,标注可编辑的单元格,并添加一行符合预期格式的真实示例数据。请勿在用户要求编辑的文件中添加此类示例行。
- 编辑现有文件:完全匹配其既有规范。这些规范优先于本指南中的所有准则。首先找到文件中指定的输入单元格——通常通过独特的字体颜色、填充色或阴影标记——仅在这些单元格中写入内容,保留所有现有公式不变。
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 to anything reading cached values — ,
, and most previewers.
Nonepandasload_workbook(data_only=True)bash
python scripts/recalc.py output.xlsx [timeout_seconds] # default 30LibreOffice computes every formula, the file is rewritten in place, and you get JSON:
( | ), , , and an
naming up to 100 cells per error type ( says how many it
withheld — trust , not the length of the list). Fix what it names and run it
again. JSON with an key instead of a means nothing was recalculated, and
only that case exits non-zero — exits 0, so never treat a clean exit as a clean
workbook.
statussuccesserrors_foundtotal_formulastotal_errorserror_summarylocations_truncatedtotal_errorserrorstatuserrors_foundA 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 — the 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 , and deletes every link. refuses to run in that state
— copy those cells' values out of the original before you save over them ( overrides,
and accepts the loss).
='[1]Returns Analysis'!$B$2[1]#NAME?recalc.py--forceopenpyxl将公式以字符串形式写入,不包含缓存值。在重新计算之前,所有公式单元格在读取缓存值的工具(如、以及大多数预览工具)中都会被读取为。
pandasload_workbook(data_only=True)Nonebash
python scripts/recalc.py output.xlsx [timeout_seconds] # 默认超时时间30秒LibreOffice会计算所有公式,文件将原地重写,并返回JSON结果:包含(或)、、以及(每种错误类型最多列出100个单元格位置,表示被省略的数量——请以为准,而非列表长度)。修复列出的错误后再次运行该脚本。若返回的JSON包含键而非,则表示未执行任何重新计算,只有这种情况会返回非零退出码——会返回0,因此绝不能将干净的退出状态视为工作簿无问题。
statussuccesserrors_foundtotal_formulastotal_errorserror_summarylocations_truncatedtotal_errorserrorstatuserrors_found重新计算通过仅证明公式可被求值,不代表公式正确。范围差一位或引用错误行都会生成无错误但数值错误的文件。在构建完整表格之前,先编写2-3个公式并验证其是否能获取预期值。
包含外部文件链接的工作簿,若使用openpyxl重新保存后再执行重新计算,会丢失这些链接。此类公式格式为——其中是工作簿外部引用列表中的索引,指向磁盘上的另一个文件,而非工作表。该文件通常不在当前环境中,因此单元格的缓存值是唯一保留数据的方式。openpyxl在保存时会清除该值;LibreOffice尝试解析引用失败后,会写入并删除所有链接。此时会拒绝运行——在覆盖保存之前,请先将原始文件中这些单元格的值复制出来(参数可强制运行,但会接受数据丢失)。
='[1]Returns Analysis'!$B$2[1]#NAME?recalc.py--forceChoosing formulas that survive verification
选择可通过验证的公式
LibreOffice implements fewer functions than Excel, and one it cannot evaluate becomes a
literal baked into the file you deliver.
#NAME?- Prefer Excel-2007-era functions — ,
SUMIFS,INDEX,MATCH,IFERROR— which need no prefix.SUMPRODUCT - Six post-2007 functions work, but only with an prefix, because openpyxl writes your formula into the XML verbatim and Excel stores post-2007 names prefixed (its UI hides the prefix):
_xlfn.,_xlfn.TEXTJOIN,_xlfn.CONCAT,_xlfn.IFS,_xlfn.SWITCH,_xlfn.MAXIFS. Written bare, each yields_xlfn.MINIFS.#NAME? - Never use ,
XLOOKUP,XMATCH,SORT,FILTER, orUNIQUE. 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 — andSEQUENCEreportsrecalc.pyon the truncated result. Usetotal_errors: 0/INDEXfor lookups, and sort, filter, and de-duplicate in Python before writing the cells.MATCH - A formula LibreOffice could not parse is written back lowercased — a quick tell beside a .
#NAME?
LibreOffice支持的函数少于Excel,无法求值的函数会在交付的文件中显示为。
#NAME?- 优先使用Excel 2007时代的函数——、
SUMIFS、INDEX、MATCH、IFERROR——这些函数无需前缀。SUMPRODUCT - 6个2007年后的函数可使用,但必须添加前缀,因为openpyxl会将公式原样写入XML,而Excel会为2007年后的函数名称添加前缀(UI会隐藏该前缀):
_xlfn.、_xlfn.TEXTJOIN、_xlfn.CONCAT、_xlfn.IFS、_xlfn.SWITCH、_xlfn.MAXIFS。若不添加前缀,这些函数都会显示为_xlfn.MINIFS。#NAME? - 切勿使用、
XLOOKUP、XMATCH、SORT、FILTER或UNIQUE。当前环境的LibreOffice无法通过任何前缀求值这些函数。新版本虽然能求值,但它们是溢出数组函数,而openpyxl生成的文件没有溢出元数据,因此只有范围左上角的单元格会获得值——且SEQUENCE会报告recalc.py,导致结果被截断。请使用total_errors: 0/INDEX实现查找功能,并在写入单元格前用Python完成排序、过滤和去重操作。MATCH - LibreOffice无法解析的公式会被小写返回——这是除之外的快速判断依据。
#NAME?
openpyxl gotchas
openpyxl常见陷阱
- Reading a model takes two loads. yields cached values with the formulas gone; the default yields formula strings with no values. One pass cannot give you both.
data_only=True - is destructive if you save. That workbook has no formulas left, so saving replaces every one with a literal — permanently.
data_only=True - on a file openpyxl just wrote returns
data_only=Trueeverywhere — runNonefirst. (A formula whose result isrecalc.pyalso reads back as"".)None - Merged cells: write the top-left anchor only. Every other cell in the range is a whose
MergedCellis read-only..value - loses its macros unless you pass
.xlsmtokeep_vba=True.load_workbook - A sheet name containing a space must be quoted in a cross-sheet reference: . Unquoted, it evaluates to
='Assumptions Inputs'!$B$5.#VALUE!
- 读取模型需要两次加载。会返回缓存值,但公式会丢失;默认方式会返回公式字符串,但无数值。单次加载无法同时获取两者。
data_only=True - 若保存时使用会造成破坏性影响。该工作簿将不再包含公式,保存后所有公式都会被替换为字面量——且无法恢复。
data_only=True - 对openpyxl刚写入的文件使用加载,所有单元格都会返回
data_only=True——请先运行None。(结果为recalc.py的公式也会被读取为""。)None - 合并单元格:仅写入左上角锚点单元格。合并范围中的其他单元格均为,其
MergedCell属性为只读。.value - 文件会丢失宏,除非在
.xlsm中传入load_workbook参数。keep_vba=True - 包含空格的工作表名称在跨表引用中必须加引号:例如。若不加引号,会求值为
='Assumptions Inputs'!$B$5。#VALUE!
Financial models
财务模型规范
Unless the user says otherwise, or the existing file already does something else.
Color: blue text () for hardcoded inputs and scenario levers · black for formulas ·
green () for links to another sheet · red () for links to another file ·
yellow fill () for key assumptions and cells the user should fill in.
0,0,2550,128,0255,0,0255,255,0Numbers: currency , with the unit named in the header () · zeros
render as , including in percentages () · negatives in parentheses ·
percentages , stored as fractions ( renders ; storing renders
) · valuation multiples · years as text (, never ).
$#,##0Revenue ($mm)-$#,##0;($#,##0);-0.0%0.1515.0%151500.0%0.0x"2024"2,024Structure: every assumption in its own labeled cell, referenced by the formulas that use it
(, never ) · formulas consistent across every projection period, since a
lone edited cell mid-row is the commonest silent error · guard denominators that can be zero.
=B5*(1+$B$6)=B5*1.05除非用户另有说明,或现有文件已采用其他规范。
颜色规则:硬编码输入和场景控制项使用蓝色文本()· 公式使用黑色文本 · 跨工作表链接使用绿色文本()· 跨文件链接使用红色文本()· 关键假设和用户需填写的单元格使用黄色填充()。
0,0,2550,128,0255,0,0255,255,0数字格式:货币使用格式,单位在表头中标注(例如)· 零值显示为,百分比格式也遵循此规则()· 负值用括号包裹 · 百分比使用格式,以分数形式存储(显示为;若存储为则显示为)· 估值倍数使用格式 · 年份以文本形式存储(,绝不能是)。
$#,##0Revenue ($mm)-$#,##0;($#,##0);-0.0%0.1515.0%151500.0%0.0x"2024"2,024结构规则:每个假设都放在单独的带标签单元格中,公式通过引用该单元格使用假设值(例如,绝不能是)· 所有预测周期的公式保持一致,因为行中单独编辑的单元格是最常见的隐性错误 · 对可能为零的分母添加防护逻辑。
=B5*(1+$B$6)=B5*1.05Dependencies
依赖项
openpyxlpandasmarkitdownsofficescripts/office/soffice.pyopenpyxlpandasmarkitdownsofficescripts/office/soffice.py