delete_paragraph
Remove specific paragraphs from Microsoft Word documents by specifying the filename and paragraph index, streamlining editing through standardized tools.
Instructions
Delete a paragraph from a document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| paragraph_index | Yes |
Implementation Reference
- Core handler function that loads the Word document, validates the paragraph index, removes the paragraph's underlying XML element using python-docx workaround, saves the document, and returns success/error message.async def delete_paragraph(filename: str, paragraph_index: int) -> str: """Delete a paragraph from a document. Args: filename: Path to the Word document paragraph_index: Index of the paragraph to delete (0-based) """ filename = ensure_docx_extension(filename) if not os.path.exists(filename): return f"Document {filename} does not exist" # Check if file is writeable is_writeable, error_message = check_file_writeable(filename) if not is_writeable: return f"Cannot modify document: {error_message}. Consider creating a copy first." try: doc = Document(filename) # Validate paragraph index if paragraph_index < 0 or paragraph_index >= len(doc.paragraphs): return f"Invalid paragraph index. Document has {len(doc.paragraphs)} paragraphs (0-{len(doc.paragraphs)-1})." # Delete the paragraph (by removing its content and setting it empty) # Note: python-docx doesn't support true paragraph deletion, this is a workaround paragraph = doc.paragraphs[paragraph_index] p = paragraph._p p.getparent().remove(p) doc.save(filename) return f"Paragraph at index {paragraph_index} deleted successfully." except Exception as e: return f"Failed to delete paragraph: {str(e)}"
- word_document_server/main.py:197-200 (registration)MCP tool registration using @mcp.tool() decorator. This sync wrapper delegates to the async handler in content_tools.py.@mcp.tool() def delete_paragraph(filename: str, paragraph_index: int): """Delete a paragraph from a document.""" return content_tools.delete_paragraph(filename, paragraph_index)