write_excel
Convert and save CSV or JSON data into an Excel file, specifying the file path, sheet name, and format. Ideal for organizing and exporting structured data into spreadsheets.
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
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | ||
| file_path | Yes | ||
| format | No | csv | |
| sheet_name | No | Sheet1 |
Implementation Reference
- mcp_excel_server/server.py:212-251 (handler)The handler function for the 'write_excel' tool. Decorated with @mcp.tool() for registration in the FastMCP server. Parses input data as CSV or JSON into a Pandas DataFrame and writes it to the specified file in Excel (.xlsx, .xls, .xlsm), CSV, TSV, or JSON format.@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)}"
- mcp_excel_server/server.py:212-212 (registration)The @mcp.tool() decorator registers the write_excel function as an MCP tool in the FastMCP server instance.@mcp.tool()