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
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| new_name | Yes | ||
| old_name | Yes |
Implementation Reference
- src/excel_mcp/server.py:411-426 (handler)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
- src/excel_mcp/sheet.py:58-77 (helper)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))
- src/excel_mcp/server.py:411-426 (registration)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