Skip to main content
Glama

write_excel

Export CSV or JSON data to Excel files with customizable sheet names for structured data storage and analysis.

Instructions

Write data to an Excel file.

Args:
    file_path: Path to save the Excel file
    data: Data in CSV or JSON format
    sheet_name: Name of the sheet (for Excel files)
    format: Format of the input data ('csv' or 'json')
    
Returns:
    Confirmation message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes
dataYes
sheet_nameNoSheet1
formatNocsv

Implementation Reference

  • The main handler function for the 'write_excel' tool. Decorated with @mcp.tool(), it reads input data as CSV or JSON into a pandas DataFrame and writes it to the specified file_path in Excel, CSV, TSV, or JSON format based on the extension. Returns success or error message.
    @mcp.tool()
    def write_excel(file_path: str, data: str, sheet_name: Optional[str] = "Sheet1", 
                  format: Optional[str] = "csv") -> str:
        """
        Write data to an Excel file.
        
        Args:
            file_path: Path to save the Excel file
            data: Data in CSV or JSON format
            sheet_name: Name of the sheet (for Excel files)
            format: Format of the input data ('csv' or 'json')
            
        Returns:
            Confirmation message
        """
        try:
            if format.lower() == 'csv':
                df = pd.read_csv(io.StringIO(data))
            elif format.lower() == 'json':
                df = pd.read_json(io.StringIO(data))
            else:
                return f"Unsupported data format: {format}"
            
            _, ext = os.path.splitext(file_path)
            ext = ext.lower()
            
            if ext in ['.xlsx', '.xls', '.xlsm']:
                df.to_excel(file_path, sheet_name=sheet_name, index=False)
            elif ext == '.csv':
                df.to_csv(file_path, index=False)
            elif ext == '.tsv':
                df.to_csv(file_path, sep='\t', index=False)
            elif ext == '.json':
                df.to_json(file_path, orient='records')
            else:
                return f"Unsupported output file extension: {ext}"
            
            return f"Successfully wrote data to {file_path}"
        except Exception as e:
            return f"Error writing data: {str(e)}"
  • The @mcp.tool() decorator registers the write_excel function as an MCP tool.
    @mcp.tool()

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