# Editing Documents (EDIT)

When the user wants to modify an existing document.

## Step 1: Unpack

```bash
cd "$SKILL_DIR" && uv run --project . python scripts/office/unpack.py <document.docx> /tmp/_tmp_docx_unpacked/
```

This extracts the ZIP, pretty-prints XML, optionally merges adjacent runs and simplifies tracked changes.

## Step 2: Edit XML files

Edit the relevant XML files under `/tmp/_tmp_docx_unpacked/`:
- `word/document.xml` — main content
- `word/comments.xml` — comments (use `comment.py` to add comments instead of editing directly)

## Step 3: Repack and validate

```bash
cd "$SKILL_DIR" && uv run --project . python scripts/office/pack.py /tmp/_tmp_docx_unpacked/ <output.docx> --original <original.docx>
```

This condenses XML, validates against XSD schemas (comparing with original to only flag new errors), auto-repairs common issues, and creates the .docx file.

## Step 4: Upload

```bash
cd "$SKILL_DIR" && bash scripts/upload_file.sh --file <output.docx> --url-only
```

## Adding Comments

**CRITICAL: Always pass `--author` with your current Agent display name.** Without this, Word displays "未知" (unknown) for the comment author.

First, confirm your agent name — you are the current Agent in this conversation, use the display name from your identity. Then:

```bash
cd "$SKILL_DIR" && uv run --project . python scripts/comment.py /tmp/_tmp_docx_unpacked/ 0 "Comment text" --author "你的Agent名称" --initials "首字母缩写"
# For replies:
cd "$SKILL_DIR" && uv run --project . python scripts/comment.py /tmp/_tmp_docx_unpacked/ 1 "Reply text" --parent 0 --author "你的Agent名称" --initials "首字母缩写"
```

After running, add the printed `<w:commentRangeStart/>` / `<w:commentRangeEnd/>` / `<w:commentReference/>` markers to `document.xml`.

## Accepting Tracked Changes

```bash
cd "$SKILL_DIR" && uv run --project . python scripts/accept_changes.py input.docx output.docx
```

## Other operations

### Converting .doc to .docx (requires LibreOffice)

Legacy `.doc` files must be converted before editing. **Requires `soffice`; if not installed, ask the user to convert manually.**

```bash
cd "$SKILL_DIR" && uv run --project . python scripts/office/soffice.py --headless --convert-to docx document.doc
```

### Converting to Images (optional — requires LibreOffice)

**If `soffice` is not available, skip this step.** Check first: `which soffice && echo "available" || echo "NOT available — skip"`

```bash
cd "$SKILL_DIR" && uv run --project . python scripts/office/soffice.py --headless --convert-to pdf document.docx
pdftoppm -jpeg -r 150 document.pdf page
```
