Skip to main content
Glama

filter_excel

Filter Excel spreadsheet data using pandas query strings to extract specific rows based on conditions like Age > 30 and Department == 'Sales'.

Instructions

Filter Excel data using a pandas query string.

Args:
    file_path: Path to the Excel file
    query: Pandas query string (e.g., "Age > 30 and Department == 'Sales'")
    sheet_name: Name of the sheet to filter (for Excel files)
    
Returns:
    Filtered data as string

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
queryYes
sheet_nameNo

Implementation Reference

  • The main handler function for the 'filter_excel' MCP tool. It reads data from an Excel-compatible file, applies a pandas query filter based on the provided query string, and returns the filtered DataFrame as a formatted string. The @mcp.tool() decorator registers it as an MCP tool.
    @mcp.tool()
    def filter_excel(file_path: str, query: str, sheet_name: Optional[str] = None) -> str:
        """
        Filter Excel data using a pandas query string.
        
        Args:
            file_path: Path to the Excel file
            query: Pandas query string (e.g., "Age > 30 and Department == 'Sales'")
            sheet_name: Name of the sheet to filter (for Excel files)
            
        Returns:
            Filtered data as string
        """
        try:
            # Read file
            _, ext = os.path.splitext(file_path)
            ext = ext.lower()
            
            read_params = {}
            if ext in ['.xlsx', '.xls', '.xlsm'] and sheet_name is not None:
                read_params["sheet_name"] = sheet_name
                
            if ext in ['.xlsx', '.xls', '.xlsm']:
                df = pd.read_excel(file_path, **read_params)
            elif ext == '.csv':
                df = pd.read_csv(file_path)
            elif ext == '.tsv':
                df = pd.read_csv(file_path, sep='\t')
            elif ext == '.json':
                df = pd.read_json(file_path)
            else:
                return f"Unsupported file extension: {ext}"
            
            # Apply filter
            filtered_df = df.query(query)
            
            # Return results
            if filtered_df.empty:
                return "No data matches the filter criteria."
            
            return filtered_df.to_string(index=False)
        except Exception as e:
            return f"Error filtering data: {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/yzfly/mcp-excel-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server