Deeper guidance for write_docx/read_docx -- on top of what's already in
your base instructions (the markdown subset, and the "no partial editing,
rewrite the whole content and overwrite=True" rule).
A new document
Without template_path, write_docx lays the document out itself: fonts
for the document's language, heading styles, A4, page numbers, and tables
with a shaded header row; a last row whose first cell is "Total" or "合计" is
set bold. Pass language as the language the person wants the document in
(usually the one they write to you in). Spend the effort on content and
structure instead:
- Decide who reads it and what they must see first. The opening says what the document is, for whom, and its point in two lines.
#once for the title,##for sections; put figures a reader compares in a table, not in sentences.- Every figure, date and name exactly as in the sources. Add nothing they do not contain: no attendees, roles or budget lines the notes don't give.
Page breaks and finishing
A line that is exactly [PAGEBREAK] starts the next block on a new page (a cover page, a template
that is to stand alone on its page). Layout the subset cannot say (a font size, an indent, a column)
is not worth a script: write the document once, look at the rendered preview_path once, fix what the
tools can fix, and tell the person what could not be done. Every Word write (accepting or rejecting
tracked changes and review edits too) returns page_check: what the office program's layout showed (an empty page, a table past the paper's edge, a last page of a line
or two): fix those and write again; when it says "not checked", say so rather than call the layout
fine. Do not convert the file to PDF and read
it page by page again and again, and do not patch the .docx with a script to polish it.
Changing an existing document's structure
To add a section, a list, a table or a contents list to a user's document,
move a section or delete one, use edit_docx_structure -- one call with a
list of operations -- not a script and not write_docx (which would rebuild
the file and lose what the markdown subset can't hold). For example, a
contents list after the introduction and a section moved to the front:
[{"op": "insert", "after": "<first words of the intro>", "content": "# Contents\n\n[TOC]"},
{"op": "move_section", "section": "Safety", "before": "Onboarding"}]
after_section places content after everything under a heading. Pass
output_path when the user wants a new file. Everything the operations
don't name is kept as it is, and the result's document lists the new
headings, tables, pictures, header and footer: check the edit there, not
with a script. Before editing, read_docx(path, view="structure") shows each
block's style, the tables with their styles, the pictures, and the header
and footer -- what the markdown view leaves out -- without a script. Use replace_docx_text and
friends for wording inside paragraphs.
Table of contents
A line that is exactly [TOC] inserts a real Word TOC field, built from
the document's own heading levels (#/##/###). It only works if the
document actually uses headings for its sections -- a document written as
plain paragraphs with bold text standing in for headings produces an empty
or missing TOC, since Word has no way to tell that text is structural. For
any document long enough to want a TOC, use real #/##/### lines for
every section and subsection, not just for the ones that happen to need
one. The list shows the headings without page numbers: only Word can
paginate, and the person adds them with right-click, Update Field. Don't
describe it as broken, and don't write page numbers or page breaks into it
with a script. A heading written just above [TOC] reading "Contents" is
the list's title and stays out of it.
A cover page and a contents list suit a document of about six pages or more. A shorter one gets neither unless the person asks, since each takes a nearly empty page. Headline figures go in a short table or a list, not in large type in a table of many columns: the numbers break across lines.
Comments vs. tracked changes -- pick the one that matches the ask
These are two different things and answer different requests:
{{comment: your comment text}}at the end of a heading/paragraph/ bullet/numbered-item line attaches a real Word comment anchored to that line, visible in Word's Comments pane. Use this for feedback on content that isn't changing -- a note, a question, a flag for the reader. Not recognized inside table cells.track_changes=Truemarks everything this call writes as a tracked insertion instead of final content, and (withtemplate_path) marks the template's old body as a tracked deletion instead of removing it outright. Use this when the document itself should change and the change is what needs review -- a redline the user can accept or reject in Word.
Don't reach for track_changes=True when what's actually wanted is a
comment on unchanged text, or vice versa -- they produce visibly different
documents and Word review workflows expect the right one.
To review a document that already has changes or comments -- accept or
reject a change, answer or resolve a comment, comment on some words -- use
read_docx_review for the ids, then edit_docx_review, rather than a script.
Templates
template_path carries over an existing document's styles and page setup
(useful for matching a user's letterhead or house style) but its own body
content is removed (or tracked-deleted, with track_changes=True) before
your new content is added -- it is a style source, not something your
new content gets appended to. Tables in a template are always removed
outright, tracked changes or not, since table-level tracked deletions
aren't supported.
Charts and pictures
write_docx has no syntax for them. Write the document first, then call
add_docx_chart(path, chart_type, data, title=..., after_text="<heading>")
for a bar/line/pie chart from a pipe-table -- it is a native Word chart, so
the reader can edit its data, and it needs no plotting library. Use
add_docx_image for an existing PNG/JPEG. Both put the item after the
paragraph named by after_text (or at the end) and leave the rest of the
file alone. Writing the document again with write_docx discards them, so
make text changes with replace_docx_text/insert_docx_text afterwards.
Give a caption when the text refers to the figure.
Before finishing
For a document with real structure (a TOC, multiple sections, a template
applied, tracked changes), look at the rendered preview_path or consider
asking review_work to check it -- whether that extra step is worth it is
your judgment call for the specific document, not a fixed rule. A one-
paragraph memo doesn't need it; a multi-section report going to someone
else usually does.