DOCX Creation, Editing, and Analysis
Overview
Work with .docx files which are ZIP archives containing XML files. Different tools and workflows available for different tasks.
Workflow Decision Tree
Reading/Analyzing Content
Use text extraction or raw XML access
Creating New Document
Use docx-js workflow
Editing Existing Document
- Your own document + simple changes: Basic OOXML editing
- Someone else's document: Redlining workflow (recommended)
- Legal, academic, business docs: Redlining workflow (required)
Reading and Analyzing Content
Text Extraction
# Convert to markdown with tracked changes
pandoc --track-changes=all path-to-file.docx -o output.md
Raw XML Access
For comments, complex formatting, document structure, embedded media, and metadata:
# Unpack document
python ooxml/scripts/unpack.py <office_file> <output_directory>
Key file structures:
word/document.xml- Main document contentsword/comments.xml- Commentsword/media/- Embedded images and media
Creating New Documents
Use docx-js (JavaScript/TypeScript):
import { Document, Paragraph, TextRun, Packer } from 'docx';
const doc = new Document({
sections: [{
properties: {},
children: [
new Paragraph({
children: [new TextRun("Hello World!")],
}),
],
}],
});
// Export
const buffer = await Packer.toBuffer(doc);
Editing Existing Documents
Use the Document library (Python) for OOXML manipulation:
- Unpack:
python ooxml/scripts/unpack.py <file.docx> <dir> - Create Python script using Document library
- Pack:
python ooxml/scripts/pack.py <dir> <file.docx>
Redlining Workflow
For tracked changes:
- Convert to markdown:
pandoc --track-changes=all file.docx -o current.md - Identify and group changes
- Unpack document
- Implement changes in batches using Document library
- Pack the document
- Verify all changes applied correctly
Dependencies
- pandoc: For text extraction
- docx:
npm install -g docxfor creating new documents - LibreOffice: For PDF conversion
- defusedxml: For secure XML parsing