Skip to content
skillxone

CherryHQ/cherry-studio / resources/skills/office-transform

office-transform

Verified

Extract or edit a selection from xlsx, docx, pdf, or pptx into a new file; the source is never modified.

scripts/office_patch_copy.py

View on GitHub
#!/usr/bin/env python3
"""Derive a new .xlsx/.docx by copying the original package and rewriting only
the XML parts the requested edits touch.

OOXML files are ZIP packages of XML parts. This script copies every part of
the original byte-for-byte and re-serializes only what an edit reaches: the
targeted part (one worksheet, or word/document.xml) and, for xlsx, the workbook
bookkeeping named below. Fidelity risk is confined to those.
The touched part is manipulated with xml.dom.minidom, which round-trips
namespace prefixes and declarations verbatim (unlike ElementTree, which
rewrites unknown prefixes and breaks mc:Ignorable references). The source
file is never modified. Standard library only — no dependencies.

Edit JSON shapes (pass via --edits):

    {"format": "xlsx", "sheet": "Sheet1", "cells": {"B2": 42, "C3": "hello", "D4": true}}
    {"format": "docx", "replacements": [{"paragraph": 3, "text": "new text",
                                          "paraId": "502E8D33", "expectText": "old text"}]}

xlsx: each cell is overwritten with the JSON value (number, string, or
boolean); an ordinary formula in that cell is replaced by the value, while a
cell belonging to a shared, array, or data-table formula group is refused (see
reject_shared_formula and grouped_formula_ranges). The worksheet's <dimension>
is widened when edits create cells outside it. Any write also sets fullCalcOnLoad
on xl/workbook.xml (see request_full_recalc), and replacing a formula additionally
drops xl/calcChain.xml (see drop_calc_chain); those, plus [Content_Types].xml /
workbook.xml.rels, are the only parts besides the edited worksheet this script
ever rewrites.
docx: 'paragraph' is the zero-based ordinal among BODY-LEVEL paragraphs
(direct w:body children; tables excluded). Optional 'paraId' (w14:paraId) is
resolved first when present; a paraId that resolves to a different paragraph
than the ordinal is an error, never a silent pick. Optional 'expectText' is a
hard gate: the target paragraph's current text (whitespace-normalized) must
equal it or the edit is refused — take its value from a prior extract of the
anchor, not from a selection-ref excerpt. The paragraph keeps its paragraph
style and the first run's character style, and extra run-level styling is
flattened into the new text. A paragraph holding anything the output shape
cannot carry is refused rather than silently stripped — see
reject_unrepresentable_content, which allow-lists what survives instead of
enumerating what is dangerous.

The output is written to a staging file and renamed on success, so a failure
never leaves a partial package behind (see atomic_output).
"""

import argparse
import sys

# SkillInstaller verifies built-in skills by directory hash; a __pycache__ dir would mismatch it and get the skill unlinked.
# The sibling imports below would create one, so this must run before them.
sys.dont_write_bytecode = True

import zipfile
from pathlib import Path

from office import docx, xlsx
from office.common import (
    atomic_output,
    fail,
    parse_json_object,
    preflight_zip,
    validate_io_paths,
)


PATCHERS = {
    "xlsx": xlsx.patch_xlsx,
    "docx": docx.patch_docx,
}


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--file", required=True, help="absolute path of the source document (read-only)")
    parser.add_argument("--edits", required=True, help="edit JSON — see module docstring for shapes")
    parser.add_argument("--out", required=True, help="absolute path of the NEW file to create; must not exist")
    args = parser.parse_args()

    src = Path(args.file)
    out_path = Path(args.out)
    validate_io_paths(src, out_path)

    edits = parse_json_object(args.edits, "edits")
    edits_format = edits.get("format")
    patcher = PATCHERS.get(edits_format) if isinstance(edits_format, str) else None
    if patcher is None:
        fail(f"unsupported edits format: {edits_format!r} (use xlsx or docx)")

    out_path.parent.mkdir(parents=True, exist_ok=True)
    # A package written in place and interrupted mid-copy stays readable with the edit applied while
    # silently missing the parts never copied, and then blocks the retry as an existing output path.
    with atomic_output(out_path) as staging:
        with zipfile.ZipFile(src) as archive:
            preflight_zip(archive)
            replaced_parts, dropped_parts = patcher(archive, edits)
            with zipfile.ZipFile(staging, "w") as derived:
                for item in archive.infolist():
                    if item.filename in dropped_parts:
                        continue
                    data = replaced_parts.get(item.filename, None)
                    if data is None:
                        data = archive.read(item.filename)
                    derived.writestr(item, data, compress_type=item.compress_type)
    print(str(out_path))


if __name__ == "__main__":
    main()
canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations.Stars: 195kUpdated Claude CodeCodexVerifiedInstall
docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation.Stars: 195kUpdated Claude CodeCodexVerifiedInstall
pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill.Stars: 195kUpdated Claude CodeCodexVerifiedInstall
pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill.Stars: 195kUpdated Claude CodeCodexVerifiedInstall