$npx -y skills add K-Dense-AI/scientific-agent-skills --skill docxUse 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, page n
| 1 | # DOCX creation, editing, and analysis |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | A .docx file is a ZIP archive containing XML files. |
| 6 | |
| 7 | ## Quick Reference |
| 8 | |
| 9 | | Task | Approach | |
| 10 | |------|----------| |
| 11 | | Read/analyze content | `pandoc` or unpack for raw XML | |
| 12 | | Create new document | Use `docx-js` - see Creating New Documents below | |
| 13 | | Edit existing document | Unpack → edit XML → repack - see Editing Existing Documents below | |
| 14 | |
| 15 | ### Converting .doc to .docx |
| 16 | |
| 17 | Legacy `.doc` files must be converted before editing: |
| 18 | |
| 19 | ```bash |
| 20 | python scripts/office/soffice.py --headless --convert-to docx document.doc |
| 21 | ``` |
| 22 | |
| 23 | ### Reading Content |
| 24 | |
| 25 | ```bash |
| 26 | # Text extraction with tracked changes |
| 27 | pandoc --track-changes=all document.docx -o output.md |
| 28 | |
| 29 | # Raw XML access |
| 30 | python scripts/office/unpack.py document.docx unpacked/ |
| 31 | ``` |
| 32 | |
| 33 | ### Converting to Images |
| 34 | |
| 35 | ```bash |
| 36 | python scripts/office/soffice.py --headless --convert-to pdf document.docx |
| 37 | pdftoppm -jpeg -r 150 document.pdf page |
| 38 | ``` |
| 39 | |
| 40 | ### Accepting Tracked Changes |
| 41 | |
| 42 | To produce a clean document with all tracked changes accepted (requires LibreOffice): |
| 43 | |
| 44 | ```bash |
| 45 | python scripts/accept_changes.py input.docx output.docx |
| 46 | ``` |
| 47 | |
| 48 | --- |
| 49 | |
| 50 | ## Creating New Documents |
| 51 | |
| 52 | Generate .docx files with JavaScript, then validate. Install: `npm install -g docx` |
| 53 | |
| 54 | ### Setup |
| 55 | ```javascript |
| 56 | const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, ImageRun, |
| 57 | Header, Footer, AlignmentType, PageOrientation, LevelFormat, ExternalHyperlink, |
| 58 | InternalHyperlink, Bookmark, FootnoteReferenceRun, PositionalTab, |
| 59 | PositionalTabAlignment, PositionalTabRelativeTo, PositionalTabLeader, |
| 60 | TabStopType, TabStopPosition, Column, SectionType, |
| 61 | TableOfContents, HeadingLevel, BorderStyle, WidthType, ShadingType, |
| 62 | VerticalAlign, PageNumber, PageBreak } = require('docx'); |
| 63 | |
| 64 | const doc = new Document({ sections: [{ children: [/* content */] }] }); |
| 65 | Packer.toBuffer(doc).then(buffer => fs.writeFileSync("doc.docx", buffer)); |
| 66 | ``` |
| 67 | |
| 68 | ### Validation |
| 69 | After creating the file, validate it. If validation fails, unpack, fix the XML, and repack. |
| 70 | ```bash |
| 71 | python scripts/office/validate.py doc.docx |
| 72 | ``` |
| 73 | |
| 74 | ### Page Size |
| 75 | |
| 76 | ```javascript |
| 77 | // CRITICAL: docx-js defaults to A4, not US Letter |
| 78 | // Always set page size explicitly for consistent results |
| 79 | sections: [{ |
| 80 | properties: { |
| 81 | page: { |
| 82 | size: { |
| 83 | width: 12240, // 8.5 inches in DXA |
| 84 | height: 15840 // 11 inches in DXA |
| 85 | }, |
| 86 | margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1 inch margins |
| 87 | } |
| 88 | }, |
| 89 | children: [/* content */] |
| 90 | }] |
| 91 | ``` |
| 92 | |
| 93 | **Common page sizes (DXA units, 1440 DXA = 1 inch):** |
| 94 | |
| 95 | | Paper | Width | Height | Content Width (1" margins) | |
| 96 | |-------|-------|--------|---------------------------| |
| 97 | | US Letter | 12,240 | 15,840 | 9,360 | |
| 98 | | A4 (default) | 11,906 | 16,838 | 9,026 | |
| 99 | |
| 100 | **Landscape orientation:** docx-js swaps width/height internally, so pass portrait dimensions and let it handle the swap: |
| 101 | ```javascript |
| 102 | size: { |
| 103 | width: 12240, // Pass SHORT edge as width |
| 104 | height: 15840, // Pass LONG edge as height |
| 105 | orientation: PageOrientation.LANDSCAPE // docx-js swaps them in the XML |
| 106 | }, |
| 107 | // Content width = 15840 - left margin - right margin (uses the long edge) |
| 108 | ``` |
| 109 | |
| 110 | ### Styles (Override Built-in Headings) |
| 111 | |
| 112 | Use Arial as the default font (universally supported). Keep titles black for readability. |
| 113 | |
| 114 | ```javascript |
| 115 | const doc = new Document({ |
| 116 | styles: { |
| 117 | default: { document: { run: { font: "Arial", size: 24 } } }, // 12pt default |
| 118 | paragraphStyles: [ |
| 119 | // IMPORTANT: Use exact IDs to override built-in styles |
| 120 | { id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quickFormat: true, |
| 121 | run: { size: 32, bold: true, font: "Arial" }, |
| 122 | paragraph: { spacing: { before: 240, after: 240 }, outlineLevel: 0 } }, // outlineLevel required for TOC |
| 123 | { id: "Heading2", name: "Heading 2", basedOn: "Normal", next: "Normal", quickFormat: true, |
| 124 | run: { size: 28, bold: true, font: "Arial" }, |
| 125 | paragraph: { spacing: { before: 180, after: 180 }, outlineLevel: 1 } }, |
| 126 | ] |
| 127 | }, |
| 128 | sections: [{ |
| 129 | children: [ |
| 130 | new Paragraph({ heading: HeadingLevel.HEADI |