Skip to main content
Glama

create_table

Transform a specified data range into a native Excel table, enabling structured data management, formatting, and advanced analysis within Excel workbooks.

Instructions

Creates a native Excel table from a specified range of data.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
data_rangeYes
filepathYes
sheet_nameYes
table_nameNo
table_styleNoTableStyleMedium9

Implementation Reference

  • MCP tool handler and registration for 'create_table'. Resolves file path, calls the implementation, and handles errors.
    @mcp.tool()
    def create_table(
        filepath: str,
        sheet_name: str,
        data_range: str,
        table_name: Optional[str] = None,
        table_style: str = "TableStyleMedium9"
    ) -> str:
        """Creates a native Excel table from a specified range of data."""
        try:
            full_path = get_excel_path(filepath)
            result = create_table_impl(
                filepath=full_path,
                sheet_name=sheet_name,
                data_range=data_range,
                table_name=table_name,
                table_style=table_style
            )
            return result["message"]
        except DataError as e:
            return f"Error: {str(e)}"
        except Exception as e:
            logger.error(f"Error creating table: {e}")
            raise
  • Core helper function that implements the Excel table creation using openpyxl.Table, including style application and validation.
    def create_excel_table(
        filepath: str,
        sheet_name: str,
        data_range: str,
        table_name: str | None = None,
        table_style: str = "TableStyleMedium9"
    ) -> dict:
        """Creates a native Excel table for the given data range.
        
        Args:
            filepath: Path to the Excel file.
            sheet_name: Name of the worksheet.
            data_range: The cell range for the table (e.g., "A1:D5").
            table_name: A unique name for the table. If not provided, a unique name is generated.
            table_style: The visual style to apply to the table.
            
        Returns:
            A dictionary with a success message and table details.
        """
        try:
            wb = load_workbook(filepath)
            if sheet_name not in wb.sheetnames:
                raise DataError(f"Sheet '{sheet_name}' not found.")
                
            ws = wb[sheet_name]
    
            # If no table name is provided, generate a unique one
            if not table_name:
                table_name = f"Table_{uuid.uuid4().hex[:8]}"
    
            # Check if table name already exists
            if table_name in ws.parent.defined_names:
                raise DataError(f"Table name '{table_name}' already exists.")
    
            # Create the table
            table = Table(displayName=table_name, ref=data_range)
            
            # Apply style
            style = TableStyleInfo(
                name=table_style, 
                showFirstColumn=False,
                showLastColumn=False, 
                showRowStripes=True, 
                showColumnStripes=False
            )
            table.tableStyleInfo = style
            
            ws.add_table(table)
            
            wb.save(filepath)
            
            return {
                "message": f"Successfully created table '{table_name}' in sheet '{sheet_name}'.",
                "table_name": table_name,
                "range": data_range
            }
    
        except Exception as e:
            logger.error(f"Failed to create table: {e}")
            raise DataError(str(e)) 

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