Skip to content

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.

references/docx-edit.md

View on GitHub
# Edit docx — edit runs, never `Paragraph.text`

Routed here from **"Edit docx"** in `SKILL.md`, which patch-copy's refusal messages point at.
The two skill invariants still hold: the source file is read-only, and the derived file is
written to a path nothing occupies yet.

When patch-copy refuses a paragraph, the reason is that the paragraph holds structure a
single rebuilt run cannot carry. `python-docx` can preserve it, but **only if you edit runs
in place**. Assigning `paragraph.text = "..."` clears the paragraph and rebuilds one run,
destroying bookmarks, comment anchors, hyperlinks, images and run formatting — the same loss
patch-copy refused to inflict, minus the refusal.

Two traps make the naive loop wrong:

- `paragraph.runs` does **not** include runs inside a `w:hyperlink`, while `paragraph.text`
  does. Offsets computed against `.text` will not line up with `.runs`. Walk
  `iter_inner_content()` instead.
- `Run.text`'s setter keeps that run's `rPr`, but rewrites the run's content from the
  characters you give it. It can only spell back what a character stands for: a bare `w:br`
  or `w:tab` survives, while `w:br w:type="page"` and `w:noBreakHyphen` vanish and `w:ptab`
  returns as a plain `w:tab`. A touched run is checked for those before it is written.

```python
from docx.oxml.ns import qn

def inline_runs(paragraph):
    """Runs in document order, including those inside hyperlinks, so the concatenation of
    their text equals paragraph.text and character offsets line up."""
    runs = []
    for item in paragraph.iter_inner_content():   # python-docx >= 1.1
        runs.extend(item.runs) if hasattr(item, "runs") else runs.append(item)
    return runs

def rebuildable(run):
    """Whether Run.text's setter can put this run back. It rewrites the run from characters,
    so it restores only what a character spells: a bare w:br or w:tab. A page break, a column
    break, a w:ptab or a w:noBreakHyphen comes back as the plain kind or not at all, which
    changes the layout without changing the text — refuse instead."""
    for child in run._r:
        if child.tag in (qn("w:rPr"), qn("w:t")):
            continue
        if child.tag in (qn("w:br"), qn("w:tab")) and not child.attrib:
            continue
        return False
    return True

def replace_char_range(paragraph, start, end, new_text):
    """Replace paragraph.text[start:end] by editing run text only."""
    position, written = 0, False
    for run in inline_runs(paragraph):
        run_start, run_end = position, position + len(run.text)
        position = run_end
        if run_end <= start or run_start >= end:
            continue
        if not rebuildable(run):
            raise ValueError("run holds inline content the text setter cannot rebuild")
        head = run.text[: max(0, start - run_start)]
        tail = run.text[max(0, end - run_start) :] if end < run_end else ""
        run.text = head + ("" if written else new_text) + tail
        written = True
    if not written:
        raise ValueError("charRange did not intersect any run")

# `save(path)` overwrites whatever is there, and a library edit owes the caller the same
# no-overwrite guarantee the scripts give. "x" states it without a check that can race.
with open("/abs/report-updated.docx", "xb") as out:
    document.save(out)
```

Verify by reopening the derived file and checking that the structure you meant to keep is
still there — not just that the text reads correctly:

```python
from docx import Document
check = Document("/abs/report-updated.docx")
para = check.paragraphs[3]
assert para.text == expected_text
assert len(para.runs) == runs_before          # nothing collapsed
assert "bookmarkStart" in para._p.xml         # anchors intact, if the source had them
```
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