delete_table
Remove a specific table from a Microsoft Word document by specifying the filename and table index, ensuring precise document editing and management.
Instructions
Delete a table from a document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| table_index | Yes |
Implementation Reference
- The core handler function that executes the delete_table tool: validates inputs, loads the Word document using python-docx, removes the specified table by index from the document body, saves the modified document, and returns a status message.async def delete_table(filename: str, table_index: int) -> str: """Delete a table from a document. Args: filename: Path to the Word document table_index: Index of the table 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 table index if table_index < 0 or table_index >= len(doc.tables): return f"Invalid table index. Document has {len(doc.tables)} tables (0-{len(doc.tables)-1})." # Delete the table by removing its element from the document table = doc.tables[table_index] table._tbl.getparent().remove(table._tbl) doc.save(filename) return f"Table at index {table_index} deleted successfully." except Exception as e: return f"Failed to delete table: {str(e)}"
- word_document_server/main.py:732-736 (registration)The registration of the 'delete_table' tool using FastMCP's @mcp.tool() decorator in the main server file. This wrapper function defines the tool interface and delegates execution to the implementation in content_tools.delete_table.@mcp.tool() async def delete_table(filename: str, table_index: int): """Delete a table from a document.""" return await content_tools.delete_table(filename, table_index)