copy_worksheet
Duplicate a worksheet within an Excel workbook by specifying source and target sheet names, enabling efficient data organization and management.
Instructions
Copy worksheet within workbook.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| source_sheet | Yes | ||
| target_sheet | Yes |
Implementation Reference
- src/excel_mcp/server.py:378-393 (handler)MCP tool handler for 'copy_worksheet'. Registers the tool via @mcp.tool() decorator and delegates to copy_sheet helper function from sheet.py. Defines input parameters: filepath, source_sheet, target_sheet.@mcp.tool() def copy_worksheet( filepath: str, source_sheet: str, target_sheet: str ) -> str: """Copy worksheet within workbook.""" try: full_path = get_excel_path(filepath) result = copy_sheet(full_path, source_sheet, target_sheet) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error copying worksheet: {e}") raise
- src/excel_mcp/sheet.py:15-36 (helper)Core helper function implementing the worksheet copy logic using openpyxl's Workbook.copy_worksheet() method. Performs validation and saves the workbook.def copy_sheet(filepath: str, source_sheet: str, target_sheet: str) -> Dict[str, Any]: """Copy a worksheet within the same workbook.""" try: wb = load_workbook(filepath) if source_sheet not in wb.sheetnames: raise SheetError(f"Source sheet '{source_sheet}' not found") if target_sheet in wb.sheetnames: raise SheetError(f"Target sheet '{target_sheet}' already exists") source = wb[source_sheet] target = wb.copy_worksheet(source) target.title = target_sheet wb.save(filepath) return {"message": f"Sheet '{source_sheet}' copied to '{target_sheet}'"} except SheetError as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to copy sheet: {e}") raise SheetError(str(e))
- src/excel_mcp/server.py:378-378 (registration)The @mcp.tool() decorator registers the copy_worksheet function as an MCP tool.@mcp.tool()