delete_paragraph
Remove a specific paragraph from a Microsoft Word document by specifying the filename and paragraph index. Streamline document editing and content management with this targeted tool.
Instructions
Delete a paragraph from a document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| paragraph_index | Yes |
Implementation Reference
- The primary handler function for the 'delete_paragraph' tool. It loads the Word document, validates the paragraph index, removes the paragraph's underlying XML element using python-docx workaround, saves the document, and returns a status 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/tools/__init__.py:16-20 (registration)Registration of content tools including 'delete_paragraph' by importing it from content_tools.py into the tools package __init__.py, making it available for MCP server registration.from word_document_server.tools.content_tools import ( add_heading, add_paragraph, add_table, add_picture, add_page_break, add_table_of_contents, delete_paragraph, search_and_replace )