Skip to main content
Glama

rename_worksheet

Rename a specific worksheet within an Excel workbook by specifying the file path, old worksheet name, and new worksheet name.

Instructions

Rename worksheet in workbook.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes
new_nameYes
old_nameYes

Implementation Reference

  • MCP tool handler for 'rename_worksheet' that wraps the core rename_sheet function, handles errors, and returns success/error messages.
    @mcp.tool()
    def rename_worksheet(
        filepath: str,
        old_name: str,
        new_name: str
    ) -> str:
        """Rename worksheet in workbook."""
        try:
            full_path = get_excel_path(filepath)
            result = rename_sheet(full_path, old_name, new_name)
            return result["message"]
        except (ValidationError, SheetError) as e:
            return f"Error: {str(e)}"
        except Exception as e:
            logger.error(f"Error renaming worksheet: {e}")
            raise
  • Core helper function that performs the actual worksheet renaming using openpyxl: loads workbook, validates sheet names, updates title, and saves.
    def rename_sheet(filepath: str, old_name: str, new_name: str) -> Dict[str, Any]:
        """Rename a worksheet."""
        try:
            wb = load_workbook(filepath)
            if old_name not in wb.sheetnames:
                raise SheetError(f"Sheet '{old_name}' not found")
                
            if new_name in wb.sheetnames:
                raise SheetError(f"Sheet '{new_name}' already exists")
                
            sheet = wb[old_name]
            sheet.title = new_name
            wb.save(filepath)
            return {"message": f"Sheet renamed from '{old_name}' to '{new_name}'"}
        except SheetError as e:
            logger.error(str(e))
            raise
        except Exception as e:
            logger.error(f"Failed to rename sheet: {e}")
            raise SheetError(str(e))
  • The @mcp.tool() decorator registers the 'rename_worksheet' function as an MCP tool.
    @mcp.tool()
    def rename_worksheet(
        filepath: str,
        old_name: str,
        new_name: str
    ) -> str:
        """Rename worksheet in workbook."""
        try:
            full_path = get_excel_path(filepath)
            result = rename_sheet(full_path, old_name, new_name)
            return result["message"]
        except (ValidationError, SheetError) as e:
            return f"Error: {str(e)}"
        except Exception as e:
            logger.error(f"Error renaming 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