export_chart
Generate and export charts as images from Excel data by specifying file path, x-axis, y-axis columns, and chart type. Simplify data visualization for analysis and reporting.
Instructions
Create a chart from Excel data and return as an image.
Args:
file_path: Path to the Excel file
x_column: Column to use for x-axis
y_column: Column to use for y-axis
chart_type: Type of chart ('line', 'bar', 'scatter', 'hist')
sheet_name: Name of the sheet to chart (for Excel files)
Returns:
Chart as image
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chart_type | No | line | |
| file_path | Yes | ||
| sheet_name | No | ||
| x_column | Yes | ||
| y_column | Yes |
Implementation Reference
- mcp_excel_server/server.py:479-555 (handler)The main handler function for the 'export_chart' tool. Decorated with @mcp.tool(), it reads data from various file formats (Excel, CSV, TSV, JSON), creates charts using matplotlib/seaborn (line, bar, scatter, hist), saves as PNG image bytes, and returns as Image object. Handles errors by returning an error image.def export_chart(file_path: str, x_column: str, y_column: str, chart_type: str = "line", sheet_name: Optional[str] = None) -> Image: """ Create a chart from Excel data and return as an image. Args: file_path: Path to the Excel file x_column: Column to use for x-axis y_column: Column to use for y-axis chart_type: Type of chart ('line', 'bar', 'scatter', 'hist') sheet_name: Name of the sheet to chart (for Excel files) Returns: Chart as image """ import matplotlib.pyplot as plt import seaborn as sns 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: raise ValueError(f"Unsupported file extension: {ext}") # Create chart plt.figure(figsize=(10, 6)) if chart_type == "line": sns.lineplot(data=df, x=x_column, y=y_column) elif chart_type == "bar": sns.barplot(data=df, x=x_column, y=y_column) elif chart_type == "scatter": sns.scatterplot(data=df, x=x_column, y=y_column) elif chart_type == "hist": df[y_column].hist() plt.xlabel(y_column) else: raise ValueError(f"Unsupported chart type: {chart_type}") plt.title(f"{chart_type.capitalize()} Chart: {y_column} by {x_column}") plt.tight_layout() # Save to bytes buffer buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) # Convert to Image plt.close() return Image(data=buf.getvalue(), format="png") except Exception as e: # Return error image plt.figure(figsize=(8, 2)) plt.text(0.5, 0.5, f"Error creating chart: {str(e)}", horizontalalignment='center', fontsize=12, color='red') plt.axis('off') buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) plt.close() return Image(data=buf.getvalue(), format="png")