DOCX Skill (Read • Create • Edit • Redline • Comment)
Titles + Intro to doc
Title clarity is an absolute requirement. State the specific subject and purpose so the reader understands what the document is for before reading the body. Use plain descriptive language with no slogans and no punctuation. Apply this to document titles, subtitles, and section titles, using only words, numbers, and spaces. Use Word’s Title paragraph style for document titles. Keep it black with no underlining, paragraph borders, or decorative lines beneath it. Remove any direct formatting or separately added lines that conflict.
The opening content is essential to the reader's understanding of the whole document. Establish what the document covers, why it matters to this reader, and the main conclusion, decision, or task. Give enough context and scope to make the sections that follow easy to understand and show what the reader should learn or do.
Writing quality
- Write for the intended reader. Identify the author, recipient, and what the reader needs to understand or do. Follow user instructions first, choose the requested document format, and preserve the style of an existing document or supplied reference.
- Write directly in the author's voice, using “I” or “we” when appropriate. Present the update, recommendation, or request to the recipient. Match the author's tone and relationship to that audience; do not invent experience, authority, commitments, or facts from style examples.
- Lead with the conclusion, decision, or request. Use concrete subjects, strong verbs, and natural sentences. State what changed, why it matters, and what evidence or constraint supports the claim. Keep necessary qualifications and distinguish facts, interpretation, recommendations, and uncertainty.
- Remove stock formulas, slogans, inflated significance, vague abstractions, unsupported authorities, canned empathy, and ornamental transitions. Avoid conspicuous rhetorical triads, forced contrasts, repetitive cadence, and punctuation used only for emphasis. Judge these patterns in context; an isolated phrase, accurate technical term, or useful contrast is not automatically a defect.
- Review both the writing and the rendered document. Check that claims are supported, the author's voice is consistent, and every page is readable and free of layout defects. Do not mention this editorial framework in the delivered document unless asked.
Before formatting, read the title and section headings as an outline. Write connected paragraphs that explain relationships, and replace compressed labels or unnecessary compounds with natural wording. Preserve the source's meaning, including uncertainty, conditions, time periods, and comparisons. Use punctuation and passive voice in body text when they improve precision.
For the review steps, examples, and more context, read writing_quality.md.
Use this skill when you need to create or modify .docx, Word, or Google Docs-targeted document artifacts in this container environment and verify them visually.
Tools + Contract Requirements
- Use Codex workspace dependencies for docx artifact work: resolve them through the workspace dependency loader or runtime skill, then treat the returned Node/Python runtimes and package directory as authoritative. Do not use system
node, system python, global npm packages, or repo-local installs.
- For document creation and deterministic OOXML edits, it is still acceptable to use the bundled Python/OOXML helper scripts in this skill package when the JS surface is incomplete.
- Run any builder or helper file from a writable workspace or temp directory, not from the managed dependency directory itself.
- Final user-facing responses should describe only the requested document result. Do not link QA intermediates unless the user explicitly asks for them.
Immediately before the first create/edit authoring command, run mark_artifact_operation_started.mjs successfully exactly once using the command below. Do not run it for read-only work. For edits, replace create with edit; adjust the expected count and output format to match the requested outputs.
node container_tools/mark_artifact_operation_started.mjs --operation-kind create --expected-output-count 1 --output-format docx
Artifact Template Selection
When creating new documents without a template, reference, or visual direction, or when the user asks to browse or upload templates, read template selection before choosing a design or starting creation.
Google Docs-targeted output
For a net-new Google Docs request, create and visually verify a local .docx with this skill first. The native Google Docs deliverable must then be produced by the Google Drive plugin's document import action, mcp__codex_apps__google_drive_import_document, with upload_mode: "native_google_docs".
Before rendering or importing any Google Docs-targeted DOCX, run the deterministic title sanitizer:
python scripts/google_docs_title_sanitize.py input.docx --out sanitized.docx
python scripts/google_docs_title_sanitize.py sanitized.docx --check
Use the sanitized DOCX for render QA and native Google Docs import. This is not a style preference or prose reminder: the sanitizer removes Word Title paragraph-style border residue, direct title-paragraph borders, and leading title-block paragraph borders from the OOXML so Word's built-in blue title rule cannot survive into the imported Google Doc.
Do not use Computer Use, Browser Use, blank-Google-Doc creation plus Google Docs write APIs, or another direct-to-Docs construction path for net-new Google Docs unless the user explicitly asks for that alternate workflow. If they do, mention first that output quality is expected to be best when a local .docx is imported through the Google Drive plugin.
If the Google Drive plugin is unavailable, install google-drive@openai-curated yourself and continue. If the plugin is available but _import_document is missing, reinstall or refresh the Google Drive plugin, then continue with the native Google Docs deliverable.
Template Following
When an attached or retained DOCX is meant to control a new document, read
template-distill.md and then template-create.md. Keep the reference file and
the task-local $TMP_DIR/artifact.md together throughout authoring. In this
mode, the retained reference is the design authority: do not apply a generic
design preset, page baseline, or header pattern unless the user explicitly asks
to depart from the template. The render gate and Google Docs import contract
still apply. For a Google Docs-targeted result, record any change made by the
required title sanitizer as an intentional fidelity deviation.
Non-negotiable: render → inspect PNGs → iterate
You do not “know” a DOCX is satisfactory until you’ve rendered it and visually inspected page images.
DOCX text extraction (or reading XML) will miss layout defects: clipping, overlap, missing glyphs, broken tables, spacing drift, and header/footer issues.
Shipping gate: before delivering any DOCX, you must:
- Run
render_docx.py to produce page-<N>.png images (optionally also a PDF with --emit_pdf)
- Open the PNGs (100% zoom) and confirm every page is clean
- If anything looks off, fix the DOCX and re-render (repeat until flawless)
If rendering fails, diagnose the packaged renderer using its logs before retrying.
Deliverable discipline: Rendered artifacts (PNGs and optional PDFs) are for internal QA only. Unless the user explicitly asks for intermediates, return only the requested final deliverable (e.g., when the task asks for a DOCX, deliver the DOCX — not page images or PDFs).
Design standards for document generation
For generating new documents or major rewrite/repackages, follow the design standards below unless the user explicitly requests otherwise. The user's instructions always take precedence; otherwise, adhere to these standards.
When creating the document design, do not compromise on the content and make factual/technical errors. Do not produce something that looks polished but not actually what the user requested.
It is very important that the document is professional and aesthetically pleasing. As such, you should follow this general workflow to make your final delivered document:
- Before you make the DOCX, please first think about the high-level design of the DOCX:
- Before creating the document, decide what kind of document it is (for example, a memo, report, SOP, workflow, form, proposal, or manual) and design accordingly. In general, you shall create documents which are professional, visually polished, and aesthetically pleasing. However, you should also calibrate the level of styling to the document's purpose: for formal, serious, or highly utilitarian documents, visual appeal should come mainly from strong typography, spacing, hierarchy, and overall polish rather than expressive styling. The goal is for the document's visual character to feel appropriate to its real-world use case, with readability and usability always taking priority.
- You should make documents that feel visually natural. If a human looks at your document, they should find the design natural and smooth. This is very important; please think carefully about how to achieve this.
- Think about how you would like the first page to be organized. How about subsequent pages? What about the placement of the title? What does the heading ladder look like? Should there be a clear hierarchy? etc
- Would you like to include visual components, such as tables, checklists, images, etc? If yes, then plan out the design for each component.
- Think about the general spacing and layout. What will be the default body spacing? What page budget is allocated between packaging and substance? How will page breaks behave around tables and figures, since we must make sure to avoid large blank gaps, keep captions and their visuals together when possible, and keep content from becoming too wide by maintaining generous side margins so the page feels balanced and natural.
- Think about font, type scale, consistent accent treatment, etc. Try to avoid forcing large chunks of small text into narrow areas. When space is tight, adjust font size, line breaks, alignment, or layout instead of cramming in more text.
- Once you have a working DOCX, continue iterating until the entire document is polished and correct. After every change or edit, render the DOCX and review it carefully to evaluate the result. The plan from (1) should guide you, but it is only a flexible draft; you should update your decisions as needed throughout the revision process. Important: each time you render and reflect, you should check for both:
- Design aesthetics: the document should be aesthetically pleasing and easy to skim. Ask yourself: if a human were to look at my document, would they find it aesthetically nice? It should feel natural, smooth, and visually cohesive.
- Formatting issues that need to be fixed: e.g. text overlap, overflow, cramped spacing between adjacent elements, awkward spacing in tables/charts, awkward page breaks, etc. This is super important. Do not stop revising until all formatting issues are fixed.
While making and revising the DOCX, please adhere to and check against these quality reminders, to ensure the deliverable is visually high quality:
- Document density: Try to avoid having verbose dense walls of text, unless it's necessary. Avoid long runs of consecutive plain paragraphs or too many words before visual anchors. For some tasks this may be necessary (i.e. verbose legal documents); in those cases ignore this suggestion.
- Font: Use professional, easy-to-read font choices with appropriate size that is not too small. Usage of bold, underlines, and italics should be professional.
- Color: Set all document titles, subtitles, headings, subheadings, and page headers to black (
#000000). Apply black to their styles and remove theme colors or direct formatting that would override it. For table header rows, use the fill and text colors specified in the table guidance below.
- Visuals: Consider using tables, diagrams, and other visual components when they improve comprehension, navigation, or usability.
- Tables:
- Use tables intentionally and only for these purposes:
- Comparing multiple items across the same set of attributes.
- Presenting numeric data, metrics, specifications, pricing, dates, or other values readers need to scan across.
- Showing a compact matrix, such as options × criteria, roles × responsibilities, or risks × mitigations.
- Presenting repeated records with a consistent schema.
- Keep long explanations, research findings, and proposed policy language in prose under descriptive headings. Use a compact matrix to summarize fields readers need to compare. Review consecutive table pages and replace tables that merely arrange narrative paragraphs into cells. Keep long tables only when readers need the full set of comparable records together.
- Suggestions:
- Set deliberate table/cell widths and heights instead of defaulting to full page width.
- Choose column widths intentionally rather than giving every column equal width by default. Very short fields (for example: item number, checkbox, score, result, year, date, or status) should usually be kept compact, while wider columns should be reserved for longer content.
- Avoid overly wide tables, and leave generous side margins so the layout feels natural.
- Keep all text vertically centered and make deliberate horizontal alignment choices.
- Ensure cell height avoids a crowded look. Leave clear vertical spacing between a table and its caption or following text.
- Hard constraints:
- Borders: Explicitly set outer and internal cell borders to light gray (
#D9D9D9) so every table has visible borders.
- Header colors: Choose light gray, dark gray, dark blue, or light blue header fills to suit the document; do not default every table to light gray. Keep related tables consistent. Use white header text on dark fills and black text on light fills.
- Row shading: With a dark gray or dark blue header, alternate body-row backgrounds between white and a pale gray or pale blue tint. Keep the light gray borders visible.
- To prevent clipping/overflow:
- Never use fixed row heights that can truncate text; allow rows to expand with wrapped content.
- Ensure cell padding and line spacing are sufficient so descenders/ascenders don't get clipped.
- If content is tight, prefer (in order): wrap text -> adjust column widths -> reduce font slightly -> abbreviate headers/use two-line headers.
- Padding / breathing room: Ensure text doesn't sit against cell borders or look "pinned" to the upper-left. Favor generous internal padding on all sides, and keep it consistent across the table.
- Vertical alignment: In general, you should center your text vertically. Make sure that the content uses the available cell space naturally rather than clustering at the top.
- Horizontal alignment: Do not default all body cells to top-left alignment. Choose horizontal alignment intentionally by column type: centered alignment often works best for short values, status fields, dates, numbers, and check indicators; left alignment is usually better for narrative or multi-line text.
- Line height inside cells: Use line spacing that avoids a cramped feel and prevents ascenders/descenders from looking clipped. If a cell feels tight, adjust wrapping/width/padding before shrinking type.
- Width + wrapping sanity check: Avoid default equal-width columns when the content in each column clearly has different sizes. Avoid lines that run so close to the right edge that the cell feels overfull. If this happens, prefer wrapping or column-width adjustments before reducing font size.
- Spacing around tables: Keep clear separation between tables and surrounding text (especially the paragraph immediately above/below) so the layout doesn't feel stuck together. Captions and tables should stay visually paired, with deliberate spacing.
- Quick visual QA pass: Look for text that appears "boundary-hugging", specifically content pressed against the top or left edge of a cell or sitting too close beneath a table. Also watch for overly narrow descriptive columns and short-value columns whose contents feel awkwardly pinned. Correct these issues through padding, alignment, wrapping, or small column-width adjustments.
- Forms / questionnaires: Design these as a usable form, not a spreadsheet.
- Prioritize clear response options, obvious and well-sized check targets, readable scale labels, generous row height, clear section hierarchy, light visual structure. Please size fields and columns based on the content they hold rather than by equal-width table cells.
- Use spacing, alignment, and subtle header/section styling to organize the page. Avoid dense full-grid borders, cramped layouts, and ambiguous numeric-only response areas.
- Coherence vs. fragmentation: In general, try to keep things to be one coherent representation rather than fragmented, if possible.
- For example, don't split one logical dataset across multiple independent tables unless there's a clear, labeled reason.
- For example, if a table must span across pages, continue to the next page with a repeated header and consistent column order
- Callouts: Do not use callout boxes, shaded note cards, accent-bar blocks, or boxed summaries and decision panels. Present this content as ordinary paragraphs, optionally with a bold lead-in. This applies whether the callout is built with a table, text box, shape, or paragraph shading/borders.
- Spacing: Please check rigorously for spacing issues. Please always use a natural amount of spacing between adjacent components. Use clear, generous vertical spacing between sections and paragraphs, and leave a bit of extra space between subheadings and the content that follows when it improves readability. Use indentation and alignment intentionally so the document's hierarchy is immediately clear. At the same time, avoid large "layout gaps" caused by a table or chart not fitting at the bottom of a page and getting pushed to the next one. If this happens, please try these suggestions:
- moving the preceding paragraph(s) with it to the next page to keep the narrative cohesive
- scaling the visual modestly or simplify labels without hurting readability, formatting, or aesthetics of the visual
- Splitting the table/figure cleanly across multiple pages, but use repeated headers to make the page continuation clear.
- Text boxes: For text boxes, please follow the same breathing-room rules as the tables: make sure to use generous internal padding, intentional alignment, and sufficient line spacing so text never feels cramped, clipped, or pinned to the edges. Keep spacing around the text box clear so it remains visually distinct from surrounding content, and if the content feels tight, prefer adjusting box size, padding, or text wrapping before reducing font size.
- Layout/archetype: Remember to choose the right document archetype/template (proposal, SOP, workflow, form, handbook, etc.). Use a coherent style system. Once a style system is chosen, apply it consistently across headings, spacing, table treatments, and accent usage. If appropriate to the document type, include a cover page or front-matter elements such as title, subtitle, metadata, or branding.
Note on page sizing
When creating a new DOCX, always default to the Letter size 8.5 x 11 inches, in Portrait orientation, unless the user specifies otherwise.
Note on font sizing
Use a readable size appropriate to the text's role and typeface; ~11-12 pt is a good default for sustained prose. Use text 10 pt and below only if ideal for secondary roles or constrained tables/forms, and only when it remains comfortable at normal print or fit-width viewing. Do not shrink type merely to meet a page-count or compactness target. Follow explicit user typography instructions, but never at the expense of practical readability.
Editing tasks (DOCX edits) — apply instead of major rewrite behavior
When the user asks to edit an existing document, preserve the original and make minimal, local changes:
- Prefer inline edits (small replacements) over rewriting whole paragraphs.
- Use clear inline annotations/comments at the point of change (margin comments or comment markers). Don’t move all feedback to the end.
- Keep the original structure unless there’s a strong reason; if a restructure is needed, do it surgically and explain via comments.
- Don’t “cross out everything and rewrite”; avoid heavy, blanket deletions. The goal is trackable improvements, not a fresh draft unless explicitly requested.
Equations: native Word math vs rendered fallback
When the requested document or source contains mathematical equations, choose the equation
representation deliberately. Never leave raw LaTeX in the document or approximate structured
notation with plain text.
- Prefer native Word equations (OMML, such as
<m:oMath> or <m:oMathPara>) when the user
asks for native or editable equations, when an existing DOCX already uses native equations, or
when equations need to remain searchable, accessible, copyable, inline with prose, or easy to
revise. Use native equations only through a tested OMML authoring path, and verify that Word and
the final LibreOffice render preserve the notation correctly.
- Use the rendered MathJax fallback below when native/editable math is not required and either
no reliable OMML authoring path is available or a complex display equation needs predictable
visual fidelity across renderers. This path produces an image, not a native Word equation. It is
best for stable display equations where portability matters more than editability.
Do not silently rasterize an equation when the user explicitly requires native or editable Word
math. If no tested OMML path is available, explain that limitation rather than mislabeling an image
as native. When editing an existing DOCX, preserve its equation representation unless the request or
render QA gives a clear reason to change it.
Rendered fallback: MathJax to high-resolution PNG
The standard artifact container includes Node.js, mathjax-full, sharp, and python-docx. Use
MathJax to render LaTeX to SVG, then rasterize it to a high-resolution transparent PNG for reliable
insertion with python-docx and reliable LibreOffice rendering:
"use strict";
const sharp = require("sharp");
let _mathjax;
let _adaptor;
let _doc;
function ensureMathJax() {
if (_mathjax && _adaptor && _doc) return;
const { mathjax } = require("mathjax-full/js/mathjax.js");
const { TeX } = require("mathjax-full/js/input/tex.js");
const { SVG } = require("mathjax-full/js/output/svg.js");
const { liteAdaptor } = require("mathjax-full/js/adaptors/liteAdaptor.js");
const { RegisterHTMLHandler } = require("mathjax-full/js/handlers/html.js");
const { AllPackages } = require("mathjax-full/js/input/tex/AllPackages.js");
_adaptor = liteAdaptor();
RegisterHTMLHandler(_adaptor);
const tex = new TeX({ packages: AllPackages });
const out = new SVG({ fontCache: "local" });
_doc = mathjax.document("", { InputJax: tex, OutputJax: out });
_mathjax = mathjax;
}
function latexToSvgDataUri(latex, display = true) {
ensureMathJax();
const html = _adaptor.outerHTML(_doc.convert(latex, { display }));
const a = html.indexOf("<svg");
const b = html.indexOf("</svg>");
let svg = a !== -1 && b !== -1 ? html.slice(a, b + 6) : html;
svg = svg.replace(/<\?xml[^>]*>/g, "");
if (!/xmlns="http:\/\/www\.w3\.org\/2000\/svg"/.test(svg)) {
svg = svg.replace(/<svg /, '<svg xmlns="http://www.w3.org/2000/svg" ');
}
svg = svg.replace(/(width|height)="([0-9.]+)(ex|em)"/g, (_m, attr, num) => {
const px = Math.round(parseFloat(num) * 8.5);
return `${attr}="${px}px"`;
});
svg = svg.replace(/currentColor/g, "#000000");
return "data:image/svg+xml;base64," + Buffer.from(svg).toString("base64");
}
async function latexToPng(latex, outputPath, display = true) {
const dataUri = latexToSvgDataUri(latex, display);
const svg = Buffer.from(dataUri.split(",", 2)[1], "base64");
await sharp(svg, { density: 300 }).png().toFile(outputPath);
}
latexToPng(
String.raw`\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}`,
"/mnt/data/equation.png",
).catch((error) => {
console.error(error);
process.exit(1);
});
Insert the PNG at an intentional physical size without stretching it:
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.shared import Inches
doc = Document()
paragraph = doc.add_paragraph()
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
paragraph.add_run().add_picture("/mnt/data/equation.png", width=Inches(2.6))
doc.save("/mnt/data/output/equations.docx")
Use String.raw for LaTeX strings so JavaScript preserves backslashes. Choose display=true for
standalone equations and display=false for compact inline-style expressions. After using either
native OMML or the rendered fallback, run the normal render_docx.py workflow and inspect every
equation in the rendered page PNGs for missing glyphs, clipping, blur, poor sizing, or bad page
breaks.
Quick start (common one-liners)
# 1) Render any DOCX to PNGs (visual QA)
python render_docx.py input.docx --output_dir out
# 2) Remove reviewer comments (finalization)
python scripts/comments_strip.py input.docx --out no_comments.docx
# 3) Accept tracked changes (finalization)
python scripts/accept_tracked_changes.py input.docx --mode accept --out accepted.docx
# 4) Accessibility audit (+ optional safe fixes)
python scripts/a11y_audit.py input.docx
python scripts/a11y_audit.py input.docx --out_json a11y_report.json
python scripts/a11y_audit.py input.docx --fix_image_alt from_filename --out a11y_fixed.docx
# 5) Redact sensitive text (layout-preserving by default)
python scripts/redact_docx.py input.docx redacted.docx --emails --phones
Package layout
This skill is organized for progressive discovery: start here, then jump into task- or OOXML-specific docs.
DOCS SKILL PACKAGE
Root:
- SKILL.md: short overview + routing
- manifest.txt: machine-readable list of files to download (one relative path per line)
- render_docx.py: canonical DOCX→PNG renderer (container-safe LO profile + writable HOME + verbose logs)
Tasks:
- tasks/read_review.md
- tasks/create_edit.md
- tasks/verify_render.md
- tasks/accessibility_a11y.md
- tasks/comments_manage.md
- tasks/protection_restrict_editing.md
- tasks/privacy_scrub_metadata.md
- tasks/multi_doc_merge.md
- tasks/style_lint_normalize.md
- tasks/forms_content_controls.md
- tasks/captions_crossrefs.md
- tasks/redaction_anonymization.md
- tasks/clean_tracked_changes.md
- tasks/compare_diff.md
- tasks/templates_style_packs.md
- tasks/watermarks_background.md
- tasks/footnotes_endnotes.md
- tasks/fixtures_edge_cases.md
- tasks/navigation_internal_links.md
OOXML:
- ooxml/tracked_changes.md
- ooxml/comments.md
- ooxml/hyperlinks_and_fields.md
- ooxml/rels_and_content_types.md
Troubleshooting:
- troubleshooting/libreoffice_headless.md
- troubleshooting/run_splitting.md
Scripts:
Core building blocks (importable helpers):
scripts/docx_ooxml_patch.py — low-level OOXML patch helper (tracked changes, comments, hyperlinks, relationships). Other scripts reuse this.
scripts/fields_materialize.py — materialize SEQ/REF field display text for deterministic headless rendering/QA.
High-leverage utilities (also importable, but commonly invoked as CLIs):
render_docx.py — canonical DOCX → PNG renderer (optional PDF via --emit_pdf; do not deliver intermediates unless asked).
scripts/render_and_diff.py — render + per-page image diff between two DOCXs.
scripts/content_controls.py — list / wrap / fill Word content controls (SDTs) for forms/templates.
scripts/captions_and_crossrefs.py — insert Caption paragraphs for tables/figures + optional bookmarks around caption numbers.
scripts/insert_ref_fields.py — replace [[REF:bookmark]] markers with real REF fields (cross-references).
scripts/internal_nav.py — add internal navigation links (static TOC + Top/Bottom + figN/tblN jump links).
scripts/style_lint.py — report common formatting/style inconsistencies.
scripts/style_normalize.py — conservative cleanup (clear run-level overrides; optional paragraph overrides).
scripts/redact_docx.py — layout-preserving redaction/anonymization.
scripts/privacy_scrub.py — remove personal metadata + rsid* attributes.
scripts/set_protection.py — restrict editing (read-only / comments / forms).
scripts/comments_extract.py — extract comments to JSON (text, author/date, resolved flag, anchored snippets).
scripts/comments_strip.py — remove all comments (final-delivery mode).
Audits / conversions / niche helpers:
scripts/fields_report.py, scripts/heading_audit.py, scripts/section_audit.py, scripts/images_audit.py, scripts/footnotes_report.py, scripts/watermark_audit_remove.py
scripts/xlsx_to_docx_table.py, scripts/docx_table_to_csv.py
scripts/insert_toc.py, scripts/insert_note.py, scripts/apply_template_styles.py, scripts/accept_tracked_changes.py, scripts/make_fixtures.py
v7 additions (stress-test helpers):
scripts/watermark_add.py — add a detectable VML watermark object into an existing header.
scripts/comments_add.py — add multiple comments (by paragraph substring match) and wire up comments.xml plumbing if needed.
scripts/comments_apply_patch.py — append/replace comment text and mark/clear resolved state (w:done=1).
scripts/add_tracked_replacements.py — generate tracked-change replacements (<w:del> + <w:ins>) in-place.
scripts/a11y_audit.py — audit a11y issues; can also apply simple fixes via --fix_table_headers / --fix_image_alt.
scripts/flatten_ref_fields.py — replace REF/PAGEREF field blocks with their cached visible text for deterministic rendering.
scripts/xlsx_to_docx_table.py also marks header rows as repeating headers (w:tblHeader) to improve a11y and multi-page tables.
Examples:
- examples/end_to_end_smoke_test.md
Note: manifest.txt is machine-readable and is used by download tooling. It must contain only relative file paths (one per line).
Coverage map (scripts ↔ task guides)
This is a quick index so you can jump from a helper script to the right task guide.
Layout & style
style_lint.py, style_normalize.py → tasks/style_lint_normalize.md
apply_template_styles.py → tasks/templates_style_packs.md
section_audit.py → tasks/sections_layout.md
heading_audit.py → tasks/headings_numbering.md
Figures / images
images_audit.py, a11y_audit.py → tasks/images_figures.md, tasks/accessibility_a11y.md
captions_and_crossrefs.py → tasks/captions_crossrefs.md
Tables / spreadsheets
xlsx_to_docx_table.py → tasks/tables_spreadsheets.md
docx_table_to_csv.py → tasks/tables_spreadsheets.md
Fields & references
fields_report.py, fields_materialize.py → tasks/fields_update.md
insert_ref_fields.py, flatten_ref_fields.py → tasks/fields_update.md, tasks/captions_crossrefs.md
insert_toc.py → tasks/toc_workflow.md
Review lifecycle (comments / tracked changes)
add_tracked_replacements.py, accept_tracked_changes.py → tasks/clean_tracked_changes.md
comments_add.py, comments_extract.py, comments_apply_patch.py, comments_strip.py → tasks/comments_manage.md
Privacy / publishing
privacy_scrub.py → tasks/privacy_scrub_metadata.md
redact_docx.py → tasks/redaction_anonymization.md
watermark_add.py, watermark_audit_remove.py → tasks/watermarks_background.md
Navigation & multi-doc assembly
internal_nav.py → tasks/navigation_internal_links.md
merge_docx_append.py → tasks/multi_doc_merge.md
Forms & protection
content_controls.py → tasks/forms_content_controls.md
set_protection.py → tasks/protection_restrict_editing.md
QA / regression
render_and_diff.py, render_docx.py → tasks/compare_diff.md, tasks/verify_render.md
make_fixtures.py → tasks/fixtures_edge_cases.md
docx_ooxml_patch.py → used across guides for targeted patches
Skill folder contents
tasks/ — task playbooks (what to do step-by-step)
ooxml/ — advanced OOXML patches (tracked changes, comments, hyperlinks, fields)
scripts/ — reusable helper scripts
examples/ — small runnable examples
template-distill.md — distill a retained DOCX into a task-local artifact.md
template-create.md — create from the retained DOCX and its artifact.md
Default workflow (80/20)
Rule of thumb: every meaningful edit batch must end with a render + PNG review. No exceptions.
"80/20" here means: follow the simplest workflow that covers most DOCX tasks reliably.
Golden path (don’t mix-and-match unless debugging):
- Author/edit with
python-docx (paragraphs, runs, styles, tables, headers/footers).
- Render → inspect PNGs immediately (DOCX → PNGs). Treat this as your feedback loop.
- Fix and repeat until the PNGs are visually perfect.
- Only if needed: use OOXML patching for tracked changes, comments, hyperlinks, or fields.
- Re-render and inspect again after any OOXML patch or layout-sensitive change.
- Deliver only after the latest PNG review passes (all pages, 100% zoom).
Visual review (recommended)
Use the packaged renderer (dedicated LibreOffice profile + writable HOME):
python render_docx.py /mnt/data/input.docx --output_dir /mnt/data/out
# If debugging LibreOffice:
python render_docx.py /mnt/data/input.docx --output_dir /mnt/data/out --verbose
# Optional: also write <input_stem>.pdf to --output_dir (for debugging/archival):
python render_docx.py /mnt/data/input.docx --output_dir /mnt/data/out --emit_pdf
Then inspect the generated page-<N>.png files.
Success criteria (render + visual QA):
- PNGs exist for each page
- Page count matches expectations
- Inspect every page at 100% zoom (no “spot check” for final delivery)
- No clipping/overlap, no broken tables, no missing glyphs, no header/footer misplacement
Note: LibreOffice sometimes prints scary-looking stderr (e.g., error : Unknown IO error) even when output is correct. Treat the render as successful if the PNGs exist and look right (and if you used --emit_pdf, the PDF exists and is non-empty).
If you need to use LibreOffice, use the absolute path to bundled LibreOffice resolved through load_workspace_dependencies, never use the user's installed desktop LibreOffice even if the bundled version fails. Include this instruction and the bundled path in every subagent handoff.
What rendering does and doesn’t validate
- Great for: layout correctness, fonts, spacing, tables, headers/footers, and whether tracked changes visually appear.
- Not reliable for: comments (often not rendered in headless PDF export). For comments, also do structural checks (comments.xml + anchors + rels + content-types).
Default behaviors for you to follow (unless the user specifies otherwise)
- Do not place a horizontal rule directly below a document title or subtitle. Use whitespace and typography to create separation instead.
- Avoid horizontal lines under other headers.
- Avoid headers/footers unless necessary/important to the document.
- The final doc should not contain mentions of how you interpreted the prompt / decisions you made / information you could not find (e.g. phrases like 'This report uses only the supplied findings' or 'Synthetic source material' or 'Internal working draft'); instead, these should be flagged to the user via preamble messages and in your final answer.
Quality reminders
- Don’t ship visible defects (clipped/overlapping text, broken tables, unreadable glyphs).
- Don’t leak tool citation tokens into the DOCX (convert them to normal human citations).
- Prefer ASCII punctuation (avoid exotic Unicode hyphens/dashes that render inconsistently).
Where to go next
- If the task is reading/reviewing:
tasks/read_review.md
- If the task is creating/editing:
tasks/create_edit.md
- If you need an accessibility audit (alt text, headings, tables, links):
tasks/accessibility_a11y.md
- If you need to extract or remove comments:
tasks/comments_manage.md
- If you need to restrict editing / make read-only:
tasks/protection_restrict_editing.md
- If you need to scrub personal metadata (author/rsid/custom props):
tasks/privacy_scrub_metadata.md
- If you need to merge/append DOCXs:
tasks/multi_doc_merge.md
- If you need format consistency / style cleanup:
tasks/style_lint_normalize.md
- If you need forms / content controls (SDTs):
tasks/forms_content_controls.md
- If you need captions + cross-references:
tasks/captions_crossrefs.md
- If you need redaction/anonymization:
tasks/redaction_anonymization.md
- If the task is verification/raster review:
tasks/verify_render.md
- If your render looks wrong but content is right (stale fields):
tasks/fields_update.md
- If you need a Table of Contents:
tasks/toc_workflow.md
- If you need internal navigation links (static TOC + Back-to-TOC + Top/Bottom):
tasks/navigation_internal_links.md
- If headings/numbering/TOC levels are messy:
tasks/headings_numbering.md
- If you have mixed portrait/landscape or margin weirdness:
tasks/sections_layout.md
- If images shift or overlap across renderers:
tasks/images_figures.md
- If you need spreadsheet ↔ table round-tripping:
tasks/tables_spreadsheets.md
- If you need tracked changes (redlines):
ooxml/tracked_changes.md
- If you need comments:
ooxml/comments.md
- If you need hyperlinks/fields/page numbers/headers:
ooxml/hyperlinks_and_fields.md
- If LibreOffice headless is failing:
troubleshooting/libreoffice_headless.md
- If you need a clean copy with tracked changes accepted:
tasks/clean_tracked_changes.md
- If you need to diff two DOCXs (render + per-page diff):
tasks/compare_diff.md
- If you need templates / style packs (DOTX):
tasks/templates_style_packs.md
- If you need watermark audit/removal:
tasks/watermarks_background.md
- If you need true footnotes/endnotes:
tasks/footnotes_endnotes.md
- If you want reproducible fixtures for edge cases:
tasks/fixtures_edge_cases.md
Final response citations
Place :codex-file-citation{...} inline in prose without wrapping it in backticks or a code block, not in a trailing list. Use purpose="source" for Q&A/no-op and purpose="output" for create/edit.
- [HARD REQUIREMENT] Create/edit: cite each final DOCX exactly once with a plain output citation. Summarize representative changes; do not cite every section/page or add a separate filename, path, or Markdown link. Example:
Created :codex-file-citation{path="/abs/path/launch-plan.docx" purpose="output"}, highlighting the rollout and owners.
- Q&A: do not edit/re-export. Inspect complete relevant pages and preserve material headings, question/table labels, footnotes, sources, and sample sizes; cite each needed page once.
For page-specific evidence, use a page number verified against the latest render/inspection:
:codex-file-citation{path="/abs/path/file.docx" purpose="source" artifact_kind="document" page_number="4"}
Document locators support only page_number; otherwise use a plain citation. Do not g
…(truncated)
1---2name: documents3description: Create, edit, redline, and comment on `.docx`, Word, and Google Docs-targeted document artifacts inside the container, with a strict render-and-verify workflow. Use `render_docx.py` to generate page PNGs (and optional PDF) for visual QA, then iterate until layout is flawless before delivering the final document.4---56# DOCX Skill (Read • Create • Edit • Redline • Comment)78## Titles + Intro to doc910**Title clarity is an absolute requirement.** State the specific subject and purpose so the reader understands what the document is for before reading the body. Use plain descriptive language with no slogans and no punctuation. Apply this to document titles, subtitles, and section titles, using only words, numbers, and spaces. Use Word’s `Title` paragraph style for document titles. Keep it black with no underlining, paragraph borders, or decorative lines beneath it. Remove any direct formatting or separately added lines that conflict.1112**The opening content is essential to the reader's understanding of the whole document.** Establish what the document covers, why it matters to this reader, and the main conclusion, decision, or task. Give enough context and scope to make the sections that follow easy to understand and show what the reader should learn or do.1314## Writing quality1516- Write for the intended reader. Identify the author, recipient, and what the reader needs to understand or do. Follow user instructions first, choose the requested document format, and preserve the style of an existing document or supplied reference.17- Write directly in the author's voice, using “I” or “we” when appropriate. Present the update, recommendation, or request to the recipient. Match the author's tone and relationship to that audience; do not invent experience, authority, commitments, or facts from style examples.18- Lead with the conclusion, decision, or request. Use concrete subjects, strong verbs, and natural sentences. State what changed, why it matters, and what evidence or constraint supports the claim. Keep necessary qualifications and distinguish facts, interpretation, recommendations, and uncertainty.19- Remove stock formulas, slogans, inflated significance, vague abstractions, unsupported authorities, canned empathy, and ornamental transitions. Avoid conspicuous rhetorical triads, forced contrasts, repetitive cadence, and punctuation used only for emphasis. Judge these patterns in context; an isolated phrase, accurate technical term, or useful contrast is not automatically a defect.20- Review both the writing and the rendered document. Check that claims are supported, the author's voice is consistent, and every page is readable and free of layout defects. Do not mention this editorial framework in the delivered document unless asked.2122Before formatting, read the title and section headings as an outline. Write connected paragraphs that explain relationships, and replace compressed labels or unnecessary compounds with natural wording. Preserve the source's meaning, including uncertainty, conditions, time periods, and comparisons. Use punctuation and passive voice in body text when they improve precision.2324For the review steps, examples, and more context, read [writing_quality.md](writing_quality.md#editorial-review-for-documents).252627Use this skill when you need to create or modify `.docx`, Word, or Google Docs-targeted document artifacts **in this container environment** and verify them visually.2829## Tools + Contract Requirements3031- Use Codex workspace dependencies for docx artifact work: resolve them through the workspace dependency loader or runtime skill, then treat the returned Node/Python runtimes and package directory as authoritative. Do not use system `node`, system `python`, global npm packages, or repo-local installs.32- For document creation and deterministic OOXML edits, it is still acceptable to use the bundled Python/OOXML helper scripts in this skill package when the JS surface is incomplete.33- Run any builder or helper file from a writable workspace or temp directory, not from the managed dependency directory itself.34- Final user-facing responses should describe only the requested document result. Do not link QA intermediates unless the user explicitly asks for them.3536Immediately before the first create/edit authoring command, run `mark_artifact_operation_started.mjs` successfully exactly once using the command below. Do not run it for read-only work. For edits, replace `create` with `edit`; adjust the expected count and output format to match the requested outputs.3738```bash39node container_tools/mark_artifact_operation_started.mjs --operation-kind create --expected-output-count 1 --output-format docx40```4142## Artifact Template Selection4344When creating new documents without a template, reference, or visual direction, or when the user asks to browse or upload templates, read [template selection](references/template-elicitation.md) before choosing a design or starting creation.4546## Google Docs-targeted output4748For a net-new Google Docs request, create and visually verify a local `.docx` with this skill first. The native Google Docs deliverable must then be produced by the Google Drive plugin's document import action, `mcp__codex_apps__google_drive_import_document`, with `upload_mode: "native_google_docs"`.4950Before rendering or importing any Google Docs-targeted DOCX, run the deterministic title sanitizer:5152```bash53python scripts/google_docs_title_sanitize.py input.docx --out sanitized.docx54python scripts/google_docs_title_sanitize.py sanitized.docx --check55```5657Use the sanitized DOCX for render QA and native Google Docs import. This is not a style preference or prose reminder: the sanitizer removes Word `Title` paragraph-style border residue, direct title-paragraph borders, and leading title-block paragraph borders from the OOXML so Word's built-in blue title rule cannot survive into the imported Google Doc.5859Do not use Computer Use, Browser Use, blank-Google-Doc creation plus Google Docs write APIs, or another direct-to-Docs construction path for net-new Google Docs unless the user explicitly asks for that alternate workflow. If they do, mention first that output quality is expected to be best when a local `.docx` is imported through the Google Drive plugin.6061If the Google Drive plugin is unavailable, install `google-drive@openai-curated` yourself and continue. If the plugin is available but `_import_document` is missing, reinstall or refresh the Google Drive plugin, then continue with the native Google Docs deliverable.6263## Template Following6465When an attached or retained DOCX is meant to control a new document, read66`template-distill.md` and then `template-create.md`. Keep the reference file and67the task-local `$TMP_DIR/artifact.md` together throughout authoring. In this68mode, the retained reference is the design authority: do not apply a generic69design preset, page baseline, or header pattern unless the user explicitly asks70to depart from the template. The render gate and Google Docs import contract71still apply. For a Google Docs-targeted result, record any change made by the72required title sanitizer as an intentional fidelity deviation.7374## Non-negotiable: render → inspect PNGs → iterate7576**You do not “know” a DOCX is satisfactory until you’ve rendered it and visually inspected page images.**77DOCX text extraction (or reading XML) will miss layout defects: clipping, overlap, missing glyphs, broken tables, spacing drift, and header/footer issues.7879**Shipping gate:** before delivering any DOCX, you must:80- Run `render_docx.py` to produce `page-<N>.png` images (optionally also a PDF with `--emit_pdf`)81- Open the PNGs (100% zoom) and confirm every page is clean82- If anything looks off, fix the DOCX and **re-render** (repeat until flawless)8384If rendering fails, diagnose the packaged renderer using its logs before retrying.8586**Deliverable discipline:** Rendered artifacts (PNGs and optional PDFs) are for internal QA only. Unless the user explicitly asks for intermediates, **return only the requested final deliverable** (e.g., when the task asks for a DOCX, deliver the DOCX — not page images or PDFs).8788899091## Design standards for document generation9293For generating new documents or major rewrite/repackages, follow the design standards below unless the user explicitly requests otherwise. The user's instructions always take precedence; otherwise, adhere to these standards.9495When creating the document design, do not compromise on the content and make factual/technical errors. Do not produce something that looks polished but not actually what the user requested.9697It is very important that the document is professional and aesthetically pleasing. As such, you should follow this general workflow to make your final delivered document:98991. Before you make the DOCX, please first think about the high-level design of the DOCX:100 - Before creating the document, decide what kind of document it is (for example, a memo, report, SOP, workflow, form, proposal, or manual) and design accordingly. In general, you shall create documents which are professional, visually polished, and aesthetically pleasing. However, you should also calibrate the level of styling to the document's purpose: for formal, serious, or highly utilitarian documents, visual appeal should come mainly from strong typography, spacing, hierarchy, and overall polish rather than expressive styling. The goal is for the document's visual character to feel appropriate to its real-world use case, with readability and usability always taking priority.101 - You should make documents that feel visually natural. If a human looks at your document, they should find the design natural and smooth. This is very important; please think carefully about how to achieve this.102 - Think about how you would like the first page to be organized. How about subsequent pages? What about the placement of the title? What does the heading ladder look like? Should there be a clear hierarchy? etc103 - Would you like to include visual components, such as tables, checklists, images, etc? If yes, then plan out the design for each component.104 - Think about the general spacing and layout. What will be the default body spacing? What page budget is allocated between packaging and substance? How will page breaks behave around tables and figures, since we must make sure to avoid large blank gaps, keep captions and their visuals together when possible, and keep content from becoming too wide by maintaining generous side margins so the page feels balanced and natural.105 - Think about font, type scale, consistent accent treatment, etc. Try to avoid forcing large chunks of small text into narrow areas. When space is tight, adjust font size, line breaks, alignment, or layout instead of cramming in more text.1062. Once you have a working DOCX, continue iterating until the entire document is polished and correct. After every change or edit, render the DOCX and review it carefully to evaluate the result. The plan from (1) should guide you, but it is only a flexible draft; you should update your decisions as needed throughout the revision process. Important: each time you render and reflect, you should check for both:107 1. Design aesthetics: the document should be aesthetically pleasing and easy to skim. Ask yourself: if a human were to look at my document, would they find it aesthetically nice? It should feel natural, smooth, and visually cohesive.108 2. Formatting issues that need to be fixed: e.g. text overlap, overflow, cramped spacing between adjacent elements, awkward spacing in tables/charts, awkward page breaks, etc. This is super important. Do not stop revising until all formatting issues are fixed.109110While making and revising the DOCX, please adhere to and check against these quality reminders, to ensure the deliverable is visually high quality:111112- Document density: Try to avoid having verbose dense walls of text, unless it's necessary. Avoid long runs of consecutive plain paragraphs or too many words before visual anchors. For some tasks this may be necessary (i.e. verbose legal documents); in those cases ignore this suggestion.113- Font: Use professional, easy-to-read font choices with appropriate size that is not too small. Usage of bold, underlines, and italics should be professional.114- Color: Set all document titles, subtitles, headings, subheadings, and page headers to black (`#000000`). Apply black to their styles and remove theme colors or direct formatting that would override it. For table header rows, use the fill and text colors specified in the table guidance below.115- Visuals: Consider using tables, diagrams, and other visual components when they improve comprehension, navigation, or usability.116- Tables:117 - Use tables intentionally and only for these purposes:118 - Comparing multiple items across the same set of attributes.119 - Presenting numeric data, metrics, specifications, pricing, dates, or other values readers need to scan across.120 - Showing a compact matrix, such as options × criteria, roles × responsibilities, or risks × mitigations.121 - Presenting repeated records with a consistent schema.122 - Keep long explanations, research findings, and proposed policy language in prose under descriptive headings. Use a compact matrix to summarize fields readers need to compare. Review consecutive table pages and replace tables that merely arrange narrative paragraphs into cells. Keep long tables only when readers need the full set of comparable records together.123 - Suggestions:124 - Set deliberate table/cell widths and heights instead of defaulting to full page width.125 - Choose column widths intentionally rather than giving every column equal width by default. Very short fields (for example: item number, checkbox, score, result, year, date, or status) should usually be kept compact, while wider columns should be reserved for longer content.126 - Avoid overly wide tables, and leave generous side margins so the layout feels natural.127 - Keep all text vertically centered and make deliberate horizontal alignment choices.128 - Ensure cell height avoids a crowded look. Leave clear vertical spacing between a table and its caption or following text.129 - Hard constraints:130 - Borders: Explicitly set outer and internal cell borders to light gray (`#D9D9D9`) so every table has visible borders.131 - Header colors: Choose light gray, dark gray, dark blue, or light blue header fills to suit the document; do not default every table to light gray. Keep related tables consistent. Use white header text on dark fills and black text on light fills.132 - Row shading: With a dark gray or dark blue header, alternate body-row backgrounds between white and a pale gray or pale blue tint. Keep the light gray borders visible.133 - To prevent clipping/overflow:134 - Never use fixed row heights that can truncate text; allow rows to expand with wrapped content.135 - Ensure cell padding and line spacing are sufficient so descenders/ascenders don't get clipped.136 - If content is tight, prefer (in order): wrap text -> adjust column widths -> reduce font slightly -> abbreviate headers/use two-line headers.137 - Padding / breathing room: Ensure text doesn't sit against cell borders or look "pinned" to the upper-left. Favor generous internal padding on all sides, and keep it consistent across the table.138 - Vertical alignment: In general, you should center your text vertically. Make sure that the content uses the available cell space naturally rather than clustering at the top.139 - Horizontal alignment: Do not default all body cells to top-left alignment. Choose horizontal alignment intentionally by column type: centered alignment often works best for short values, status fields, dates, numbers, and check indicators; left alignment is usually better for narrative or multi-line text.140 - Line height inside cells: Use line spacing that avoids a cramped feel and prevents ascenders/descenders from looking clipped. If a cell feels tight, adjust wrapping/width/padding before shrinking type.141 - Width + wrapping sanity check: Avoid default equal-width columns when the content in each column clearly has different sizes. Avoid lines that run so close to the right edge that the cell feels overfull. If this happens, prefer wrapping or column-width adjustments before reducing font size.142 - Spacing around tables: Keep clear separation between tables and surrounding text (especially the paragraph immediately above/below) so the layout doesn't feel stuck together. Captions and tables should stay visually paired, with deliberate spacing.143 - Quick visual QA pass: Look for text that appears "boundary-hugging", specifically content pressed against the top or left edge of a cell or sitting too close beneath a table. Also watch for overly narrow descriptive columns and short-value columns whose contents feel awkwardly pinned. Correct these issues through padding, alignment, wrapping, or small column-width adjustments.144- Forms / questionnaires: Design these as a usable form, not a spreadsheet.145 - Prioritize clear response options, obvious and well-sized check targets, readable scale labels, generous row height, clear section hierarchy, light visual structure. Please size fields and columns based on the content they hold rather than by equal-width table cells.146 - Use spacing, alignment, and subtle header/section styling to organize the page. Avoid dense full-grid borders, cramped layouts, and ambiguous numeric-only response areas.147- Coherence vs. fragmentation: In general, try to keep things to be one coherent representation rather than fragmented, if possible.148 - For example, don't split one logical dataset across multiple independent tables unless there's a clear, labeled reason.149 - For example, if a table must span across pages, continue to the next page with a repeated header and consistent column order150- Callouts: Do not use callout boxes, shaded note cards, accent-bar blocks, or boxed summaries and decision panels. Present this content as ordinary paragraphs, optionally with a bold lead-in. This applies whether the callout is built with a table, text box, shape, or paragraph shading/borders.151- Spacing: Please check rigorously for spacing issues. Please always use a natural amount of spacing between adjacent components. Use clear, generous vertical spacing between sections and paragraphs, and leave a bit of extra space between subheadings and the content that follows when it improves readability. Use indentation and alignment intentionally so the document's hierarchy is immediately clear. At the same time, avoid large "layout gaps" caused by a table or chart not fitting at the bottom of a page and getting pushed to the next one. If this happens, please try these suggestions:152 - moving the preceding paragraph(s) with it to the next page to keep the narrative cohesive153 - scaling the visual modestly or simplify labels without hurting readability, formatting, or aesthetics of the visual154 - Splitting the table/figure cleanly across multiple pages, but use repeated headers to make the page continuation clear.155- Text boxes: For text boxes, please follow the same breathing-room rules as the tables: make sure to use generous internal padding, intentional alignment, and sufficient line spacing so text never feels cramped, clipped, or pinned to the edges. Keep spacing around the text box clear so it remains visually distinct from surrounding content, and if the content feels tight, prefer adjusting box size, padding, or text wrapping before reducing font size.156- Layout/archetype: Remember to choose the right document archetype/template (proposal, SOP, workflow, form, handbook, etc.). Use a coherent style system. Once a style system is chosen, apply it consistently across headings, spacing, table treatments, and accent usage. If appropriate to the document type, include a cover page or front-matter elements such as title, subtitle, metadata, or branding.157158### Note on page sizing159160When creating a new DOCX, **always** default to the Letter size 8.5 x 11 inches, in Portrait orientation, unless the user specifies otherwise.161162### Note on font sizing163164Use a readable size appropriate to the text's role and typeface; ~11-12 pt is a good default for sustained prose. Use text 10 pt and below only if ideal for secondary roles or constrained tables/forms, and only when it remains comfortable at normal print or fit-width viewing. Do not shrink type merely to meet a page-count or compactness target. Follow explicit user typography instructions, but never at the expense of practical readability.165166### Editing tasks (DOCX edits) — apply instead of major rewrite behavior167168When the user asks to edit an existing document, preserve the original and make minimal, local changes:169170- Prefer inline edits (small replacements) over rewriting whole paragraphs.171- Use clear inline annotations/comments at the point of change (margin comments or comment markers). Don’t move all feedback to the end.172- Keep the original structure unless there’s a strong reason; if a restructure is needed, do it surgically and explain via comments.173- Don’t “cross out everything and rewrite”; avoid heavy, blanket deletions. The goal is trackable improvements, not a fresh draft unless explicitly requested.174175## Equations: native Word math vs rendered fallback176177When the requested document or source contains mathematical equations, choose the equation178representation deliberately. Never leave raw LaTeX in the document or approximate structured179notation with plain text.1801811. **Prefer native Word equations (OMML, such as `<m:oMath>` or `<m:oMathPara>`)** when the user182 asks for native or editable equations, when an existing DOCX already uses native equations, or183 when equations need to remain searchable, accessible, copyable, inline with prose, or easy to184 revise. Use native equations only through a tested OMML authoring path, and verify that Word and185 the final LibreOffice render preserve the notation correctly.1862. **Use the rendered MathJax fallback below** when native/editable math is not required and either187 no reliable OMML authoring path is available or a complex display equation needs predictable188 visual fidelity across renderers. This path produces an image, not a native Word equation. It is189 best for stable display equations where portability matters more than editability.190191Do not silently rasterize an equation when the user explicitly requires native or editable Word192math. If no tested OMML path is available, explain that limitation rather than mislabeling an image193as native. When editing an existing DOCX, preserve its equation representation unless the request or194render QA gives a clear reason to change it.195196### Rendered fallback: MathJax to high-resolution PNG197198The standard artifact container includes Node.js, `mathjax-full`, `sharp`, and `python-docx`. Use199MathJax to render LaTeX to SVG, then rasterize it to a high-resolution transparent PNG for reliable200insertion with `python-docx` and reliable LibreOffice rendering:201202```javascript203"use strict";204205const sharp = require("sharp");206207let _mathjax;208let _adaptor;209let _doc;210211function ensureMathJax() {212 if (_mathjax && _adaptor && _doc) return;213 const { mathjax } = require("mathjax-full/js/mathjax.js");214 const { TeX } = require("mathjax-full/js/input/tex.js");215 const { SVG } = require("mathjax-full/js/output/svg.js");216 const { liteAdaptor } = require("mathjax-full/js/adaptors/liteAdaptor.js");217 const { RegisterHTMLHandler } = require("mathjax-full/js/handlers/html.js");218 const { AllPackages } = require("mathjax-full/js/input/tex/AllPackages.js");219220 _adaptor = liteAdaptor();221 RegisterHTMLHandler(_adaptor);222 const tex = new TeX({ packages: AllPackages });223 const out = new SVG({ fontCache: "local" });224 _doc = mathjax.document("", { InputJax: tex, OutputJax: out });225 _mathjax = mathjax;226}227228function latexToSvgDataUri(latex, display = true) {229 ensureMathJax();230 const html = _adaptor.outerHTML(_doc.convert(latex, { display }));231 const a = html.indexOf("<svg");232 const b = html.indexOf("</svg>");233 let svg = a !== -1 && b !== -1 ? html.slice(a, b + 6) : html;234 svg = svg.replace(/<\?xml[^>]*>/g, "");235 if (!/xmlns="http:\/\/www\.w3\.org\/2000\/svg"/.test(svg)) {236 svg = svg.replace(/<svg /, '<svg xmlns="http://www.w3.org/2000/svg" ');237 }238 svg = svg.replace(/(width|height)="([0-9.]+)(ex|em)"/g, (_m, attr, num) => {239 const px = Math.round(parseFloat(num) * 8.5);240 return `${attr}="${px}px"`;241 });242 svg = svg.replace(/currentColor/g, "#000000");243 return "data:image/svg+xml;base64," + Buffer.from(svg).toString("base64");244}245246async function latexToPng(latex, outputPath, display = true) {247 const dataUri = latexToSvgDataUri(latex, display);248 const svg = Buffer.from(dataUri.split(",", 2)[1], "base64");249 await sharp(svg, { density: 300 }).png().toFile(outputPath);250}251252latexToPng(253 String.raw`\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}`,254 "/mnt/data/equation.png",255).catch((error) => {256 console.error(error);257 process.exit(1);258});259```260261Insert the PNG at an intentional physical size without stretching it:262263```python264from docx import Document265from docx.enum.text import WD_ALIGN_PARAGRAPH266from docx.shared import Inches267268doc = Document()269paragraph = doc.add_paragraph()270paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER271paragraph.add_run().add_picture("/mnt/data/equation.png", width=Inches(2.6))272doc.save("/mnt/data/output/equations.docx")273```274275Use `String.raw` for LaTeX strings so JavaScript preserves backslashes. Choose `display=true` for276standalone equations and `display=false` for compact inline-style expressions. After using either277native OMML or the rendered fallback, run the normal `render_docx.py` workflow and inspect every278equation in the rendered page PNGs for missing glyphs, clipping, blur, poor sizing, or bad page279breaks.280281## Quick start (common one-liners)282283```bash284# 1) Render any DOCX to PNGs (visual QA)285python render_docx.py input.docx --output_dir out286287# 2) Remove reviewer comments (finalization)288python scripts/comments_strip.py input.docx --out no_comments.docx289290# 3) Accept tracked changes (finalization)291python scripts/accept_tracked_changes.py input.docx --mode accept --out accepted.docx292293# 4) Accessibility audit (+ optional safe fixes)294python scripts/a11y_audit.py input.docx295python scripts/a11y_audit.py input.docx --out_json a11y_report.json296python scripts/a11y_audit.py input.docx --fix_image_alt from_filename --out a11y_fixed.docx297298# 5) Redact sensitive text (layout-preserving by default)299python scripts/redact_docx.py input.docx redacted.docx --emails --phones300```301302## Package layout303304This skill is organized for progressive discovery: start here, then jump into task- or OOXML-specific docs.305306DOCS SKILL PACKAGE307308Root:309- SKILL.md: short overview + routing310- manifest.txt: machine-readable list of files to download (one relative path per line)311- render_docx.py: canonical DOCX→PNG renderer (container-safe LO profile + writable HOME + verbose logs)312313Tasks:314- tasks/read_review.md315- tasks/create_edit.md316- tasks/verify_render.md317- tasks/accessibility_a11y.md318- tasks/comments_manage.md319- tasks/protection_restrict_editing.md320- tasks/privacy_scrub_metadata.md321- tasks/multi_doc_merge.md322- tasks/style_lint_normalize.md323- tasks/forms_content_controls.md324- tasks/captions_crossrefs.md325- tasks/redaction_anonymization.md326- tasks/clean_tracked_changes.md327- tasks/compare_diff.md328- tasks/templates_style_packs.md329- tasks/watermarks_background.md330- tasks/footnotes_endnotes.md331- tasks/fixtures_edge_cases.md332- tasks/navigation_internal_links.md333334OOXML:335- ooxml/tracked_changes.md336- ooxml/comments.md337- ooxml/hyperlinks_and_fields.md338- ooxml/rels_and_content_types.md339340Troubleshooting:341- troubleshooting/libreoffice_headless.md342- troubleshooting/run_splitting.md343344Scripts:345346**Core building blocks (importable helpers):**347- `scripts/docx_ooxml_patch.py` — low-level OOXML patch helper (tracked changes, comments, hyperlinks, relationships). Other scripts reuse this.348- `scripts/fields_materialize.py` — materialize `SEQ`/`REF` field *display text* for deterministic headless rendering/QA.349350**High-leverage utilities (also importable, but commonly invoked as CLIs):**351- `render_docx.py` — canonical DOCX → PNG renderer (optional PDF via `--emit_pdf`; do not deliver intermediates unless asked).352- `scripts/render_and_diff.py` — render + per-page image diff between two DOCXs.353- `scripts/content_controls.py` — list / wrap / fill Word content controls (SDTs) for forms/templates.354- `scripts/captions_and_crossrefs.py` — insert Caption paragraphs for tables/figures + optional bookmarks around caption numbers.355- `scripts/insert_ref_fields.py` — replace `[[REF:bookmark]]` markers with real `REF` fields (cross-references).356- `scripts/internal_nav.py` — add internal navigation links (static TOC + Top/Bottom + figN/tblN jump links).357- `scripts/style_lint.py` — report common formatting/style inconsistencies.358- `scripts/style_normalize.py` — conservative cleanup (clear run-level overrides; optional paragraph overrides).359- `scripts/redact_docx.py` — layout-preserving redaction/anonymization.360- `scripts/privacy_scrub.py` — remove personal metadata + `rsid*` attributes.361- `scripts/set_protection.py` — restrict editing (read-only / comments / forms).362- `scripts/comments_extract.py` — extract comments to JSON (text, author/date, resolved flag, anchored snippets).363- `scripts/comments_strip.py` — remove all comments (final-delivery mode).364365**Audits / conversions / niche helpers:**366- `scripts/fields_report.py`, `scripts/heading_audit.py`, `scripts/section_audit.py`, `scripts/images_audit.py`, `scripts/footnotes_report.py`, `scripts/watermark_audit_remove.py`367- `scripts/xlsx_to_docx_table.py`, `scripts/docx_table_to_csv.py`368- `scripts/insert_toc.py`, `scripts/insert_note.py`, `scripts/apply_template_styles.py`, `scripts/accept_tracked_changes.py`, `scripts/make_fixtures.py`369370**v7 additions (stress-test helpers):**371- `scripts/watermark_add.py` — add a detectable VML watermark object into an existing header.372- `scripts/comments_add.py` — add multiple comments (by paragraph substring match) and wire up comments.xml plumbing if needed.373- `scripts/comments_apply_patch.py` — append/replace comment text and mark/clear resolved state (`w:done=1`).374- `scripts/add_tracked_replacements.py` — generate tracked-change replacements (`<w:del>` + `<w:ins>`) in-place.375- `scripts/a11y_audit.py` — audit a11y issues; can also apply simple fixes via `--fix_table_headers` / `--fix_image_alt`.376- `scripts/flatten_ref_fields.py` — replace REF/PAGEREF field blocks with their cached visible text for deterministic rendering.377378> `scripts/xlsx_to_docx_table.py` also marks header rows as repeating headers (`w:tblHeader`) to improve a11y and multi-page tables.379380Examples:381- examples/end_to_end_smoke_test.md382383> Note: `manifest.txt` is **machine-readable** and is used by download tooling. It must contain only relative file paths (one per line).384385386## Coverage map (scripts ↔ task guides)387388This is a quick index so you can jump from a helper script to the right task guide.389390### Layout & style391- `style_lint.py`, `style_normalize.py` → `tasks/style_lint_normalize.md`392- `apply_template_styles.py` → `tasks/templates_style_packs.md`393- `section_audit.py` → `tasks/sections_layout.md`394- `heading_audit.py` → `tasks/headings_numbering.md`395396### Figures / images397- `images_audit.py`, `a11y_audit.py` → `tasks/images_figures.md`, `tasks/accessibility_a11y.md`398- `captions_and_crossrefs.py` → `tasks/captions_crossrefs.md`399400### Tables / spreadsheets401- `xlsx_to_docx_table.py` → `tasks/tables_spreadsheets.md`402- `docx_table_to_csv.py` → `tasks/tables_spreadsheets.md`403404### Fields & references405- `fields_report.py`, `fields_materialize.py` → `tasks/fields_update.md`406- `insert_ref_fields.py`, `flatten_ref_fields.py` → `tasks/fields_update.md`, `tasks/captions_crossrefs.md`407- `insert_toc.py` → `tasks/toc_workflow.md`408409### Review lifecycle (comments / tracked changes)410- `add_tracked_replacements.py`, `accept_tracked_changes.py` → `tasks/clean_tracked_changes.md`411- `comments_add.py`, `comments_extract.py`, `comments_apply_patch.py`, `comments_strip.py` → `tasks/comments_manage.md`412413### Privacy / publishing414- `privacy_scrub.py` → `tasks/privacy_scrub_metadata.md`415- `redact_docx.py` → `tasks/redaction_anonymization.md`416- `watermark_add.py`, `watermark_audit_remove.py` → `tasks/watermarks_background.md`417418### Navigation & multi-doc assembly419- `internal_nav.py` → `tasks/navigation_internal_links.md`420- `merge_docx_append.py` → `tasks/multi_doc_merge.md`421422### Forms & protection423- `content_controls.py` → `tasks/forms_content_controls.md`424- `set_protection.py` → `tasks/protection_restrict_editing.md`425426### QA / regression427- `render_and_diff.py`, `render_docx.py` → `tasks/compare_diff.md`, `tasks/verify_render.md`428- `make_fixtures.py` → `tasks/fixtures_edge_cases.md`429- `docx_ooxml_patch.py` → used across guides for targeted patches430431## Skill folder contents432- `tasks/` — task playbooks (what to do step-by-step)433- `ooxml/` — advanced OOXML patches (tracked changes, comments, hyperlinks, fields)434- `scripts/` — reusable helper scripts435- `examples/` — small runnable examples436- `template-distill.md` — distill a retained DOCX into a task-local `artifact.md`437- `template-create.md` — create from the retained DOCX and its `artifact.md`438439## Default workflow (80/20)440441**Rule of thumb:** every meaningful edit batch must end with a render + PNG review. No exceptions.442"80/20" here means: follow the simplest workflow that covers *most* DOCX tasks reliably.443444**Golden path (don’t mix-and-match unless debugging):**4451. **Author/edit with `python-docx`** (paragraphs, runs, styles, tables, headers/footers).4462. **Render → inspect PNGs immediately** (DOCX → PNGs). Treat this as your feedback loop.4473. **Fix and repeat** until the PNGs are visually perfect.4484. **Only if needed**: use OOXML patching for tracked changes, comments, hyperlinks, or fields.4495. **Re-render and inspect again** after *any* OOXML patch or layout-sensitive change.4506. **Deliver only after the latest PNG review passes** (all pages, 100% zoom).451452## Visual review (recommended)453454Use the packaged renderer (dedicated LibreOffice profile + writable HOME):455456```bash457python render_docx.py /mnt/data/input.docx --output_dir /mnt/data/out458# If debugging LibreOffice:459python render_docx.py /mnt/data/input.docx --output_dir /mnt/data/out --verbose460# Optional: also write <input_stem>.pdf to --output_dir (for debugging/archival):461python render_docx.py /mnt/data/input.docx --output_dir /mnt/data/out --emit_pdf462```463464Then inspect the generated `page-<N>.png` files.465466**Success criteria (render + visual QA):**467- PNGs exist for each page468- Page count matches expectations469- **Inspect every page at 100% zoom** (no “spot check” for final delivery)470- No clipping/overlap, no broken tables, no missing glyphs, no header/footer misplacement471472**Note:** LibreOffice sometimes prints scary-looking stderr (e.g., `error : Unknown IO error`) even when output is correct. Treat the render as successful if the PNGs exist and look right (and if you used `--emit_pdf`, the PDF exists and is non-empty).473474If you need to use LibreOffice, use the absolute path to bundled LibreOffice resolved through `load_workspace_dependencies`, never use the user's installed desktop LibreOffice even if the bundled version fails. Include this instruction and the bundled path in every subagent handoff.475476### What rendering does and doesn’t validate477478- **Great for:** layout correctness, fonts, spacing, tables, headers/footers, and whether **tracked changes** visually appear.479- **Not reliable for:** **comments** (often not rendered in headless PDF export). For comments, also do **structural checks** (comments.xml + anchors + rels + content-types).480481## Default behaviors for you to follow (unless the user specifies otherwise)482- Do not place a horizontal rule directly below a document title or subtitle. Use whitespace and typography to create separation instead.483- Avoid horizontal lines under other headers.484- Avoid headers/footers unless necessary/important to the document.485- The final doc should not contain mentions of how you interpreted the prompt / decisions you made / information you could not find (e.g. phrases like 'This report uses only the supplied findings' or 'Synthetic source material' or 'Internal working draft'); instead, these should be flagged to the user via preamble messages and in your final answer.486487## Quality reminders488- Don’t ship visible defects (clipped/overlapping text, broken tables, unreadable glyphs).489- Don’t leak tool citation tokens into the DOCX (convert them to normal human citations).490- Prefer ASCII punctuation (avoid exotic Unicode hyphens/dashes that render inconsistently).491492## Where to go next493- If the task is **reading/reviewing**: `tasks/read_review.md`494- If the task is **creating/editing**: `tasks/create_edit.md`495- If you need an **accessibility audit** (alt text, headings, tables, links): `tasks/accessibility_a11y.md`496- If you need to **extract or remove comments**: `tasks/comments_manage.md`497- If you need to **restrict editing / make read-only**: `tasks/protection_restrict_editing.md`498- If you need to **scrub personal metadata** (author/rsid/custom props): `tasks/privacy_scrub_metadata.md`499- If you need to **merge/append DOCXs**: `tasks/multi_doc_merge.md`500- If you need **format consistency / style cleanup**: `tasks/style_lint_normalize.md`501- If you need **forms / content controls (SDTs)**: `tasks/forms_content_controls.md`502- If you need **captions + cross-references**: `tasks/captions_crossrefs.md`503- If you need **redaction/anonymization**: `tasks/redaction_anonymization.md`504- If the task is **verification/raster review**: `tasks/verify_render.md`505- If your render looks wrong but content is right (stale fields): `tasks/fields_update.md`506- If you need a **Table of Contents**: `tasks/toc_workflow.md`507- If you need **internal navigation links** (static TOC + Back-to-TOC + Top/Bottom): `tasks/navigation_internal_links.md`508- If headings/numbering/TOC levels are messy: `tasks/headings_numbering.md`509- If you have mixed portrait/landscape or margin weirdness: `tasks/sections_layout.md`510- If images shift or overlap across renderers: `tasks/images_figures.md`511- If you need spreadsheet ↔ table round-tripping: `tasks/tables_spreadsheets.md`512- If you need **tracked changes (redlines)**: `ooxml/tracked_changes.md`513- If you need **comments**: `ooxml/comments.md`514- If you need **hyperlinks/fields/page numbers/headers**: `ooxml/hyperlinks_and_fields.md`515- If LibreOffice headless is failing: `troubleshooting/libreoffice_headless.md`516- If you need a **clean copy** with tracked changes accepted: `tasks/clean_tracked_changes.md`517- If you need to **diff two DOCXs** (render + per-page diff): `tasks/compare_diff.md`518- If you need **templates / style packs (DOTX)**: `tasks/templates_style_packs.md`519- If you need **watermark audit/removal**: `tasks/watermarks_background.md`520- If you need **true footnotes/endnotes**: `tasks/footnotes_endnotes.md`521- If you want reproducible fixtures for edge cases: `tasks/fixtures_edge_cases.md`522523## Final response citations524525Place :codex-file-citation{...} inline in prose without wrapping it in backticks or a code block, not in a trailing list. Use `purpose="source"` for Q&A/no-op and `purpose="output"` for create/edit.526527- [HARD REQUIREMENT] Create/edit: cite each final DOCX exactly once with a plain output citation. Summarize representative changes; do not cite every section/page or add a separate filename, path, or Markdown link. Example: `Created :codex-file-citation{path="/abs/path/launch-plan.docx" purpose="output"}, highlighting the rollout and owners.`528- Q&A: do not edit/re-export. Inspect complete relevant pages and preserve material headings, question/table labels, footnotes, sources, and sample sizes; cite each needed page once.529530For page-specific evidence, use a page number verified against the latest render/inspection:531532:codex-file-citation{path="/abs/path/file.docx" purpose="source" artifact_kind="document" page_number="4"}533534Document locators support only `page_number`; otherwise use a plain citation. Do not g535536…(truncated)