Edit Word Text
word_editEdits Word .docx body text by inserting, rewriting, deleting, finding and replacing, adding breaks, redacting sensitive content, and applying anchored batch changes with optional tracked changes.
Instructions
Change body text: insert, rewrite, delete, find and replace, page and section breaks, redaction, the anchored batch editor (apply_edits) and anchor stamping. Tables and lists are word_structure; styles and formatting are word_format. In any text an op writes, \n is a line break and \t a tab. Ops that take track=true record the change as a tracked change. Leave author out to sign with the user's own name, or pass author="assistant" to sign as "AI Assistant via KS4Word". Every edit to a closed file backs it up first and saves atomically, and a failed validation changes nothing. A document open in Word is edited live and stays unsaved until word_app com_save_document; ops with no live route refuse it. Paragraph numbers count body paragraphs from 0, tables not included, so the first paragraph (usually the title) is paragraph 0; get_text and get_outline show the numbers. An insert at {"paragraph": N} goes after paragraph N (position "before" puts it in front). When a heading or phrase marks the spot, use after_heading, search or anchor instead of a number. Ops, one per line, as name(required; opt optional): what it does. file_path, live and backup are not repeated; word_help shows them:
insert_paragraphs(paragraphs; opt location inherit_format copy_format_from track author): Insert paragraphs (items {text, style?, formatting?, heading_level?, paragraph_format?}) at a location object (omitted = document end). Each item becomes a new paragraph; to add words inside an existing paragraph, use search_and_replace. More ops follow, in the same format, in the op parameter's description.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| op | Yes | The operation to run: an op listed in this tool's description, or one of these, in the same format: - search_and_replace(replacements; opt scope preview find_formatting max_replacements track author): Find and replace a list of {find, replace, regex?} items in one call, safe across Word's split runs; preview=true changes nothing. To add words inside a paragraph, replace the words just before the spot with the same words plus the new ones; with track=true only the new words are marked. Not for removing sensitive text: to remove a name or code everywhere before sharing a document, use redact_text, which also cleans charts, properties and hidden places and confirms the result. If redact_text refuses a replacement, ask the user what to use instead of replacing here. - delete_paragraphs(opt start end range track author expect_start expect_end): Delete body paragraphs by 0-based inclusive index (start, end; end defaults to start) or by range={start, end} location objects. - insert_break(opt type location): Insert a page break or a section break after the located paragraph (omit location for the document end). - apply_edits(edits; opt atomic): Apply a batch of anchor-addressed edits in one call: one lock, backup, and validated save. - delete_element(type; opt id location): Delete one equation, image, chart, bookmark, hyperlink, caption, reference list or content control (type picks which). - stamp_anchors(opt scope detail include): Write durable paragraph anchors into the file so later edits can address paragraphs by anchor; the writing form of get_document_view. - set_paragraph_text(location, new_text; opt expect): Replace one paragraph's full text, keeping style and base formatting; address it with a location object ({paragraph: N}, {search: ...}, {outline: ...}). Pass expect to refuse if the index shifted. - redact_text(targets; opt replacement scope ignore_case whole_word): Permanently remove matched text, then re-read the saved file. targets: [{find, regex?}]. | |
| args | No | The op's arguments as an object. Include file_path (the absolute .docx path) unless the op's line names other paths or the op reads no document; unknown keys are refused. word_help lists every argument. |