DOCX creation, editing, and analysis
Overview
A user may ask you to create, edit, or analyze the contents of a .docx file. A .docx file is essentially a ZIP archive containing XML files and other resources that you can read or edit. You have different tools and workflows available for different tasks.
Design requiremnet
Deliver studio-quality Word documents with deep thought on content, functionality, and styling. Users often don't explicitly request advanced features (covers, TOC, backgrounds, back covers, footnotes, charts)—deeply understand needs and proactively extend. The document must have 1.3x line spacing and have charts centered horizontally.
Available color(choose one)
- "Ink & Zen" Color Palette (Wabi-Sabi Style)
The design uses a grayscale "Ink" palette to differentiate from standard business blue/morandi styles.
Primary (Titles):#0B1220
Body Text:#0F172A
Secondary (Subtitles):#2B2B2B
Accent (UI / Decor):#9AA6B2
Table Header / Subtle Background:#F1F5F9
- Wilderness Oasis": Sage & Deep Forest
Primary (Titles): #1A1F16 (Deep Forest Ink)
Body Text: #2D3329 (Dark Moss Gray)
Secondary (Subtitles): #4A5548 (Neutral Olive)
Accent (UI/Decor): #94A3B8 (Steady Silver)
Table/Background: #F8FAF7 (Ultra-Pale Mint White)
- "Terra Cotta Afterglow": Warm Clay & Greige
Commonly utilized by top-tier consulting firms and architectural studios, this scheme warms up the gray scale to create a tactile sensation similar to premium cashmere.
Primary (Titles): #26211F (Deep Charcoal Espresso)
Body Text: #3D3735 (Dark Umber Gray)
Secondary (Subtitles): #6B6361 (Warm Greige)
Accent (UI/Decor): #C19A6B (Terra Cotta Gold / Muted Ochre)
Table/Background: #FDFCFB (Off-White / Paper Texture)
- "Midnight Code": High-Contrast Slate & Silver
Ideal for cutting-edge technology, AI ventures, or digital transformation projects. This palette carries a slight "electric" undertone that provides superior visual penetration.
Primary (Titles): #020617 (Midnight Black)
Body Text: #1E293B (Deep Slate Blue)
Secondary (Subtitles): #64748B (Cool Blue-Gray)
Accent (UI/Decor): #94A3B8 (Steady Silver)
Table/Background: #F8FAFC (Glacial Blue-White)
Chinese plot PNG method**
If using Python to generate PNGs containing Chinese characters, note that Matplotlib defaults to the DejaVu Sans font which lacks Chinese support; since the environment already has the SimHei font installed, you should set it as the default by configuring:
matplotlib.font_manager.fontManager.addfont('/usr/share/fonts/truetype/chinese/SimHei.ttf')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
Specialized Element Styling
- Table Borders: Use a "Single" line style with a size of 12 and the Primary Ink color. Internal vertical borders should be set to Nil (invisible) to create a clean, modern horizontal-only look.
- CRITICAL: Table Cell Margins - ALL tables MUST set
marginsproperty at the Table level to prevent text from touching borders. This is mandatory for professional document quality.
Alignment and Typography
CJK body: justify + 2-char indent. English: left. Table numbers: right. Headings: no indent.
For both languages, Must use a line spacing of 1.3x (250 twips). Do not use single line spacing !!!
CRITICAL: Chinese Quotes in JavaScript/TypeScript Code
MANDATORY: When writing JavaScript/TypeScript code for docx-js, ALL Chinese quotation marks (""", ''') inside strings MUST be escaped as Unicode escape sequences:
- Left double quote "\u201c" (")
- Right double quote "\u201d" (")
- Left single quote "\u2018" (')
- Right single quote "\u2019" (')
Example - INCORRECT (will cause syntax error):
new TextRun({
text: "他说"你好"" // ERROR: Chinese quotes break JS syntax
})
Example - CORRECT:
new TextRun({
text: "他说\u201c你好\u201d" // Correct: escaped Unicode
})
Alternative - Use template literals:
new TextRun({
text: `他说"你好"` // Also works: template literals allow Chinese quotes
})
Workflow Decision Tree
Reading/Analyzing Content
Use "Text extraction" or "Raw XML access" sections below.
Creating New Document
Use "Creating a new Word document" workflow.
Editing Existing Document
- Your own document + simple changes
Use "Basic OOXML editing" workflow
- Someone else's document
Use "Redlining workflow" (recommended default)
- Legal, academic, business, or government docs
Use "Redlining workflow" (required)
Reading and analyzing content
Note: For .doc (legacy format), first convert with libreoffice --convert-to docx file.doc.
Text extraction
If you just need to read the text contents of a document, you should convert the document to markdown using pandoc. Pandoc provides excellent support for preserving document structure and can show tracked changes:
# Convert document to markdown with tracked changes
pandoc --track-changes=all path-to-file.docx -o output.md
# Options: --track-changes=accept/reject/all
Raw XML access
You need raw XML access for: comments, complex formatting, document structure, embedded media, and metadata. For any of these features, you'll need to unpack a document and read its raw XML contents.
Unpacking a file
python ooxml/scripts/unpack.py <office_file> <output_directory>
Key file structures
word/document.xml- Main document contentsword/comments.xml- Comments referenced in document.xmlword/media/- Embedded images and media files- Tracked changes use
<w:ins>(insertions) and<w:del>(deletions) tags
Creating a new Word document
When creating a new Word document from scratch, use docx-js, but use bun instead of node to implement it. which allows you to create Word documents using JavaScript/TypeScript.