read_excel_by_sheet_index
Extract data from a specific sheet in Excel (xlsx) files using its index. Defaults to the first sheet if no index is provided, returning structured JSON output for efficient data processing and analysis.
Instructions
Read content from a specific sheet by index in Excel (xlsx) files. Reads first sheet (index 0) if sheet_index not provided.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Path to the Excel file | |
| sheet_index | No | Index of the sheet to read (optional, defaults to 0) |
Implementation Reference
- Handler logic for the 'read_excel_by_sheet_index' tool: retrieves the sheet by index (default 0), validates index, loads sheet data by iterating rows and converting cell values to strings, stores in result dict.elif name == "read_excel_by_sheet_index": # Get sheet by index, default to 0 if not specified sheet_index = arguments.get("sheet_index", 0) if sheet_index < 0 or sheet_index >= len(workbook.sheetnames): raise ValueError(f"Sheet index {sheet_index} is out of range") sheet_name = workbook.sheetnames[sheet_index] sheet = workbook[sheet_name] sheet_data = [] for row in sheet.rows: row_data = [str(cell.value) if cell.value is not None else "" for cell in row] sheet_data.append(row_data) result[sheet_name] = sheet_data
- Input schema for the tool: requires 'file_path' string, optional 'sheet_index' integer >=0.types.Tool( name="read_excel_by_sheet_index", description="Read content from a specific sheet by index in Excel (xlsx) files. Reads first sheet (index 0) if sheet_index not provided.", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Excel file" }, "sheet_index": { "type": "integer", "description": "Index of the sheet to read (optional, defaults to 0)", "minimum": 0 } }, "required": ["file_path"] } )
- src/excel_reader_server/server.py:12-70 (registration)Registration of the tool via the list_tools handler, which returns the Tool object including schema for read_excel_by_sheet_index among others.@server.list_tools() async def handle_list_tools() -> list[types.Tool]: """ List available tools. Each tool specifies its arguments using JSON Schema validation. """ return [ types.Tool( name="read_excel", description="Read content from Excel (xlsx) files", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Excel file" } }, "required": ["file_path"] } ), types.Tool( name="read_excel_by_sheet_name", description="Read content from a specific sheet by name in Excel (xlsx) files. Reads first sheet if sheet_name not provided.", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Excel file" }, "sheet_name": { "type": "string", "description": "Name of the sheet to read (optional, defaults to first sheet)" } }, "required": ["file_path"] } ), types.Tool( name="read_excel_by_sheet_index", description="Read content from a specific sheet by index in Excel (xlsx) files. Reads first sheet (index 0) if sheet_index not provided.", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Excel file" }, "sheet_index": { "type": "integer", "description": "Index of the sheet to read (optional, defaults to 0)", "minimum": 0 } }, "required": ["file_path"] } ) ]