Word documents (.docx)
A .docx is a zip of XML parts. Most jobs never need to look inside: python-docx
or pandoc does them. The scripts in this skill cover what those cannot do well:
tracked changes, comments, and checking that a hand-edited package is sound.
Paths below are relative to this skill's directory. Run scripts with the Python
that has the libraries installed.
Pick the route
| You want to |
Use |
| Turn Markdown, HTML or LaTeX into Word |
pandoc in.md -o out.docx (add --reference-doc=template.docx to borrow styles) |
| Build a document from data or code |
python-docx, see references/python-docx-recipes.md |
| Read text and tables |
python-docx, or pandoc in.docx -t markdown for a quick look |
| Change wording but keep formatting |
scripts/docx_track.py replace |
| Propose edits the author can accept or reject |
scripts/docx_track.py replace (tracked, the default) |
| Accept or reject someone's edits |
scripts/docx_track.py accept / reject |
| Review: add, read or remove comments |
scripts/docx_comments.py |
| Fix something no library exposes |
unpack, edit the XML, pack (it checks the result) |
| Check a file Word refuses to open |
scripts/ooxml_tools.py check |
Scripts
| Script |
Does |
docx_track.py list / accept / reject / replace |
tracked changes, optionally filtered with --author |
docx_comments.py list / add / remove |
comments anchored to an exact phrase |
ooxml_tools.py unpack / pack / check |
extract, rebuild and sanity-check the package |
docx_runs.py |
shared run-splitting code the two scripts import |
All editing scripts write to a new file with -o; they never overwrite the
input. Each prints what it did and exits non-zero when nothing matched.
Tracked replacement
python scripts/docx_track.py replace draft.docx -o draft-edited.docx \
--find "was observed" --replace "we observed" --author "Claude"
The old text becomes a tracked deletion and the new text a tracked insertion
that copies the formatting of the first matched run. Use --first for one
occurrence, --ignore-case, or --untracked to rewrite in place when the user
did not ask for a revision trail. Always say which mode you used.
Comments
python scripts/docx_comments.py add draft.docx -o reviewed.docx \
--find "significantly improved" --text "Report the effect size and CI." --author "Reviewer 2"
python scripts/docx_comments.py list reviewed.docx
--find must match text that sits in ordinary runs. If it does not match,
choose a shorter phrase that lies inside one paragraph.
Limits to state to the user
- Matching works on text in plain runs of a paragraph or hyperlink. Text split
by a tab, a field (page number, cross-reference, citation field), a drawing,
or an existing tracked change is not matched. The script says "no matches"
rather than guessing.
- Headers, footers and footnotes are not edited by the tracked-change or
comment scripts. Use python-docx for those parts.
- A deleted paragraph mark that would merge two non-empty paragraphs is left as
an ordinary break and reported.
check finds structural faults (bad zip, malformed XML, missing parts,
dangling relationships, orphaned comment references, repeated revision ids).
It does not validate against the ECMA/ISO schemas. Opening the file in
LibreOffice or Word is the real test; if neither is available, say the file
was checked structurally only.
Habits that prevent damage
- Work on a copy. Keep the original beside the edited file.
- Preserve fields. Citation-manager fields (Zotero, EndNote, Mendeley) and
cross-references live in field codes; a text rewrite that touches the
displayed result can detach them. Prefer tracked edits around them, or use the
docx-editor-zotero skill for Zotero documents.
- Prefer styles over direct formatting: set
Heading 1, Title, Caption
styles rather than bolding and enlarging text, so the table of contents and
navigation work.
- When you generate a document, open it back with python-docx and read the
headings and tables to confirm what you wrote is what is there. Render to PDF
for a visual check when LibreOffice exists (
soffice --headless --convert-to pdf), then view pages as images.
- Do not put secrets or unpublished data in comments or tracked-change author
names; both travel with the file.
Unpack, edit, pack
python scripts/ooxml_tools.py unpack report.docx work/ --pretty
# edit work/word/document.xml (or styles.xml, numbering.xml, ...)
python scripts/ooxml_tools.py pack work/ report-fixed.docx
--pretty indents between elements only and leaves text untouched, so
whitespace-only runs survive. pack puts [Content_Types].xml first, runs the
checker and refuses to write a file that fails it.
Where the parts are
word/document.xml body; word/styles.xml; word/numbering.xml lists;
word/comments.xml; word/_rels/document.xml.rels links to media, headers,
footers; docProps/core.xml author and dates; word/media/ images. Revision
markup in the body: w:ins, w:del, w:moveFrom/w:moveTo, and the
...Change records for formatting.
1---2name: docx3description: Create, read, edit and review Word (.docx) files. Use for drafting a report or manuscript as .docx, converting Markdown or LaTeX to Word, extracting text and tables, find-and-replace that keeps formatting, tracked changes (create, list, accept, reject), and comments. Not for PDFs, spreadsheets or slides, and not for Google Docs.4license: MIT5metadata:6 source: https://skillmd.com/skills/raktim-mondol/docx7 version: "2.0"8 skill-author: Academic Harness9 skill-source: original; drives python-docx, lxml and pandoc, none of which are bundled10---1112# Word documents (.docx)1314A .docx is a zip of XML parts. Most jobs never need to look inside: python-docx15or pandoc does them. The scripts in this skill cover what those cannot do well:16tracked changes, comments, and checking that a hand-edited package is sound.1718Paths below are relative to this skill's directory. Run scripts with the Python19that has the libraries installed.2021## Pick the route2223| You want to | Use |24|---|---|25| Turn Markdown, HTML or LaTeX into Word | `pandoc in.md -o out.docx` (add `--reference-doc=template.docx` to borrow styles) |26| Build a document from data or code | python-docx, see [references/python-docx-recipes.md](references/python-docx-recipes.md) |27| Read text and tables | python-docx, or `pandoc in.docx -t markdown` for a quick look |28| Change wording but keep formatting | `scripts/docx_track.py replace` |29| Propose edits the author can accept or reject | `scripts/docx_track.py replace` (tracked, the default) |30| Accept or reject someone's edits | `scripts/docx_track.py accept` / `reject` |31| Review: add, read or remove comments | `scripts/docx_comments.py` |32| Fix something no library exposes | unpack, edit the XML, `pack` (it checks the result) |33| Check a file Word refuses to open | `scripts/ooxml_tools.py check` |3435## Scripts3637| Script | Does |38|---|---|39| `docx_track.py list` / `accept` / `reject` / `replace` | tracked changes, optionally filtered with `--author` |40| `docx_comments.py list` / `add` / `remove` | comments anchored to an exact phrase |41| `ooxml_tools.py unpack` / `pack` / `check` | extract, rebuild and sanity-check the package |42| `docx_runs.py` | shared run-splitting code the two scripts import |4344All editing scripts write to a new file with `-o`; they never overwrite the45input. Each prints what it did and exits non-zero when nothing matched.4647### Tracked replacement4849```50python scripts/docx_track.py replace draft.docx -o draft-edited.docx \51 --find "was observed" --replace "we observed" --author "Claude"52```5354The old text becomes a tracked deletion and the new text a tracked insertion55that copies the formatting of the first matched run. Use `--first` for one56occurrence, `--ignore-case`, or `--untracked` to rewrite in place when the user57did not ask for a revision trail. Always say which mode you used.5859### Comments6061```62python scripts/docx_comments.py add draft.docx -o reviewed.docx \63 --find "significantly improved" --text "Report the effect size and CI." --author "Reviewer 2"64python scripts/docx_comments.py list reviewed.docx65```6667`--find` must match text that sits in ordinary runs. If it does not match,68choose a shorter phrase that lies inside one paragraph.6970## Limits to state to the user7172- Matching works on text in plain runs of a paragraph or hyperlink. Text split73 by a tab, a field (page number, cross-reference, citation field), a drawing,74 or an existing tracked change is not matched. The script says "no matches"75 rather than guessing.76- Headers, footers and footnotes are not edited by the tracked-change or77 comment scripts. Use python-docx for those parts.78- A deleted paragraph mark that would merge two non-empty paragraphs is left as79 an ordinary break and reported.80- `check` finds structural faults (bad zip, malformed XML, missing parts,81 dangling relationships, orphaned comment references, repeated revision ids).82 It does not validate against the ECMA/ISO schemas. Opening the file in83 LibreOffice or Word is the real test; if neither is available, say the file84 was checked structurally only.8586## Habits that prevent damage8788- Work on a copy. Keep the original beside the edited file.89- Preserve fields. Citation-manager fields (Zotero, EndNote, Mendeley) and90 cross-references live in field codes; a text rewrite that touches the91 displayed result can detach them. Prefer tracked edits around them, or use the92 `docx-editor-zotero` skill for Zotero documents.93- Prefer styles over direct formatting: set `Heading 1`, `Title`, `Caption`94 styles rather than bolding and enlarging text, so the table of contents and95 navigation work.96- When you generate a document, open it back with python-docx and read the97 headings and tables to confirm what you wrote is what is there. Render to PDF98 for a visual check when LibreOffice exists (`soffice --headless --convert-to99 pdf`), then view pages as images.100- Do not put secrets or unpublished data in comments or tracked-change author101 names; both travel with the file.102103## Unpack, edit, pack104105```106python scripts/ooxml_tools.py unpack report.docx work/ --pretty107# edit work/word/document.xml (or styles.xml, numbering.xml, ...)108python scripts/ooxml_tools.py pack work/ report-fixed.docx109```110111`--pretty` indents between elements only and leaves text untouched, so112whitespace-only runs survive. `pack` puts `[Content_Types].xml` first, runs the113checker and refuses to write a file that fails it.114115## Where the parts are116117`word/document.xml` body; `word/styles.xml`; `word/numbering.xml` lists;118`word/comments.xml`; `word/_rels/document.xml.rels` links to media, headers,119footers; `docProps/core.xml` author and dates; `word/media/` images. Revision120markup in the body: `w:ins`, `w:del`, `w:moveFrom`/`w:moveTo`, and the121`...Change` records for formatting.