[BUG] docx: `set text` on a paragraph silently deletes every drawing it contains (floating textboxes, floating pictures, inline pictures)
Summary
Setting the text of a paragraph deletes all non-text content of that paragraph: floating shapes (mc:AlternateContent with wps:txbx / v:textbox), floating pictures (wp:anchor), and inline pictures (wp:inline). The command reports success, emits no warning, and validate passes afterwards, so the data loss is completely silent.
This is dangerous for real-world documents: Word anchors floating shapes in whatever body paragraph it chooses, so a text edit that looks unrelated to any graphic can destroy it. Inline pictures are even easier to hit, since they live in the very paragraph the user is editing.
Scope of the damage is limited to the targeted paragraph — drawings in other paragraphs survive, and tables (w:tbl, a sibling of w:p rather than a child) are unaffected.
Environment
- OfficeCLI:
1.0.144 - OS: Windows 11 (x64)
- Source DOCX producer (
docProps/app.xml):Microsoft Office Word(Word for Microsoft 365)
Reproduction
- In Word, create a document with three paragraphs and one floating textbox (Insert → Text Box), save as
repro.docx. The textbox anchor lands in paragraph 1. - Edit paragraph 1's text:
officecli set repro.docx "/body/p[1]" --prop text="First paragraph. EDITED" --json
officecli close repro.docx
- Inspect the result:
| before | after | |
|---|---|---|
<w:drawing> in document.xml |
1 | 0 |
<w:pict> (fallback) |
1 | 0 |
<w:txbxContent> |
2 | 0 |
| textbox text present | yes | no |
Word confirms: Document.Shapes.Count = 1 before, 0 after.
Inline pictures and tables
A second document was built in Word containing an inline picture and a floating picture (both anchored in paragraph 1) plus a 2x2 table. Editing paragraph 1:
before: <w:drawing>=2 <wp:inline>=1 <wp:anchor>=1 <w:tbl>=1
after: <w:drawing>=0 <wp:inline>=0 <wp:anchor>=0 <w:tbl>=1
Editing a paragraph that holds no drawing (paragraph 2) changed nothing, and set text on a table cell was clean. So the deletion is scoped to the targeted paragraph, but within it every drawing is dropped regardless of kind.
Note on prior art
Replacing paragraph content on a text assignment is a defensible design choice — python-docx's Paragraph.text setter behaves the same way. The difference is the contract around it:
- python-docx states it in the property docstring: "Assigning text to this property causes all existing paragraph content to be replaced with a single run containing the assigned text." A developer reads that before calling it.
- OfficeCLI's
help docx set paragraphdescribestextas "Sets plain text on the paragraph by creating an implicit single run" and says nothing about existing content being removed. SKILL.md — the file that teaches agents the command surface — carries no warning either.
That asymmetry matters more here than it does for a library, because the caller is an agent that cannot see the document and has no way to notice that a figure disappeared.
Expected
Any one of these would resolve it; they are listed weakest-to-strongest, and even the first would be enough:
- Document the destructive scope on the
textproperty and in SKILL.md. - Report it — include the dropped drawings in the command's JSON result instead of a bare
"Updated ...". - Refuse by default when the target paragraph contains a drawing, with an explicit
--forceto proceed. - Preserve non-text runs (drawings, picts, field chars) and replace only the text runs.
Note that a non-destructive path already exists: set /body/p[N]/r[M] --prop text=... on a text run leaves every drawing in the paragraph intact (verified). So the safe behavior is reachable today — it is just not the one the documented happy path takes.
Actual
The command reports "Updated /body/p[1]: text=..." and the shape is gone.
Fixed on main (next release) — thanks for the thorough writeup and the strongest-fix suggestion. Paragraph-level set --prop text= now replaces only text-bearing runs and preserves any run carrying a drawing / VML picture / mc:AlternateContent shape / embedded object / field. Verified against a floating textbox and inline/floating pictures: the graphics survive, the text edit still applies, validate passes, and a real-Word render confirms the picture is intact. Text-only paragraphs behave exactly as before.
Verified fixed in v1.0.145 (commit 07127ca) with my original repro — thanks!
Both test documents now come through clean: the floating textbox (mc:Choice/mc:Fallback both), the inline picture, the floating picture and the table all survive a paragraph-level set text, and validate passes. One observation for anyone relying on this: the paragraph's runs are collapsed into a single run that inherits the first run's formatting, so mixed-formatting paragraphs are still better edited run-by-run. Closing.