delete_worksheet
Remove a specified worksheet from an Excel workbook. Input filepath and sheet name to delete the desired sheet, streamlining workbook organization and management.
Instructions
Delete worksheet from workbook.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| sheet_name | Yes |
Implementation Reference
- src/excel_mcp/server.py:395-410 (handler)MCP tool handler function for 'delete_worksheet' that validates input, resolves file path, calls the delete_sheet helper, and returns the result message or error.@mcp.tool() def delete_worksheet( filepath: str, sheet_name: str ) -> str: """Delete worksheet from workbook.""" try: full_path = get_excel_path(filepath) result = delete_sheet(full_path, sheet_name) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error deleting worksheet: {e}") raise
- src/excel_mcp/sheet.py:38-56 (helper)Core helper function implementing worksheet deletion using openpyxl: loads workbook, validates sheet existence and that it's not the last sheet, deletes the sheet, saves the file, and returns a success message.def delete_sheet(filepath: str, sheet_name: str) -> Dict[str, Any]: """Delete a worksheet from the workbook.""" try: wb = load_workbook(filepath) if sheet_name not in wb.sheetnames: raise SheetError(f"Sheet '{sheet_name}' not found") if len(wb.sheetnames) == 1: raise SheetError("Cannot delete the only sheet in workbook") del wb[sheet_name] wb.save(filepath) return {"message": f"Sheet '{sheet_name}' deleted"} except SheetError as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to delete sheet: {e}") raise SheetError(str(e))
- src/excel_mcp/server.py:395-410 (registration)The @mcp.tool() decorator registers the delete_worksheet function as an MCP tool.@mcp.tool() def delete_worksheet( filepath: str, sheet_name: str ) -> str: """Delete worksheet from workbook.""" try: full_path = get_excel_path(filepath) result = delete_sheet(full_path, sheet_name) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error deleting worksheet: {e}") raise