CherryHQ/cherry-studio / resources/skills/office-transform
office-transform
VerifiedExtract or edit a selection from xlsx, docx, pdf, or pptx into a new file; the source is never modified.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 18.0 KB |
| references/docx-edit.md | markdown | 3.7 KB |
| references/pptx-edit.md | markdown | 4.6 KB |
| scripts/office/__init__.py | script | 0 B |
| scripts/office/common.py | script | 7.6 KB |
| scripts/office/docx.py | script | 19.0 KB |
| scripts/office/ooxml.py | script | 7.1 KB |
| scripts/office/pdf.py | script | 1.8 KB |
| scripts/office/pptx.py | script | 4.4 KB |
| scripts/office/xlsx.py | script | 28.3 KB |
| scripts/office_extract.py | script | 2.6 KB |
| scripts/office_patch_copy.py | script | 4.9 KB |
references/pptx-edit.md
View on GitHub# Edit pptx — use python-pptx, saving to a new path
Routed here from **"Edit pptx"** in `SKILL.md`. The two skill invariants still hold: the
source deck is read-only, and the derived deck is written to a path nothing occupies yet.
pptx edits do not go through `office_patch_copy.py`. `python-pptx` keeps XML it does not
understand, so the parts you never touch round-trip intact. That guarantee covers the
document around your edit; it does **not** make any given API call lossless. Assigning
`.text` at paragraph or shape level rebuilds that subtree as one unformatted run, discarding
bold, size, colour and `a:hlinkClick` and orphaning the hyperlink relationship. Edit runs:
```python
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
def walk(shapes): # extraction recurses into groups, so editing must too — a flat
for shape in shapes: # `for s in slide.shapes` cannot reach a grouped shape_id
yield shape
if shape.shape_type == MSO_SHAPE_TYPE.GROUP:
yield from walk(shape.shapes)
from pptx.oxml.ns import qn
def replace_char_range(paragraph, start, end, new_text):
"""Replace paragraph.text[start:end] by editing run text only, so each run keeps its rPr
(bold, size, colour) and its a:hlinkClick. Everything that contributes to paragraph.text
without being a run has to be accounted for or every later offset shifts: a:br is one
position, and a:fld holds generated text that must not be rewritten. Skipping a child that
carries text moves the edit somewhere else without saying so, which is why an unrecognized
one is refused rather than passed over."""
position, written, covered = 0, False, []
for child in list(paragraph._p):
if child.tag in (qn("a:pPr"), qn("a:endParaRPr")):
continue # properties, no text of their own
if child.tag == qn("a:br"):
if start <= position < end:
covered.append(child) # removed below, once the range is known good
position += 1
continue
if child.tag == qn("a:fld"):
field = "".join(t.text or "" for t in child.findall(qn("a:t")))
if position < end and start < position + len(field):
raise ValueError("charRange covers an a:fld; its text is generated, not stored")
position += len(field)
continue
if child.tag != qn("a:r"):
raise ValueError(f"paragraph holds {child.tag}, which this helper cannot position")
run = next(r for r in paragraph.runs if r._r is child)
run_start, run_end = position, position + len(run.text)
position = run_end
if run_end <= start or run_start >= end:
continue
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: # nothing removed yet, so this leaves the
raise ValueError("charRange did not intersect any run") # paragraph as it was found
for child in covered:
paragraph._p.remove(child)
p = Presentation("/abs/deck.pptx")
shape = next(s for s in walk(p.slides[1].shapes) if s.shape_id == 4)
before = shape.text_frame.paragraphs[0]
replace_char_range(before, 8, 11, "8%") # anchor had "paragraph": 0
# replace_char_range(shape.table.cell(1, 0).text_frame.paragraphs[0], ...) # "tableCell" anchor
# `save(path)` overwrites whatever is there. The scripts refuse an existing --out; a library
# edit has to refuse one too, and "x" is how you say that without a check that can race.
with open("/abs/deck-updated.pptx", "xb") as out:
p.save(out)
```
Verify that the formatting survived, not just the text — `paragraphs[0].text == "..."` plus a
paragraph count passes even when every run was collapsed into one unformatted run:
```python
check = Presentation("/abs/deck-updated.pptx")
edited = next(s for s in walk(check.slides[1].shapes) if s.shape_id == 4)
after = edited.text_frame.paragraphs[0]
assert after.text == expected_text
assert len(after.runs) == len(before.runs) # nothing collapsed
assert [r.hyperlink.address for r in after.runs] == [r.hyperlink.address for r in before.runs]
assert [(r.font.bold, r.font.size) for r in after.runs] == [(r.font.bold, r.font.size) for r in before.runs]
```
A table shape has no `text_frame` at all — reaching for one raises `AttributeError`. Route a
`tableCell` anchor through `shape.table.cell(row, col).text_frame`.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| 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: 195k | Updated | Claude CodeCodex | Verified | Install |
| 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: 195k | Updated | Claude CodeCodex | Verified | Install |
| 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: 195k | Updated | Claude CodeCodex | Verified | Install |
| 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: 195k | Updated | Claude CodeCodex | Verified | Install |