- name
- office
- description
- Work with non-PowerPoint Office/OOXML documents through the published unified office CLI: identify, inspect, query, validate, diagnose, preview, create, template, comment, batch-edit, dump/replay, or safely inspect raw parts in .docx and .xlsx files, including addressed run-level text edits that refuse rather than silently lose content. Use this for Word and Excel tasks instead of python-docx, openpyxl, ExcelJS, pandoc, or LibreOffice. Use the legacy format-specific CLIs only for the few workflows the umbrella command does not provide: direct CSV import/export, formula calculation, fine-grained XLSX formula linting and live batch-capability discovery, fine-grained XLSX rendering controls, and DOCX Markdown/style-map/image-extraction conversion.
# Office documents through one CLI
Use the unified `office` command for every DOCX/XLSX workflow unless the
legacy-only table below names the exact missing capability.
Nothing to install beyond `moonx`. Run from any directory:
```
moonx moonbitlang/office help all --json
moonx moonbitlang/office <command> <args...>
```
Arguments after `moonbitlang/office` are passed directly to the command; do not
insert a `--` separator.
The WebAssembly target is the default and is the right choice for untrusted
documents. It cannot spawn programs or open network connections, and the CLI
also applies bounded package, XML, scan, output, and mutation limits. It can
still read or write the paths supplied to it and consume CPU within those
limits. For trusted files, `moonx --target native moonbitlang/office ...` is a
faster drop-in.
Pin a version when reproducibility matters: `moonx moonbitlang/office@0.4.0 ...`.
`@latest` refreshes the registry index before resolving.
## The CLI describes itself — prefer that over prose
This file will drift; the binary will not. Three introspection commands are
normative:
```
moonx moonbitlang/office help all --json # every format, command, field, limit
moonx moonbitlang/office help schemas --json # every consumed JSON contract
moonx moonbitlang/office help schema ID --json # one contract, e.g. xlsx.batch/2
```
`help all` carries a `crc32:` fingerprint over the capability registry. When
this document and the fingerprinted registry disagree, the registry wins.
## Default workflow
1. Discover the surface with `help all --json`, and `help schema ID` before
authoring any JSON input document.
2. Run `identify`, then `outline --json`, before choosing paths or edits.
3. Inspect only what you need with `get`, `text`, or `query`. Reuse the
canonical paths the CLI returns; do not invent selectors.
4. For mutations, prefer a separate `--out`, run `--dry-run` where supported,
and read the transaction preservation report.
5. Read the result back, then run `validate` and `issues`. For any XLSX
containing formulas, also run the legacy `xlsx lint` fallback below and
require `finding_count == 0`: newly authored formulas have no cached
result, so `issues` and `preview` cannot evaluate them. Lint evaluates
formula masters but not shared/array slave formulas — treat those slaves as
an unresolved residual rather than claiming formula correctness.
6. Run the delivery gate in "Verify before you deliver" below before handing
anything over. `preview` publishes the HTML, but nothing in this toolchain
renders it to an image — see the ceiling statement in that section.
Every documented failure exits non-zero, but a successful diagnostic command
can still report warnings or formula findings with exit code zero. Inspect the
structured counts and records, not just the exit code.
Ordinary `--json` commands emit one `office.output/1` success/failure envelope.
`dump --json` is the deliberate exception: it emits the replayable
`office.dump/1` document directly.
## Command map
Replace the `office` token below with the `moonx moonbitlang/office` launcher.
| Goal | Command |
| --- | --- |
| Discover formats, commands, fields, limits | `office help [all\|FORMAT\|COMMAND\|FORMAT COMMAND] [--json\|--jsonl]` |
| Discover consumed JSON contracts | `office help schemas [--json\|--jsonl]`; `office help schema ID [--json\|--jsonl]` |
| Verify and identify a package | `office identify FILE [--json]` |
| Map document/workbook structure | `office outline FILE [--max-elements N] [--max-output-chars N] [--json]` |
| Resolve one canonical selector | `office get FILE SELECTOR [limits] [--json]` |
| Extract path-tagged paragraphs/cells | `office text FILE [--under SELECTOR] [--offset N] [--limit N] [limits] [--json]` |
| Search bounded literal predicates | `office query FILE [CELL_SELECTOR] [--under SELECTOR] [DOCX predicates] [pagination/limits] [--json]` |
| Locate literal text with editability judgments | `office find FILE --text TEXT [--in PATH] [--limit N] [--context N] [--json]` |
| Replace literal text transactionally | `office replace FILE OUT.docx --text TEXT --with TEXT [--in PATH] [--nth K] [--expect N] [--allow-zero] [--dry-run] [--overwrite] [--json]` |
| Apply direct character formatting | `office format FILE OUT.docx (--text TEXT [--in PATH] [--nth K] \| --range START:END --in P) [--bold on\|off] [--italic on\|off] [--underline on\|off] [--color RRGGBB] [--expect N] [--allow-zero] [--dry-run] [--overwrite] [--json]` (at least one property flag) |
| Insert one paragraph, minting its identity | `office insert-paragraph FILE OUT.docx (--before P\|--after P) --content JSON [--dry-run] [--overwrite] [--json]` |
| Delete one direct body paragraph | `office delete-paragraph FILE OUT.docx --at (p[N]\|p[id="…"]) [--expect-text TEXT] [--dry-run] [--overwrite] [--json]` |
| Run the exact mutation validation gate | `office validate FILE [--json\|--jsonl]` |
| Report validation plus bounded actionable warnings | `office issues FILE [--json\|--jsonl]` |
| Publish deterministic offline HTML | `office preview FILE --output OUT.html [--overwrite] [--json\|--jsonl]` |
| Create a blank validated file | `office create xlsx OUT.xlsx [--sheet NAME] [--dry-run] [--overwrite] [--json]` or `office create docx OUT.docx [--dry-run] [--overwrite] [--json]` |
| Merge strict placeholders/row regions | `office template FILE DATA.json --out OUT [--dry-run] [--overwrite] [--allow-missing] [--json\|--jsonl]` |
| Replace literal text, set one addressed run's text, or accept/reject tracked changes, in an existing DOCX | `office edit FILE SCRIPT.json --out OUT.docx [--dry-run] [--overwrite] [--allow-unmatched] [--json\|--jsonl]` |
| Add/reply/resolve DOCX comments | `office annotate FILE SCRIPT.json --out OUT.docx [--dry-run] [--overwrite] [--json\|--jsonl]` |
| Mutate an XLSX transactionally | `office batch BOOK.xlsx SCRIPT.json [--out OUT.xlsx] [--dry-run] [--overwrite] [--json]` |
| Author a fresh DOCX from ops | `office batch --format docx OUT.docx SCRIPT.json [--dry-run] [--overwrite] [--json]` |
| Produce a replayable semantic dump | `office dump FILE --json` or streaming `--jsonl` |
| Reconstruct replayable dump content | `office replay DUMP.json --output OUT [--overwrite] [--json\|--jsonl]` |
| Inventory/read OOXML parts | `office raw list FILE [--json]`; `office raw read FILE PART [--json] [--base64\|--output FILE]` |
| Replace one XML part | `office raw replace FILE PART (--xml XML \| --xml-file FILE) [--out FILE] [--dry-run] [--overwrite] [--json]` |
| Edit inside one XML part | `office raw edit FILE PART --path PATH --action ACTION [action arguments] [--namespace PREFIX=URI]... [--all] [--out FILE] [--dry-run] [--overwrite] [--json]` |
`[limits]` abbreviates `--max-elements N --max-output-chars N`. DOCX query
predicates are `--kind`, `--text`, `--id`, repeatable `--property NAME=VALUE`,
and `--ignore-case`. XLSX query uses a quoted cell selector such as
`'cell[type=number][value>0]'`.
## Canonical selectors
Selectors are format-rooted:
```
/docx/body/p[1]
/docx/body/tbl[1]/tr[1]/tc[2]/p[1]
/docx/comments/comment[id="7"]
/xlsx/workbook
/xlsx/sheet[name="Data"]
/xlsx/sheet[name="Data"]/cell[A1]
/xlsx/sheet[name="Data"]/range[A1:C12]
```
Ordinal paths are snapshot-relative. Re-run `outline` or `text` after a
mutation before reusing them.
The XLSX payload shape follows the selector: a `cell[A1]` selector returns a
single `data.cell` **object**, while a `range[A1:C12]` selector returns a
`data.cells` **array**. Do not assume `data.cells` is always present.
## Mutation contracts
- `create` is create-new by default; `--overwrite` explicitly replaces an
existing destination. `--dry-run` validates without publishing.
- XLSX `batch` consumes `xlsx.batch/2`; the historical `xlsx.batch/1` remains
accepted with its exact v1 registry subset. With no `--out` it rewrites the
input after all operations pass; prefer `--out` when preserving the source
matters.
- DOCX `batch --format docx` consumes `docx.batch/2` (and accepts
`docx.batch/1`) and only authors a fresh destination. It does not edit an
existing DOCX and does not accept `--out`.
- `template` never modifies its template. It substitutes non-executable
`{{key}}` placeholders from flat scalar data and optional marked-row regions
into a separate output.
- `edit` is the literal find & replace surface for an existing DOCX. It
consumes `docx.edit/1` (`{"op": "replace_text", "params": {"find", "replace",
"occurrence"}}`) and publishes a separate output; the input is never touched.
`find` is **literal**, never a regular expression, and matches across run
boundaries. Omitting `occurrence` replaces every occurrence in document
order; `occurrence: N` replaces only the Nth. Every op is matched against the
original snapshot, so two ops whose matches overlap refuse
(`office.edit.overlapping_matches`). A match the byte-span rewriter cannot
own — mixed run content, a hyperlink boundary, or a footnote/endnote/comment
story — refuses with `office.edit.unsupported_context` rather than being
silently skipped, and an op that finds nothing refuses with
`office.edit.unmatched_find` unless `--allow-unmatched` is passed.
- `edit` also RESOLVES tracked changes, through the same `docx.edit/1` script:
`accept_revision` and `reject_revision` with `{"id"}`, `{"author"}`,
`{"type": "ins"|"del"}`, or `{"all": true}`. Spelled selector fields are
conjunctive; `id` is the stable `w:id` handle `outline` reports, and ordinal
position is never a selector. Accepting an insertion (or rejecting a
deletion) unwraps the element and keeps its runs; rejecting an insertion (or
accepting a deletion) removes the element and its content. One script is
entirely `replace_text` or entirely revision ops — mixing them is rejected.
- `edit` also has an ADDRESSED text surface, `set_run_text`, under
`"schema": "docx.edit/2"`. Use it when you know *which* run to change rather
than which text to find: `{"op": "set_run_text", "params": {"at", "expect",
"text"}}`. `at` names ONE run — the `office query` path
(`/docx/body/p[3]/r[2]`) or its body-relative form (`p[3]/r[2]`). `expect`
must equal that run's **entire** current text, not a substring; a stale
expectation refuses rather than editing the wrong run, because addresses are
snapshot-relative and a document you read earlier may have moved. `text`
replaces the run's whole text. Setting a run to its own text validates and
changes nothing. A script is entirely `set_run_text` or entirely another
family — never mixed. Prefer this over `replace_text` when the same literal
occurs more than once, or when the text you want to change is short and
ambiguous.
- **Addressed edits refuse on content whose edit would not survive.** This is
the surface's main safety property and the most common surprise. A refusal
here is not a defect to retry around — it means the write would have been
lost, misattributed, or contradicted, and nothing downstream could have told
you. `set_run_text` refuses a run that:
- holds a **field's cached result** (a `PAGE`, `DATE`, `TOC`, `REF` or
`MERGEFIELD` answer). Word recomputes it and discards your text. Edit what
the field *asks*, or flatten the field, rather than its answer.
- sits in a **tracked insertion** (`w:ins`). Writing there makes the document
record that the insertion's named author wrote your words. Resolve the
revision first with `accept_revision`, then edit the resulting plain text.
- sits in a **content control** (`w:sdt`). A data-bound one is repopulated
from its XML part on open. Change the bound data instead. Plain-text
controls refuse too — the reader cannot yet tell them apart, and refusing
the safe case is the deliberate side to err on.
- sits in **textbox content** or a **markup-compatibility fallback**, where
the same text is commonly stored twice and editing one copy leaves the
other disagreeing.
- **owns suppressed content** the reader could not model — a picture, an
unmappable symbol, an unrecognised element — because replacing the run
would leave content beside your text that you never saw.
- sits in a **logical paragraph joined from several physical ones** (a
deleted paragraph mark joins them), where one address does not name one
place in the file.
- **spans a hyperlink boundary**, where the rewrite would silently grow or
shrink what is linked.
Each refusal names the construct it found. Read the message: it tells you
which of the above you hit, and therefore what to do instead.
- `annotate` is the preservation-safe existing-DOCX mutation surface. It
consumes `docx.annotation-batch/1` with `comment_add`, `comment_reply`,
`comment_resolve`, and `comment_unresolve` ops and publishes a separate
output. **Its ops carry their fields directly, not under a `params` object**
— unlike `xlsx.batch` and `docx.batch`. `comment_add` takes
`anchor: {"at": "/docx/body/p[1]"}`, `author`, and `body` as an *array* of
strings, plus an optional `label` that later ops reference via
`{"label": ...}`. Passing `params` fails with
`office.annotate.invalid_script`. Run `office help schema
docx.annotation-batch/1 --json` and read its `examples` before authoring.
- `raw replace` and `raw edit` are expert fallbacks. Use `--dry-run` and a
separate `--out`; semantic commands are safer whenever they can express the
task.
- A preservation report is authoritative. Do not infer preservation from the
requested operations.
- `dump --json` is the form accepted by `replay`. `dump --jsonl` is a
streaming inspection form with a terminal digest, not replay input.
- `preview --overwrite` and `replay --overwrite` remove the old destination
before staging the replacement; a later write failure can leave it absent.
Use a fresh destination, or make and verify a backup before explicit
replacement. They do not share the atomic-overwrite guarantee of the
transaction-backed mutation commands.
Run `office help schema ID --json` before authoring any consumed JSON
document. It is normative for `xlsx.batch/2`, `docx.batch/2`,
`docx.edit/1`, `docx.edit/2`, `office.template.data/1`, and
`docx.annotation-batch/1`.
## Shapes that are easy to get wrong
These four cost a failed run each if you guess. All are visible in
`help schema`, but guessing is the natural failure.
**1. A table cell is an object, not a string.** `rows` is an array of arrays
of *cell objects*:
```json
{"op": "table", "params": {"header_rows": 1, "rows": [
Auf GitHub ansehen