Skip to main content
Glama

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
NameRequiredDescriptionDefault
filepathYes
sheet_nameYes

Implementation Reference

  • 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
  • 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))
  • 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

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/haris-musa/excel-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server