fix(office): 修复独立技能的 Python 依赖入口 (#229)

为 pptx 与 xlsx 提供标准库启动器,使用已准备的受管 XML 依赖,保留目标退出码并避免失败后重放编辑。

同步双语说明、目录版本和固定启动器合同测试。

Refs desirecore/desirecore#5335
This commit is contained in:
yige authored and GitHub committed 2026-10-28 10:24:31 +08:00
1 parent aa6a1a6348
commit 1b9e82d95d
12 files changed
+462 -38

No files matched your search

+17 -4
View File
@@ -16,7 +16,7 @@ description: >-
report, standalone Python script, database pipeline, or Google Sheets API
integration, even if tabular data is involved. Use when 用户提到 Excel、
电子表格、xlsx、表格处理、公式计算、数据清洗、图表、CSV导入导出。
version: 1.0.4
version: 1.0.5
type: procedural
risk_level: low
status: enabled
@@ -28,7 +28,7 @@ tags:
- office
metadata:
author: anthropic
updated_at: '2026-10-14'
updated_at: '2026-10-28'
i18n:
default_locale: en-US
source_locale: zh-CN
@@ -41,7 +41,7 @@ metadata:
description: >-
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. Use when 用户提到 Excel、 电子表格、xlsx、表格处理、公式计算、数据清洗、图表、CSV导入导出。
body: ./SKILL.zh-CN.md
source_hash: sha256:17c76a78ed03d451
source_hash: sha256:6cd6c553d8340840
translated_by: human
en-US:
name: Spreadsheet Processing
@@ -49,7 +49,7 @@ metadata:
description: >-
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. Use when the user mentions Excel, spreadsheets, xlsx, table processing, formula computation, data cleaning, charts, or CSV import/export.
body: ./SKILL.md
source_hash: sha256:e399420dae6ea7b4
source_hash: sha256:6cd6c553d8340840
translated_by: human
market:
icon: >-
@@ -106,6 +106,17 @@ python "<skill-dir>/scripts/recalc.py" output.xlsx 30
**NEVER** run `python scripts/recalc.py` directly — that relative path does not exist in the user's working directory.
For low-level Office XML unpacking/packing, use this skill's own cross-platform launcher:
```bash
python "<skill-dir>/scripts/with-deps.py" office/unpack.py workbook.xlsx unpacked/
python "<skill-dir>/scripts/with-deps.py" office/pack.py unpacked/ output.xlsx
```
The launcher loads `defusedxml` and `lxml` from the client's prepared `runtime-deps/python-libs` and prefers its `runtime-deps/python-runtime` interpreter. Targets are relative to `scripts/` or absolute; workdir, arguments and failure exit codes are preserved. It installs nothing, requires no docx/pptx skill, and suppresses child bytecode caches. If the prepared interpreter cannot start, the current Python must be 3.10+; missing dependencies fail rather than pretending validation succeeded.
This does **not** add full XLSX XSD validation: `office/validate.py` currently rejects `.xlsx` as unsupported. Packing success alone is not complete validation. Reopen the output with the actual spreadsheet library, check required cells/formulas/styles, and run recalculation as appropriate. Report the unsupported validation boundary. Keep `recalc.py` on the Python where openpyxl is installed; the Office XML profile does not install openpyxl or pandas.
## Prerequisites
### Python 3 (required)
@@ -128,6 +139,8 @@ for everything else (LibreOffice / containers / WSL), load the `dev-environment-
### Python Package Dependencies
The launcher consumes the client's preinstalled `defusedxml`/`lxml` for Office XML scripts without `pip install`. Restore the prepared runtime if those libraries are unavailable. These shared libraries do not provide the openpyxl/pandas dependencies below or full XLSX validation.
This Skill depends on the following Python packages (checked on demand):
- `openpyxl` — Excel file creation and editing (formulas, formatting)
+14 -1
View File
@@ -43,6 +43,8 @@ python3 --version 2>/dev/null || python --version 2>/dev/null
### Python 包依赖
Office XML 脚本经启动器消费客户端运行时预装的 `defusedxml`/`lxml`,无需 `pip install`;缺失时修复客户端已准备的运行时。这些共享库不提供下方的 openpyxl/pandas,也不代表已实现 XLSX 完整校验。
本技能依赖以下 Python 包(按需检测):
- `openpyxl` — Excel 文件创建和编辑(公式、格式)
@@ -70,6 +72,17 @@ python "<skill-dir>/scripts/recalc.py" output.xlsx 30
**禁止**直接执行 `python scripts/recalc.py`——该相对路径在用户工作目录下不存在。
如需低层 Office XML 解包/打包,使用本技能自带的跨平台启动器:
```bash
python "<skill-dir>/scripts/with-deps.py" office/unpack.py workbook.xlsx unpacked/
python "<skill-dir>/scripts/with-deps.py" office/pack.py unpacked/ output.xlsx
```
启动器从客户端已准备的 `runtime-deps/python-libs` 加载 `defusedxml` 与 `lxml`,优先使用 `runtime-deps/python-runtime` 解释器。目标路径相对 `scripts/`,也接受绝对路径;工作目录、参数和失败退出码保持不变。它不安装任何包,不依赖 docx/pptx 技能,并抑制子脚本字节码缓存。受控解释器不能启动时,当前 Python 必须为 3.10+;缺依赖时明确失败,不伪装成校验成功。
这**没有**增加 XLSX 完整 XSD 校验:`office/validate.py` 目前对 `.xlsx` 明确返回不支持。打包成功不等于完整校验,应使用实际表格库重新打开,核对所需单元格、公式与样式,并按需重算;交付时说明尚不支持完整校验。`recalc.py` 继续使用装有 openpyxl 的 Python,Office XML profile 不会安装 openpyxl 或 pandas。
## Output Rule
When you create or modify a .xlsx file, you **MUST** tell the user the absolute path of the output file in your response. Example: "文件已保存到:`/path/to/output.xlsx`"
@@ -357,4 +370,4 @@ The script returns JSON with error details:
**For Excel files themselves**:
- Add comments to cells with complex formulas or important assumptions
- Document data sources for hardcoded values
- Include notes for key calculations and model sections
- Include notes for key calculations and model sections
+2 -2
View File
@@ -29,13 +29,13 @@
},
"release": {
"state": "known",
"version": "1.0.4",
"version": "1.0.5",
"versionScheme": "semver"
},
"timestamps": {
"catalogUpdatedAt": {
"state": "known",
"value": "2026-10-14",
"value": "2026-10-28",
"precision": "day"
},
"releasePublishedAt": {
+86
View File
@@ -0,0 +1,86 @@
#!/usr/bin/env python3
"""Run Office XML scripts with the client's prepared Python dependencies.
Each installed skill carries this launcher so it works without another Office
skill. The bootstrap uses only the standard library, including on Python 3.9;
the target requires Python 3.10+ and prefers the runtime's prepared interpreter.
No packages are installed and target failures are never reported as success.
python "<skill-dir>/scripts/with-deps.py" office/unpack.py input.pptx unpacked/
"""
import os
import subprocess
import sys
_HERE = os.path.dirname(os.path.abspath(__file__))
# scripts -> skill -> skills -> the effective DesireCore data root
_ROOT = os.path.abspath(os.path.join(_HERE, "..", "..", ".."))
_PYLIBS = os.path.join(_ROOT, "runtime-deps", "python-libs")
_BUNDLED = os.path.join(
_ROOT,
"runtime-deps",
"python-runtime",
"python.exe" if os.name == "nt" else os.path.join("bin", "python3"),
)
def _current_python_supported():
if sys.version_info[:2] >= (3, 10):
return True
sys.stderr.write(
"with-deps.py: Office XML scripts require Python 3.10+. "
"Prepare the managed Python runtime in DesireCore and retry.\n"
)
return False
def main():
arguments = list(sys.argv[1:])
current_python = bool(arguments and arguments[0] == "--current-python")
if current_python:
arguments.pop(0)
if not arguments:
sys.stderr.write("usage: with-deps.py [--current-python] <script.py> [args...]\n")
return 2
argument = arguments[0]
target = argument if os.path.isabs(argument) else os.path.join(_HERE, argument)
if not os.path.isfile(target):
sys.stderr.write("with-deps.py: target not found: " + target + "\n")
return 2
interpreter = sys.executable
if not current_python and os.path.isfile(_BUNDLED):
if os.path.realpath(_BUNDLED) != os.path.realpath(sys.executable):
interpreter = _BUNDLED
if interpreter == sys.executable and not _current_python_supported():
return 1
environment = dict(os.environ)
if os.path.isdir(_PYLIBS):
existing = environment.get("PYTHONPATH")
environment["PYTHONPATH"] = _PYLIBS + (os.pathsep + existing if existing else "")
# Running a skill must not change its installed content digest through .pyc.
environment["PYTHONDONTWRITEBYTECODE"] = "1"
arguments = ["-B", target, *arguments[1:]]
try:
return subprocess.run([interpreter, *arguments], env=environment).returncode
except OSError as error:
if interpreter == sys.executable:
sys.stderr.write("with-deps.py: could not start Python: " + str(error) + "\n")
return 126
# Fall back only when the process could not start. A target's nonzero
# exit or signal must never replay an edit that may have already run.
sys.stderr.write("with-deps.py: managed Python could not start; trying current Python.\n")
if not _current_python_supported():
return 1
try:
return subprocess.run([sys.executable, *arguments], env=environment).returncode
except OSError as fallback_error:
sys.stderr.write("with-deps.py: could not start Python: " + str(fallback_error) + "\n")
return 126
if __name__ == "__main__":
raise SystemExit(main())