add-slide-title-with-chart
Add a slide with a title and automatically generated chart to PowerPoint presentations. The tool analyzes data structure to select appropriate chart types for clear data visualization.
Instructions
Add a new slide with a title and chart. The chart type will be automatically selected based on the data structure.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| presentation_name | Yes | Name of the presentation to add the slide to | |
| title | Yes | Title of the slide | |
| data | Yes | Chart data structure |
Implementation Reference
- src/powerpoint/server.py:589-627 (handler)Main handler logic for the 'add-slide-title-with-chart' tool. Creates a title slide, determines chart type, adds the chart, and returns success message.elif name == "add-slide-title-with-chart": presentation_name = arguments.get("presentation_name") title = arguments.get("title") chart_data = arguments.get("data") if not all([presentation_name, title, chart_data]): raise ValueError("Missing required arguments") if presentation_name not in presentation_manager.presentations: raise ValueError(f"Presentation not found: {presentation_name}") # Get the presentation and create a new slide prs = presentation_manager.presentations[presentation_name] slide_layout = prs.slide_layouts[5] # Title and blank content slide = prs.slides.add_slide(slide_layout) # Set the title title_shape = slide.shapes.title title_shape.text = title # Determine the best chart type for the data try: chart_type, chart_format = chart_manager.determine_chart_type(chart_data) except Exception as e: raise ValueError(f"Unable to determine chart type.") # Add the chart to the slide try: chart = chart_manager.add_chart_to_slide(slide, chart_type, chart_data, chart_format) chart_type_name = chart_type.name.lower().replace('xl_chart_type.', '') return [ types.TextContent( type="text", text=f"Added slide '{title}' with a {chart_type_name} chart to presentation: {presentation_name}" ) ] except Exception as e: raise ValueError(f"Failed to create slide with chart: {str(e)}")
- src/powerpoint/server.py:232-297 (registration)Tool registration in list_tools() including name, description, and input schema definition.types.Tool( name="add-slide-title-with-chart", description="Add a new slide with a title and chart. The chart type will be automatically selected based on the data structure.", inputSchema={ "type": "object", "properties": { "presentation_name": { "type": "string", "description": "Name of the presentation to add the slide to", }, "title": { "type": "string", "description": "Title of the slide", }, "data": { "type": "object", "description": "Chart data structure", "properties": { "categories": { "type": "array", "items": {"type": ["string", "number"]}, "description": "X-axis categories or labels (optional)" }, "series": { "type": "array", "items": { "type": "object", "properties": { "name": { "type": "string", "description": "Name of the data series" }, "values": { "type": "array", "items": { "oneOf": [ {"type": "number"}, { "type": "array", "items": {"type": "number"}, "minItems": 2, "maxItems": 2 } ] }, "description": "Values for the series. Can be simple numbers or [x,y] pairs for scatter plots" } }, "required": ["name", "values"] } }, "x_axis": { "type": "string", "description": "X-axis title (optional)" }, "y_axis": { "type": "string", "description": "Y-axis title (optional)" } }, "required": ["series"] } }, "required": ["presentation_name", "title", "data"], }, ),
- Helper method to automatically determine the best chart type (e.g., scatter, pie, line, bar, column) based on the input data structure.def determine_chart_type(self, data: Dict[str, Any]) -> tuple[XL_CHART_TYPE, str]: """ Analyze the data structure and determine the most appropriate chart type. Returns tuple of (PowerPoint chart type enum, chart_format) """ # evaluate the data series_count = len(data["series"]) categories = data.get("categories", []) # Check for XY data more safely by checking the first value of each series is_xy_data = False for series in data["series"]: values = series.get("values", []) if values: first_value = values[0] is_xy_data = isinstance(first_value, (list, tuple)) and len(first_value) == 2 break if is_xy_data: return XL_CHART_TYPE.XY_SCATTER, "xy" # If we have percentage data that adds up to ~100, suggest pie chart if series_count == 1 and categories: values = data["series"][0].get("values", []) if len(values) <= 8: try: total = sum(float(v) for v in values) if 95 <= total <= 105: return XL_CHART_TYPE.PIE, "category" except (TypeError, ValueError): pass # For time series or trending data, suggest line chart if categories and any( isinstance(cat, (str, int)) and any(term in str(cat).lower() for term in ["date", "time", "year", "month", "quarter", "q1", "q2", "q3", "q4"]) for cat in categories ): return XL_CHART_TYPE.LINE, "category" # For multiple series comparing values, suggest bar chart if series_count > 1 and categories: return XL_CHART_TYPE.BAR_CLUSTERED, "category" # Default to column chart for single series return XL_CHART_TYPE.COLUMN_CLUSTERED, "category"
- Helper method that constructs and adds the chart to the slide, handling category and XY data formats, positioning, legend, and axis titles.def add_chart_to_slide(self, slide, chart_type: XL_CHART_TYPE, data: Dict[str, Any], chart_format: str = "category") -> chart: """Add a chart to the slide with the specified data.""" # Position chart in the middle of the slide with margins left = Inches(1) top = Inches(2) width = Inches(8) height = Inches(5) if chart_format == "category": chart_data = CategoryChartData() chart_data.categories = data.get("categories", []) # Add each series for series in data["series"]: chart_data.add_series(series["name"], series["values"]) elif chart_format == "xy": chart_data = XyChartData() # Add each series for series in data["series"]: series_data = chart_data.add_series(series["name"]) for x, y in series["values"]: series_data.add_data_point(x, y) # Add and configure the chart graphic_frame = slide.shapes.add_chart( chart_type, left, top, width, height, chart_data ) chart = graphic_frame.chart # Basic formatting chart.has_legend = True if len(data["series"]) > 1: chart.legend.position = XL_LEGEND_POSITION.BOTTOM # Add axis titles if provided if "x_axis" in data: chart.category_axis.axis_title.text_frame.text = data["x_axis"] if "y_axis" in data: chart.value_axis.axis_title.text_frame.text = data["y_axis"] return chart