get_workbook_metadata
Extract metadata from Excel workbooks, including sheet details and optional range information, using this tool to analyze and manage workbook structures efficiently.
Instructions
Get metadata about workbook including sheets, ranges, etc.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| include_ranges | No |
Implementation Reference
- src/excel_mcp/server.py:428-442 (handler)MCP tool handler function for get_workbook_metadata. Decorated with @mcp.tool() for registration and execution. Wraps get_workbook_info helper.@mcp.tool() def get_workbook_metadata( filepath: str, include_ranges: bool = False ) -> str: """Get metadata about workbook including sheets, ranges, etc.""" try: full_path = get_excel_path(filepath) result = get_workbook_info(full_path, include_ranges=include_ranges) return str(result) except WorkbookError as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error getting workbook metadata: {e}") raise
- src/excel_mcp/workbook.py:63-96 (helper)Core helper function that loads the workbook using openpyxl and extracts metadata: filename, sheets list, file size, modification time, and optional used ranges per sheet.def get_workbook_info(filepath: str, include_ranges: bool = False) -> dict[str, Any]: """Get metadata about workbook including sheets, ranges, etc.""" try: path = Path(filepath) if not path.exists(): raise WorkbookError(f"File not found: {filepath}") wb = load_workbook(filepath, read_only=False) info = { "filename": path.name, "sheets": wb.sheetnames, "size": path.stat().st_size, "modified": path.stat().st_mtime } if include_ranges: # Add used ranges for each sheet ranges = {} for sheet_name in wb.sheetnames: ws = wb[sheet_name] if ws.max_row > 0 and ws.max_column > 0: ranges[sheet_name] = f"A1:{get_column_letter(ws.max_column)}{ws.max_row}" info["used_ranges"] = ranges wb.close() return info except WorkbookError as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to get workbook info: {e}") raise WorkbookError(str(e))