mcpcn-office-powerpoint-mcp-server 2.1.1__py3-none-any.whl → 2.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tools/structural_tools.py CHANGED
@@ -1,373 +1,373 @@
1
- """
2
- Structural element tools for PowerPoint MCP Server.
3
- Handles tables, shapes, and charts.
4
- """
5
- from typing import Dict, List, Optional, Any
6
- from mcp.server.fastmcp import FastMCP
7
- import utils as ppt_utils
8
-
9
-
10
- def register_structural_tools(app: FastMCP, presentations: Dict, get_current_presentation_id, validate_parameters, is_positive, is_non_negative, is_in_range, is_valid_rgb, add_shape_direct):
11
- """Register structural element tools with the FastMCP app"""
12
-
13
- @app.tool()
14
- def add_table(
15
- slide_index: int,
16
- rows: int,
17
- cols: int,
18
- left: float,
19
- top: float,
20
- width: float,
21
- height: float,
22
- data: Optional[List[List[str]]] = None,
23
- header_row: bool = True,
24
- header_font_size: int = 12,
25
- body_font_size: int = 10,
26
- header_bg_color: Optional[List[int]] = None,
27
- body_bg_color: Optional[List[int]] = None,
28
- border_color: Optional[List[int]] = None,
29
- presentation_id: Optional[str] = None
30
- ) -> Dict:
31
- """Add a table to a slide with enhanced formatting options."""
32
- pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
33
-
34
- if pres_id is None or pres_id not in presentations:
35
- return {
36
- "error": "No presentation is currently loaded or the specified ID is invalid"
37
- }
38
-
39
- pres = presentations[pres_id]
40
-
41
- if slide_index < 0 or slide_index >= len(pres.slides):
42
- return {
43
- "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
44
- }
45
-
46
- slide = pres.slides[slide_index]
47
-
48
- # Validate parameters
49
- validations = {
50
- "rows": (rows, [(is_positive, "must be a positive integer")]),
51
- "cols": (cols, [(is_positive, "must be a positive integer")]),
52
- "left": (left, [(is_non_negative, "must be non-negative")]),
53
- "top": (top, [(is_non_negative, "must be non-negative")]),
54
- "width": (width, [(is_positive, "must be positive")]),
55
- "height": (height, [(is_positive, "must be positive")])
56
- }
57
-
58
- if header_bg_color is not None:
59
- validations["header_bg_color"] = (header_bg_color, [(is_valid_rgb, "must be a valid RGB list [R, G, B] with values 0-255")])
60
- if body_bg_color is not None:
61
- validations["body_bg_color"] = (body_bg_color, [(is_valid_rgb, "must be a valid RGB list [R, G, B] with values 0-255")])
62
- if border_color is not None:
63
- validations["border_color"] = (border_color, [(is_valid_rgb, "must be a valid RGB list [R, G, B] with values 0-255")])
64
-
65
- valid, error = validate_parameters(validations)
66
- if not valid:
67
- return {"error": error}
68
-
69
- # Validate data if provided
70
- if data:
71
- if len(data) != rows:
72
- return {
73
- "error": f"Data has {len(data)} rows but table should have {rows} rows"
74
- }
75
- for i, row in enumerate(data):
76
- if len(row) != cols:
77
- return {
78
- "error": f"Row {i} has {len(row)} columns but table should have {cols} columns"
79
- }
80
-
81
- try:
82
- # Add the table
83
- table_shape = ppt_utils.add_table(slide, rows, cols, left, top, width, height)
84
- table = table_shape.table
85
-
86
- # Populate with data if provided
87
- if data:
88
- for r in range(rows):
89
- for c in range(cols):
90
- if r < len(data) and c < len(data[r]):
91
- table.cell(r, c).text = str(data[r][c])
92
-
93
- # Apply formatting
94
- for r in range(rows):
95
- for c in range(cols):
96
- cell = table.cell(r, c)
97
-
98
- # Header row formatting
99
- if r == 0 and header_row:
100
- if header_bg_color:
101
- ppt_utils.format_table_cell(
102
- cell, bg_color=tuple(header_bg_color), font_size=header_font_size, bold=True
103
- )
104
- else:
105
- ppt_utils.format_table_cell(cell, font_size=header_font_size, bold=True)
106
- else:
107
- # Body cell formatting
108
- if body_bg_color:
109
- ppt_utils.format_table_cell(
110
- cell, bg_color=tuple(body_bg_color), font_size=body_font_size
111
- )
112
- else:
113
- ppt_utils.format_table_cell(cell, font_size=body_font_size)
114
-
115
- return {
116
- "message": f"Added {rows}x{cols} table to slide {slide_index}",
117
- "shape_index": len(slide.shapes) - 1,
118
- "rows": rows,
119
- "cols": cols
120
- }
121
- except Exception as e:
122
- return {
123
- "error": f"Failed to add table: {str(e)}"
124
- }
125
-
126
- @app.tool()
127
- def format_table_cell(
128
- slide_index: int,
129
- shape_index: int,
130
- row: int,
131
- col: int,
132
- font_size: Optional[int] = None,
133
- font_name: Optional[str] = None,
134
- bold: Optional[bool] = None,
135
- italic: Optional[bool] = None,
136
- color: Optional[List[int]] = None,
137
- bg_color: Optional[List[int]] = None,
138
- alignment: Optional[str] = None,
139
- vertical_alignment: Optional[str] = None,
140
- presentation_id: Optional[str] = None
141
- ) -> Dict:
142
- """Format a specific table cell."""
143
- pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
144
-
145
- if pres_id is None or pres_id not in presentations:
146
- return {
147
- "error": "No presentation is currently loaded or the specified ID is invalid"
148
- }
149
-
150
- pres = presentations[pres_id]
151
-
152
- if slide_index < 0 or slide_index >= len(pres.slides):
153
- return {
154
- "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
155
- }
156
-
157
- slide = pres.slides[slide_index]
158
-
159
- if shape_index < 0 or shape_index >= len(slide.shapes):
160
- return {
161
- "error": f"Invalid shape index: {shape_index}. Available shapes: 0-{len(slide.shapes) - 1}"
162
- }
163
-
164
- shape = slide.shapes[shape_index]
165
-
166
- try:
167
- if not hasattr(shape, 'table'):
168
- return {
169
- "error": f"Shape at index {shape_index} is not a table"
170
- }
171
-
172
- table = shape.table
173
-
174
- if row < 0 or row >= len(table.rows):
175
- return {
176
- "error": f"Invalid row index: {row}. Available rows: 0-{len(table.rows) - 1}"
177
- }
178
-
179
- if col < 0 or col >= len(table.columns):
180
- return {
181
- "error": f"Invalid column index: {col}. Available columns: 0-{len(table.columns) - 1}"
182
- }
183
-
184
- cell = table.cell(row, col)
185
-
186
- ppt_utils.format_table_cell(
187
- cell,
188
- font_size=font_size,
189
- font_name=font_name,
190
- bold=bold,
191
- italic=italic,
192
- color=tuple(color) if color else None,
193
- bg_color=tuple(bg_color) if bg_color else None,
194
- alignment=alignment,
195
- vertical_alignment=vertical_alignment
196
- )
197
-
198
- return {
199
- "message": f"Formatted cell at row {row}, column {col} in table at shape index {shape_index} on slide {slide_index}"
200
- }
201
- except Exception as e:
202
- return {
203
- "error": f"Failed to format table cell: {str(e)}"
204
- }
205
-
206
- @app.tool()
207
- def add_shape(
208
- slide_index: int,
209
- shape_type: str,
210
- left: float,
211
- top: float,
212
- width: float,
213
- height: float,
214
- fill_color: Optional[List[int]] = None,
215
- line_color: Optional[List[int]] = None,
216
- line_width: Optional[float] = None,
217
- text: Optional[str] = None, # Add text to shape
218
- font_size: Optional[int] = None,
219
- font_color: Optional[List[int]] = None,
220
- presentation_id: Optional[str] = None
221
- ) -> Dict:
222
- """Add an auto shape to a slide with enhanced options."""
223
- pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
224
-
225
- if pres_id is None or pres_id not in presentations:
226
- return {
227
- "error": "No presentation is currently loaded or the specified ID is invalid"
228
- }
229
-
230
- pres = presentations[pres_id]
231
-
232
- if slide_index < 0 or slide_index >= len(pres.slides):
233
- return {
234
- "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
235
- }
236
-
237
- slide = pres.slides[slide_index]
238
-
239
- try:
240
- # Use the direct implementation that bypasses the enum issues
241
- shape = add_shape_direct(slide, shape_type, left, top, width, height)
242
-
243
- # Format the shape if formatting options are provided
244
- if any([fill_color, line_color, line_width]):
245
- ppt_utils.format_shape(
246
- shape,
247
- fill_color=tuple(fill_color) if fill_color else None,
248
- line_color=tuple(line_color) if line_color else None,
249
- line_width=line_width
250
- )
251
-
252
- # Add text to shape if provided
253
- if text and hasattr(shape, 'text_frame'):
254
- shape.text_frame.text = text
255
- if font_size or font_color:
256
- ppt_utils.format_text(
257
- shape.text_frame,
258
- font_size=font_size,
259
- color=tuple(font_color) if font_color else None
260
- )
261
-
262
- return {
263
- "message": f"Added {shape_type} shape to slide {slide_index}",
264
- "shape_index": len(slide.shapes) - 1
265
- }
266
- except ValueError as e:
267
- return {
268
- "error": str(e)
269
- }
270
- except Exception as e:
271
- return {
272
- "error": f"Failed to add shape '{shape_type}': {str(e)}"
273
- }
274
-
275
- @app.tool()
276
- def add_chart(
277
- slide_index: int,
278
- chart_type: str,
279
- left: float,
280
- top: float,
281
- width: float,
282
- height: float,
283
- categories: List[str],
284
- series_names: List[str],
285
- series_values: List[List[float]],
286
- has_legend: bool = True,
287
- legend_position: str = "right",
288
- has_data_labels: bool = False,
289
- title: Optional[str] = None,
290
- x_axis_title: Optional[str] = None,
291
- y_axis_title: Optional[str] = None,
292
- color_scheme: Optional[str] = None,
293
- presentation_id: Optional[str] = None
294
- ) -> Dict:
295
- """Add a chart to a slide with comprehensive formatting options."""
296
- pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
297
-
298
- if pres_id is None or pres_id not in presentations:
299
- return {
300
- "error": "No presentation is currently loaded or the specified ID is invalid"
301
- }
302
-
303
- pres = presentations[pres_id]
304
-
305
- if slide_index < 0 or slide_index >= len(pres.slides):
306
- return {
307
- "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
308
- }
309
-
310
- slide = pres.slides[slide_index]
311
-
312
- # Validate chart type
313
- valid_chart_types = [
314
- 'column', 'stacked_column', 'bar', 'stacked_bar', 'line',
315
- 'line_markers', 'pie', 'doughnut', 'area', 'stacked_area',
316
- 'scatter', 'radar', 'radar_markers'
317
- ]
318
- if chart_type.lower() not in valid_chart_types:
319
- return {
320
- "error": f"Invalid chart type: '{chart_type}'. Valid types are: {', '.join(valid_chart_types)}"
321
- }
322
-
323
- # Validate series data
324
- if len(series_names) != len(series_values):
325
- return {
326
- "error": f"Number of series names ({len(series_names)}) must match number of series values ({len(series_values)})"
327
- }
328
-
329
- if not categories:
330
- return {
331
- "error": "Categories list cannot be empty"
332
- }
333
-
334
- # Validate that all series have the same number of values as categories
335
- for i, values in enumerate(series_values):
336
- if len(values) != len(categories):
337
- return {
338
- "error": f"Series '{series_names[i]}' has {len(values)} values but there are {len(categories)} categories"
339
- }
340
-
341
- try:
342
- # Add the chart
343
- chart = ppt_utils.add_chart(
344
- slide, chart_type, left, top, width, height,
345
- categories, series_names, series_values
346
- )
347
-
348
- if chart is None:
349
- return {"error": "Failed to create chart"}
350
-
351
- # Format the chart
352
- ppt_utils.format_chart(
353
- chart,
354
- has_legend=has_legend,
355
- legend_position=legend_position,
356
- has_data_labels=has_data_labels,
357
- title=title,
358
- x_axis_title=x_axis_title,
359
- y_axis_title=y_axis_title,
360
- color_scheme=color_scheme
361
- )
362
-
363
- return {
364
- "message": f"Added {chart_type} chart to slide {slide_index}",
365
- "shape_index": len(slide.shapes) - 1,
366
- "chart_type": chart_type,
367
- "series_count": len(series_names),
368
- "categories_count": len(categories)
369
- }
370
- except Exception as e:
371
- return {
372
- "error": f"Failed to add chart: {str(e)}"
1
+ """
2
+ Structural element tools for PowerPoint MCP Server.
3
+ Handles tables, shapes, and charts.
4
+ """
5
+ from typing import Dict, List, Optional, Any
6
+ from mcp.server.fastmcp import FastMCP
7
+ import utils as ppt_utils
8
+
9
+
10
+ def register_structural_tools(app: FastMCP, presentations: Dict, get_current_presentation_id, validate_parameters, is_positive, is_non_negative, is_in_range, is_valid_rgb, add_shape_direct):
11
+ """Register structural element tools with the FastMCP app"""
12
+
13
+ @app.tool()
14
+ def add_table(
15
+ slide_index: int,
16
+ rows: int,
17
+ cols: int,
18
+ left: float,
19
+ top: float,
20
+ width: float,
21
+ height: float,
22
+ data: Optional[List[List[str]]] = None,
23
+ header_row: bool = True,
24
+ header_font_size: int = 12,
25
+ body_font_size: int = 10,
26
+ header_bg_color: Optional[List[int]] = None,
27
+ body_bg_color: Optional[List[int]] = None,
28
+ border_color: Optional[List[int]] = None,
29
+ presentation_id: Optional[str] = None
30
+ ) -> Dict:
31
+ """Add a table to a slide with enhanced formatting options."""
32
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
33
+
34
+ if pres_id is None or pres_id not in presentations:
35
+ return {
36
+ "error": "No presentation is currently loaded or the specified ID is invalid"
37
+ }
38
+
39
+ pres = presentations[pres_id]
40
+
41
+ if slide_index < 0 or slide_index >= len(pres.slides):
42
+ return {
43
+ "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
44
+ }
45
+
46
+ slide = pres.slides[slide_index]
47
+
48
+ # Validate parameters
49
+ validations = {
50
+ "rows": (rows, [(is_positive, "must be a positive integer")]),
51
+ "cols": (cols, [(is_positive, "must be a positive integer")]),
52
+ "left": (left, [(is_non_negative, "must be non-negative")]),
53
+ "top": (top, [(is_non_negative, "must be non-negative")]),
54
+ "width": (width, [(is_positive, "must be positive")]),
55
+ "height": (height, [(is_positive, "must be positive")])
56
+ }
57
+
58
+ if header_bg_color is not None:
59
+ validations["header_bg_color"] = (header_bg_color, [(is_valid_rgb, "must be a valid RGB list [R, G, B] with values 0-255")])
60
+ if body_bg_color is not None:
61
+ validations["body_bg_color"] = (body_bg_color, [(is_valid_rgb, "must be a valid RGB list [R, G, B] with values 0-255")])
62
+ if border_color is not None:
63
+ validations["border_color"] = (border_color, [(is_valid_rgb, "must be a valid RGB list [R, G, B] with values 0-255")])
64
+
65
+ valid, error = validate_parameters(validations)
66
+ if not valid:
67
+ return {"error": error}
68
+
69
+ # Validate data if provided
70
+ if data:
71
+ if len(data) != rows:
72
+ return {
73
+ "error": f"Data has {len(data)} rows but table should have {rows} rows"
74
+ }
75
+ for i, row in enumerate(data):
76
+ if len(row) != cols:
77
+ return {
78
+ "error": f"Row {i} has {len(row)} columns but table should have {cols} columns"
79
+ }
80
+
81
+ try:
82
+ # Add the table
83
+ table_shape = ppt_utils.add_table(slide, rows, cols, left, top, width, height)
84
+ table = table_shape.table
85
+
86
+ # Populate with data if provided
87
+ if data:
88
+ for r in range(rows):
89
+ for c in range(cols):
90
+ if r < len(data) and c < len(data[r]):
91
+ table.cell(r, c).text = str(data[r][c])
92
+
93
+ # Apply formatting
94
+ for r in range(rows):
95
+ for c in range(cols):
96
+ cell = table.cell(r, c)
97
+
98
+ # Header row formatting
99
+ if r == 0 and header_row:
100
+ if header_bg_color:
101
+ ppt_utils.format_table_cell(
102
+ cell, bg_color=tuple(header_bg_color), font_size=header_font_size, bold=True
103
+ )
104
+ else:
105
+ ppt_utils.format_table_cell(cell, font_size=header_font_size, bold=True)
106
+ else:
107
+ # Body cell formatting
108
+ if body_bg_color:
109
+ ppt_utils.format_table_cell(
110
+ cell, bg_color=tuple(body_bg_color), font_size=body_font_size
111
+ )
112
+ else:
113
+ ppt_utils.format_table_cell(cell, font_size=body_font_size)
114
+
115
+ return {
116
+ "message": f"Added {rows}x{cols} table to slide {slide_index}",
117
+ "shape_index": len(slide.shapes) - 1,
118
+ "rows": rows,
119
+ "cols": cols
120
+ }
121
+ except Exception as e:
122
+ return {
123
+ "error": f"Failed to add table: {str(e)}"
124
+ }
125
+
126
+ @app.tool()
127
+ def format_table_cell(
128
+ slide_index: int,
129
+ shape_index: int,
130
+ row: int,
131
+ col: int,
132
+ font_size: Optional[int] = None,
133
+ font_name: Optional[str] = None,
134
+ bold: Optional[bool] = None,
135
+ italic: Optional[bool] = None,
136
+ color: Optional[List[int]] = None,
137
+ bg_color: Optional[List[int]] = None,
138
+ alignment: Optional[str] = None,
139
+ vertical_alignment: Optional[str] = None,
140
+ presentation_id: Optional[str] = None
141
+ ) -> Dict:
142
+ """Format a specific table cell."""
143
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
144
+
145
+ if pres_id is None or pres_id not in presentations:
146
+ return {
147
+ "error": "No presentation is currently loaded or the specified ID is invalid"
148
+ }
149
+
150
+ pres = presentations[pres_id]
151
+
152
+ if slide_index < 0 or slide_index >= len(pres.slides):
153
+ return {
154
+ "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
155
+ }
156
+
157
+ slide = pres.slides[slide_index]
158
+
159
+ if shape_index < 0 or shape_index >= len(slide.shapes):
160
+ return {
161
+ "error": f"Invalid shape index: {shape_index}. Available shapes: 0-{len(slide.shapes) - 1}"
162
+ }
163
+
164
+ shape = slide.shapes[shape_index]
165
+
166
+ try:
167
+ if not hasattr(shape, 'table'):
168
+ return {
169
+ "error": f"Shape at index {shape_index} is not a table"
170
+ }
171
+
172
+ table = shape.table
173
+
174
+ if row < 0 or row >= len(table.rows):
175
+ return {
176
+ "error": f"Invalid row index: {row}. Available rows: 0-{len(table.rows) - 1}"
177
+ }
178
+
179
+ if col < 0 or col >= len(table.columns):
180
+ return {
181
+ "error": f"Invalid column index: {col}. Available columns: 0-{len(table.columns) - 1}"
182
+ }
183
+
184
+ cell = table.cell(row, col)
185
+
186
+ ppt_utils.format_table_cell(
187
+ cell,
188
+ font_size=font_size,
189
+ font_name=font_name,
190
+ bold=bold,
191
+ italic=italic,
192
+ color=tuple(color) if color else None,
193
+ bg_color=tuple(bg_color) if bg_color else None,
194
+ alignment=alignment,
195
+ vertical_alignment=vertical_alignment
196
+ )
197
+
198
+ return {
199
+ "message": f"Formatted cell at row {row}, column {col} in table at shape index {shape_index} on slide {slide_index}"
200
+ }
201
+ except Exception as e:
202
+ return {
203
+ "error": f"Failed to format table cell: {str(e)}"
204
+ }
205
+
206
+ @app.tool()
207
+ def add_shape(
208
+ slide_index: int,
209
+ shape_type: str,
210
+ left: float,
211
+ top: float,
212
+ width: float,
213
+ height: float,
214
+ fill_color: Optional[List[int]] = None,
215
+ line_color: Optional[List[int]] = None,
216
+ line_width: Optional[float] = None,
217
+ text: Optional[str] = None, # Add text to shape
218
+ font_size: Optional[int] = None,
219
+ font_color: Optional[List[int]] = None,
220
+ presentation_id: Optional[str] = None
221
+ ) -> Dict:
222
+ """Add an auto shape to a slide with enhanced options."""
223
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
224
+
225
+ if pres_id is None or pres_id not in presentations:
226
+ return {
227
+ "error": "No presentation is currently loaded or the specified ID is invalid"
228
+ }
229
+
230
+ pres = presentations[pres_id]
231
+
232
+ if slide_index < 0 or slide_index >= len(pres.slides):
233
+ return {
234
+ "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
235
+ }
236
+
237
+ slide = pres.slides[slide_index]
238
+
239
+ try:
240
+ # Use the direct implementation that bypasses the enum issues
241
+ shape = add_shape_direct(slide, shape_type, left, top, width, height)
242
+
243
+ # Format the shape if formatting options are provided
244
+ if any([fill_color, line_color, line_width]):
245
+ ppt_utils.format_shape(
246
+ shape,
247
+ fill_color=tuple(fill_color) if fill_color else None,
248
+ line_color=tuple(line_color) if line_color else None,
249
+ line_width=line_width
250
+ )
251
+
252
+ # Add text to shape if provided
253
+ if text and hasattr(shape, 'text_frame'):
254
+ shape.text_frame.text = text
255
+ if font_size or font_color:
256
+ ppt_utils.format_text(
257
+ shape.text_frame,
258
+ font_size=font_size,
259
+ color=tuple(font_color) if font_color else None
260
+ )
261
+
262
+ return {
263
+ "message": f"Added {shape_type} shape to slide {slide_index}",
264
+ "shape_index": len(slide.shapes) - 1
265
+ }
266
+ except ValueError as e:
267
+ return {
268
+ "error": str(e)
269
+ }
270
+ except Exception as e:
271
+ return {
272
+ "error": f"Failed to add shape '{shape_type}': {str(e)}"
273
+ }
274
+
275
+ @app.tool()
276
+ def add_chart(
277
+ slide_index: int,
278
+ chart_type: str,
279
+ left: float,
280
+ top: float,
281
+ width: float,
282
+ height: float,
283
+ categories: List[str],
284
+ series_names: List[str],
285
+ series_values: List[List[float]],
286
+ has_legend: bool = True,
287
+ legend_position: str = "right",
288
+ has_data_labels: bool = False,
289
+ title: Optional[str] = None,
290
+ x_axis_title: Optional[str] = None,
291
+ y_axis_title: Optional[str] = None,
292
+ color_scheme: Optional[str] = None,
293
+ presentation_id: Optional[str] = None
294
+ ) -> Dict:
295
+ """Add a chart to a slide with comprehensive formatting options."""
296
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
297
+
298
+ if pres_id is None or pres_id not in presentations:
299
+ return {
300
+ "error": "No presentation is currently loaded or the specified ID is invalid"
301
+ }
302
+
303
+ pres = presentations[pres_id]
304
+
305
+ if slide_index < 0 or slide_index >= len(pres.slides):
306
+ return {
307
+ "error": f"Invalid slide index: {slide_index}. Available slides: 0-{len(pres.slides) - 1}"
308
+ }
309
+
310
+ slide = pres.slides[slide_index]
311
+
312
+ # Validate chart type
313
+ valid_chart_types = [
314
+ 'column', 'stacked_column', 'bar', 'stacked_bar', 'line',
315
+ 'line_markers', 'pie', 'doughnut', 'area', 'stacked_area',
316
+ 'scatter', 'radar', 'radar_markers'
317
+ ]
318
+ if chart_type.lower() not in valid_chart_types:
319
+ return {
320
+ "error": f"Invalid chart type: '{chart_type}'. Valid types are: {', '.join(valid_chart_types)}"
321
+ }
322
+
323
+ # Validate series data
324
+ if len(series_names) != len(series_values):
325
+ return {
326
+ "error": f"Number of series names ({len(series_names)}) must match number of series values ({len(series_values)})"
327
+ }
328
+
329
+ if not categories:
330
+ return {
331
+ "error": "Categories list cannot be empty"
332
+ }
333
+
334
+ # Validate that all series have the same number of values as categories
335
+ for i, values in enumerate(series_values):
336
+ if len(values) != len(categories):
337
+ return {
338
+ "error": f"Series '{series_names[i]}' has {len(values)} values but there are {len(categories)} categories"
339
+ }
340
+
341
+ try:
342
+ # Add the chart
343
+ chart = ppt_utils.add_chart(
344
+ slide, chart_type, left, top, width, height,
345
+ categories, series_names, series_values
346
+ )
347
+
348
+ if chart is None:
349
+ return {"error": "Failed to create chart"}
350
+
351
+ # Format the chart
352
+ ppt_utils.format_chart(
353
+ chart,
354
+ has_legend=has_legend,
355
+ legend_position=legend_position,
356
+ has_data_labels=has_data_labels,
357
+ title=title,
358
+ x_axis_title=x_axis_title,
359
+ y_axis_title=y_axis_title,
360
+ color_scheme=color_scheme
361
+ )
362
+
363
+ return {
364
+ "message": f"Added {chart_type} chart to slide {slide_index}",
365
+ "shape_index": len(slide.shapes) - 1,
366
+ "chart_type": chart_type,
367
+ "series_count": len(series_names),
368
+ "categories_count": len(categories)
369
+ }
370
+ except Exception as e:
371
+ return {
372
+ "error": f"Failed to add chart: {str(e)}"
373
373
  }