Generate and Modify DOCX Documents
Guides .docx creation, editing, and redlining - text extraction via pandoc, docx-js for new files, OOXML editing, and tracked-change reviews.
Why it matters
Automate the creation, editing, and analysis of DOCX documents. This asset can generate new documents from scratch, modify existing ones with tracked changes, or extract and analyze content using various methods.
Outcomes
What it gets done
Create new DOCX files using JavaScript/TypeScript.
Edit existing DOCX files with tracked changes (redlining).
Extract text content from DOCX files.
Access raw XML for advanced document analysis.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-docx-official | bash Overview
DOCX creation, editing, and analysis
Routes .docx work to the right tool: pandoc for text extraction, docx-js for new documents, a Python OOXML library for edits, and a batched redlining workflow for precise tracked changes. Use for any .docx creation, editing, or analysis task, and specifically the Redlining workflow when editing someone else's document or any legal, academic, business, or government file.
What it does
This skill equips an assistant to create, edit, and analyze .docx files, treating a .docx as a ZIP archive of XML files and other resources. It provides a workflow decision tree: for reading/analyzing content, use text extraction or raw XML access; for creating a new document, use the docx-js workflow; for editing an existing document, use Basic OOXML editing for your own document with simple changes, or the Redlining workflow (recommended default, and required for legal, academic, business, or government documents) for someone else's document.
Text extraction converts a document to markdown via pandoc, which preserves document structure and can show tracked changes (--track-changes=accept/reject/all). Raw XML access is needed for comments, complex formatting, document structure, embedded media, and metadata: unpack a file with python ooxml/scripts/unpack.py <office_file> <output_directory>, then work with word/document.xml (main contents), word/comments.xml (comments), and word/media/ (embedded images); tracked changes use <w:ins> for insertions and <w:del> for deletions.
Creating a new document from scratch uses docx-js (JavaScript/TypeScript): mandatorily read the full docx-js.md reference (500 lines, no range limits), build the document with Document/Paragraph/TextRun components, then export via Packer.toBuffer(). Editing an existing document uses the Document library, a Python library for OOXML manipulation with both high-level methods and direct DOM access: read the full ooxml.md reference (600 lines), unpack the document, write and run a Python script using the Document library, then repack it with python ooxml/scripts/pack.py <input_directory> <office_file>.
The Redlining workflow plans tracked changes in markdown before implementing them in OOXML, batching 3-10 related changes at a time and testing each batch before moving on. Its core principle is minimal, precise edits: only mark text that actually changed, breaking replacements into unchanged-text plus deletion plus insertion plus unchanged-text, and preserving the original run's RSID for unchanged text. The full workflow: convert the document to markdown with tracked changes, identify and batch all needed changes (grouped by section, type, complexity, or page range - never by markdown line number, which doesn't map to XML structure), read the OOXML documentation and unpack the file (noting the suggested RSID), implement each batch by grepping word/document.xml for current text before scripting changes and saving via the Document library, repack the reviewed document, then do a final verification pass converting back to markdown and grepping to confirm the original phrase is gone and the replacement phrase is present.
To visually analyze a document, convert DOCX to PDF with soffice --headless --convert-to pdf document.docx, then PDF pages to JPEG with pdftoppm -jpeg -r 150 document.pdf page (adjustable DPI, page range via -f/-l, or -png output). Generated code should be concise, avoiding verbose variable names, redundant operations, and unnecessary print statements.
When to use - and when NOT to
Use whenever a user asks to create, edit, or analyze the contents of a .docx file. Use the Redlining workflow by default for anyone else's document, and it becomes required rather than just recommended for legal, academic, business, or government documents given the review stakes involved.
Inputs and outputs
Inputs are .docx files, or a JS/TS spec for new documents. Outputs are created or edited .docx files, markdown extractions (with or without tracked changes), unpacked XML directories, or JPEG/PNG page images for visual analysis. Required dependencies: pandoc (text extraction), the docx npm package (new document creation), LibreOffice (PDF conversion), Poppler (pdftoppm for PDF-to-image), and defusedxml (secure XML parsing).
pandoc --track-changes=all path-to-file.docx -o output.md
Integrations
pandoc, docx-js, the Document Python library for OOXML manipulation, LibreOffice (soffice), and Poppler's pdftoppm.
Who it's for
Anyone using an AI assistant to generate new Word documents, make simple edits to their own documents, or produce clean, professionally tracked-change redlines on someone else's document - including legal, academic, business, and government review workflows.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.