$npx -y skills add agentscope-ai/QwenPaw --skill docx-enUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files). Triggers include: any mention of \"Word doc\", \"word document\", \".docx\", or requests to produce professional documents with formatting like tables of contents, headings,
| 1 | > **Important:** All `scripts/` paths are relative to this skill directory. |
| 2 | > Run with: `cd {this_skill_dir} && python scripts/...` |
| 3 | > Or use the `cwd` parameter of `execute_shell_command`. |
| 4 | |
| 5 | # DOCX creation, editing, and analysis |
| 6 | |
| 7 | ## Prerequisites |
| 8 | |
| 9 | - **docx** (`npm install -g docx`): new document creation |
| 10 | - **LibreOffice** (`soffice`): `.doc` -> `.docx` conversion, tracked-changes acceptance, and PDF export |
| 11 | - **pandoc**: text extraction |
| 12 | - **pdftoppm** (poppler-utils): document-to-image workflows |
| 13 | - If `pdftoppm` is unavailable, a Python fallback path may use `pdf2image`. |
| 14 | - On Windows, dependencies must be installed and available in `PATH`; if missing, report the dependency issue and stop (do not keep retrying). |
| 15 | |
| 16 | ## Overview |
| 17 | |
| 18 | A .docx file is a ZIP archive containing XML files. |
| 19 | |
| 20 | ## Quick Reference |
| 21 | |
| 22 | | Task | Approach | |
| 23 | |------|----------| |
| 24 | | Read/analyze content | `pandoc` or unpack for raw XML | |
| 25 | | Create new document | Use `docx-js` - see Creating New Documents below | |
| 26 | | Edit existing document | Unpack → edit XML → repack - see Editing Existing Documents below | |
| 27 | |
| 28 | ### Converting .doc to .docx |
| 29 | |
| 30 | Legacy `.doc` files must be converted before editing: |
| 31 | |
| 32 | ```bash |
| 33 | python scripts/office/soffice.py --headless --convert-to docx document.doc |
| 34 | ``` |
| 35 | |
| 36 | ### Reading Content |
| 37 | |
| 38 | ```bash |
| 39 | # Text extraction with tracked changes |
| 40 | pandoc --track-changes=all document.docx -o output.md |
| 41 | |
| 42 | # Raw XML access |
| 43 | python scripts/office/unpack.py document.docx unpacked/ |
| 44 | ``` |
| 45 | |
| 46 | ### Converting to Images |
| 47 | |
| 48 | ```bash |
| 49 | python scripts/office/soffice.py --headless --convert-to pdf document.docx |
| 50 | pdftoppm -jpeg -r 150 document.pdf page |
| 51 | ``` |
| 52 | |
| 53 | ### Accepting Tracked Changes |
| 54 | |
| 55 | To produce a clean document with all tracked changes accepted (requires LibreOffice): |
| 56 | |
| 57 | ```bash |
| 58 | python scripts/accept_changes.py input.docx output.docx |
| 59 | ``` |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | ## Creating New Documents |
| 64 | |
| 65 | Generate .docx files with JavaScript, then validate. Install: `npm install -g docx` |
| 66 | |
| 67 | ### Setup |
| 68 | ```javascript |
| 69 | const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, ImageRun, |
| 70 | Header, Footer, AlignmentType, PageOrientation, LevelFormat, ExternalHyperlink, |
| 71 | TableOfContents, HeadingLevel, BorderStyle, WidthType, ShadingType, |
| 72 | VerticalAlign, PageNumber, PageBreak } = require('docx'); |
| 73 | |
| 74 | const doc = new Document({ sections: [{ children: [/* content */] }] }); |
| 75 | Packer.toBuffer(doc).then(buffer => fs.writeFileSync("doc.docx", buffer)); |
| 76 | ``` |
| 77 | |
| 78 | ### Validation |
| 79 | After creating the file, validate it. If validation fails, unpack, fix the XML, and repack. |
| 80 | ```bash |
| 81 | python scripts/office/validate.py doc.docx |
| 82 | ``` |
| 83 | |
| 84 | ### Page Size |
| 85 | |
| 86 | ```javascript |
| 87 | // CRITICAL: docx-js defaults to A4, not US Letter |
| 88 | // Always set page size explicitly for consistent results |
| 89 | sections: [{ |
| 90 | properties: { |
| 91 | page: { |
| 92 | size: { |
| 93 | width: 12240, // 8.5 inches in DXA |
| 94 | height: 15840 // 11 inches in DXA |
| 95 | }, |
| 96 | margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1 inch margins |
| 97 | } |
| 98 | }, |
| 99 | children: [/* content */] |
| 100 | }] |
| 101 | ``` |
| 102 | |
| 103 | **Common page sizes (DXA units, 1440 DXA = 1 inch):** |
| 104 | |
| 105 | | Paper | Width | Height | Content Width (1" margins) | |
| 106 | |-------|-------|--------|---------------------------| |
| 107 | | US Letter | 12,240 | 15,840 | 9,360 | |
| 108 | | A4 (default) | 11,906 | 16,838 | 9,026 | |
| 109 | |
| 110 | **Landscape orientation:** docx-js swaps width/height internally, so pass portrait dimensions and let it handle the swap: |
| 111 | ```javascript |
| 112 | size: { |
| 113 | width: 12240, // Pass SHORT edge as width |
| 114 | height: 15840, // Pass LONG edge as height |
| 115 | orientation: PageOrientation.LANDSCAPE // docx-js swaps them in the XML |
| 116 | }, |
| 117 | // Content width = 15840 - left margin - right margin (uses the long edge) |
| 118 | ``` |
| 119 | |
| 120 | ### Styles (Override Built-in Headings) |
| 121 | |
| 122 | Use Arial as the default font (universally supported). Keep titles black for readability. |
| 123 | |
| 124 | ```javascript |
| 125 | const doc = new Document({ |
| 126 | styles: { |
| 127 | default: { document: { run: { font: "Arial", size: 24 } } }, // 12pt default |
| 128 | paragraphStyles: [ |
| 129 | // IMPORTANT: Use exact IDs to override built-in styles |
| 130 | { id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quickFormat: |