mcpcn-office-powerpoint-mcp-server 2.0.7__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.
@@ -0,0 +1,138 @@
1
+ """
2
+ Hyperlink management tools for PowerPoint MCP Server.
3
+ Implements hyperlink operations for text shapes and runs.
4
+ """
5
+
6
+ from typing import Dict, List, Optional, Any
7
+
8
+ def register_hyperlink_tools(app, presentations, get_current_presentation_id, validate_parameters,
9
+ is_positive, is_non_negative, is_in_range, is_valid_rgb):
10
+ """Register hyperlink management tools with the FastMCP app."""
11
+
12
+ @app.tool()
13
+ def manage_hyperlinks(
14
+ operation: str,
15
+ slide_index: int,
16
+ shape_index: int = None,
17
+ text: str = None,
18
+ url: str = None,
19
+ run_index: int = 0,
20
+ presentation_id: str = None
21
+ ) -> Dict:
22
+ """
23
+ Manage hyperlinks in text shapes and runs.
24
+
25
+ Args:
26
+ operation: Operation type ("add", "remove", "list", "update")
27
+ slide_index: Index of the slide (0-based)
28
+ shape_index: Index of the shape on the slide (0-based)
29
+ text: Text to make into hyperlink (for "add" operation)
30
+ url: URL for the hyperlink
31
+ run_index: Index of text run within the shape (0-based)
32
+ presentation_id: Optional presentation ID (uses current if not provided)
33
+
34
+ Returns:
35
+ Dictionary with operation results
36
+ """
37
+ try:
38
+ # Get presentation
39
+ pres_id = presentation_id or get_current_presentation_id()
40
+ if pres_id not in presentations:
41
+ return {"error": "Presentation not found"}
42
+
43
+ pres = presentations[pres_id]
44
+
45
+ # Validate slide index
46
+ if not (0 <= slide_index < len(pres.slides)):
47
+ return {"error": f"Slide index {slide_index} out of range"}
48
+
49
+ slide = pres.slides[slide_index]
50
+
51
+ if operation == "list":
52
+ # List all hyperlinks in the slide
53
+ hyperlinks = []
54
+ for shape_idx, shape in enumerate(slide.shapes):
55
+ if hasattr(shape, 'text_frame') and shape.text_frame:
56
+ for para_idx, paragraph in enumerate(shape.text_frame.paragraphs):
57
+ for run_idx, run in enumerate(paragraph.runs):
58
+ if run.hyperlink.address:
59
+ hyperlinks.append({
60
+ "shape_index": shape_idx,
61
+ "paragraph_index": para_idx,
62
+ "run_index": run_idx,
63
+ "text": run.text,
64
+ "url": run.hyperlink.address
65
+ })
66
+
67
+ return {
68
+ "message": f"Found {len(hyperlinks)} hyperlinks on slide {slide_index}",
69
+ "hyperlinks": hyperlinks
70
+ }
71
+
72
+ # For other operations, validate shape index
73
+ if shape_index is None or not (0 <= shape_index < len(slide.shapes)):
74
+ return {"error": f"Shape index {shape_index} out of range"}
75
+
76
+ shape = slide.shapes[shape_index]
77
+
78
+ # Check if shape has text
79
+ if not hasattr(shape, 'text_frame') or not shape.text_frame:
80
+ return {"error": "Shape does not contain text"}
81
+
82
+ if operation == "add":
83
+ if not text or not url:
84
+ return {"error": "Both 'text' and 'url' are required for adding hyperlinks"}
85
+
86
+ # Add new text run with hyperlink
87
+ paragraph = shape.text_frame.paragraphs[0]
88
+ run = paragraph.add_run()
89
+ run.text = text
90
+ run.hyperlink.address = url
91
+
92
+ return {
93
+ "message": f"Added hyperlink '{text}' -> '{url}' to shape {shape_index}",
94
+ "text": text,
95
+ "url": url
96
+ }
97
+
98
+ elif operation == "update":
99
+ if not url:
100
+ return {"error": "URL is required for updating hyperlinks"}
101
+
102
+ # Update existing hyperlink
103
+ paragraphs = shape.text_frame.paragraphs
104
+ if run_index < len(paragraphs[0].runs):
105
+ run = paragraphs[0].runs[run_index]
106
+ old_url = run.hyperlink.address
107
+ run.hyperlink.address = url
108
+
109
+ return {
110
+ "message": f"Updated hyperlink from '{old_url}' to '{url}'",
111
+ "old_url": old_url,
112
+ "new_url": url,
113
+ "text": run.text
114
+ }
115
+ else:
116
+ return {"error": f"Run index {run_index} out of range"}
117
+
118
+ elif operation == "remove":
119
+ # Remove hyperlink from specific run
120
+ paragraphs = shape.text_frame.paragraphs
121
+ if run_index < len(paragraphs[0].runs):
122
+ run = paragraphs[0].runs[run_index]
123
+ old_url = run.hyperlink.address
124
+ run.hyperlink.address = None
125
+
126
+ return {
127
+ "message": f"Removed hyperlink '{old_url}' from text '{run.text}'",
128
+ "removed_url": old_url,
129
+ "text": run.text
130
+ }
131
+ else:
132
+ return {"error": f"Run index {run_index} out of range"}
133
+
134
+ else:
135
+ return {"error": f"Unsupported operation: {operation}. Use 'add', 'remove', 'list', or 'update'"}
136
+
137
+ except Exception as e:
138
+ return {"error": f"Failed to manage hyperlinks: {str(e)}"}
tools/master_tools.py ADDED
@@ -0,0 +1,114 @@
1
+ """
2
+ Slide master management tools for PowerPoint MCP Server.
3
+ Implements slide master and layout access capabilities.
4
+ """
5
+
6
+ from typing import Dict, List, Optional, Any
7
+
8
+ def register_master_tools(app, presentations, get_current_presentation_id, validate_parameters,
9
+ is_positive, is_non_negative, is_in_range, is_valid_rgb):
10
+ """Register slide master management tools with the FastMCP app."""
11
+
12
+ @app.tool()
13
+ def manage_slide_masters(
14
+ operation: str,
15
+ master_index: int = 0,
16
+ layout_index: int = None,
17
+ presentation_id: str = None
18
+ ) -> Dict:
19
+ """
20
+ Access and manage slide master properties and layouts.
21
+
22
+ Args:
23
+ operation: Operation type ("list", "get_layouts", "get_info")
24
+ master_index: Index of the slide master (0-based)
25
+ layout_index: Index of specific layout within master (0-based)
26
+ presentation_id: Optional presentation ID (uses current if not provided)
27
+
28
+ Returns:
29
+ Dictionary with slide master information
30
+ """
31
+ try:
32
+ # Get presentation
33
+ pres_id = presentation_id or get_current_presentation_id()
34
+ if pres_id not in presentations:
35
+ return {"error": "Presentation not found"}
36
+
37
+ pres = presentations[pres_id]
38
+
39
+ if operation == "list":
40
+ # List all slide masters
41
+ masters_info = []
42
+ for idx, master in enumerate(pres.slide_masters):
43
+ masters_info.append({
44
+ "index": idx,
45
+ "layout_count": len(master.slide_layouts),
46
+ "name": getattr(master, 'name', f"Master {idx}")
47
+ })
48
+
49
+ return {
50
+ "message": f"Found {len(masters_info)} slide masters",
51
+ "masters": masters_info,
52
+ "total_masters": len(pres.slide_masters)
53
+ }
54
+
55
+ # Validate master index
56
+ if not (0 <= master_index < len(pres.slide_masters)):
57
+ return {"error": f"Master index {master_index} out of range"}
58
+
59
+ master = pres.slide_masters[master_index]
60
+
61
+ if operation == "get_layouts":
62
+ # Get all layouts for a specific master
63
+ layouts_info = []
64
+ for idx, layout in enumerate(master.slide_layouts):
65
+ layouts_info.append({
66
+ "index": idx,
67
+ "name": layout.name,
68
+ "placeholder_count": len(layout.placeholders) if hasattr(layout, 'placeholders') else 0
69
+ })
70
+
71
+ return {
72
+ "message": f"Master {master_index} has {len(layouts_info)} layouts",
73
+ "master_index": master_index,
74
+ "layouts": layouts_info
75
+ }
76
+
77
+ elif operation == "get_info":
78
+ # Get detailed info about master or specific layout
79
+ if layout_index is not None:
80
+ if not (0 <= layout_index < len(master.slide_layouts)):
81
+ return {"error": f"Layout index {layout_index} out of range"}
82
+
83
+ layout = master.slide_layouts[layout_index]
84
+ placeholders_info = []
85
+
86
+ if hasattr(layout, 'placeholders'):
87
+ for placeholder in layout.placeholders:
88
+ placeholders_info.append({
89
+ "idx": placeholder.placeholder_format.idx,
90
+ "type": str(placeholder.placeholder_format.type),
91
+ "name": getattr(placeholder, 'name', 'Unnamed')
92
+ })
93
+
94
+ return {
95
+ "message": f"Layout info for master {master_index}, layout {layout_index}",
96
+ "master_index": master_index,
97
+ "layout_index": layout_index,
98
+ "layout_name": layout.name,
99
+ "placeholders": placeholders_info
100
+ }
101
+ else:
102
+ # Master info
103
+ return {
104
+ "message": f"Master {master_index} information",
105
+ "master_index": master_index,
106
+ "layout_count": len(master.slide_layouts),
107
+ "name": getattr(master, 'name', f"Master {master_index}")
108
+ }
109
+
110
+ else:
111
+ return {"error": f"Unsupported operation: {operation}. Use 'list', 'get_layouts', or 'get_info'"}
112
+
113
+ except Exception as e:
114
+ return {"error": f"Failed to manage slide masters: {str(e)}"}
@@ -0,0 +1,212 @@
1
+ """
2
+ Presentation management tools for PowerPoint MCP Server.
3
+ Handles presentation creation, opening, saving, and core properties.
4
+ """
5
+ from typing import Dict, List, Optional, Any
6
+ import os
7
+ from mcp.server.fastmcp import FastMCP
8
+ import utils as ppt_utils
9
+
10
+
11
+ def register_presentation_tools(app: FastMCP, presentations: Dict, get_current_presentation_id, get_template_search_directories):
12
+ """Register presentation management tools with the FastMCP app"""
13
+
14
+ @app.tool()
15
+ def create_presentation(id: Optional[str] = None) -> Dict:
16
+ """Create a new PowerPoint presentation."""
17
+ # Create a new presentation
18
+ pres = ppt_utils.create_presentation()
19
+
20
+ # Generate an ID if not provided
21
+ if id is None:
22
+ id = f"presentation_{len(presentations) + 1}"
23
+
24
+ # Store the presentation
25
+ presentations[id] = pres
26
+ # Set as current presentation (this would need to be handled by caller)
27
+
28
+ return {
29
+ "presentation_id": id,
30
+ "message": f"Created new presentation with ID: {id}",
31
+ "slide_count": len(pres.slides)
32
+ }
33
+
34
+ @app.tool()
35
+ def create_presentation_from_template(template_path: str, id: Optional[str] = None) -> Dict:
36
+ """Create a new PowerPoint presentation from a template file."""
37
+ # Check if template file exists
38
+ if not os.path.exists(template_path):
39
+ # Try to find the template by searching in configured directories
40
+ search_dirs = get_template_search_directories()
41
+ template_name = os.path.basename(template_path)
42
+
43
+ for directory in search_dirs:
44
+ potential_path = os.path.join(directory, template_name)
45
+ if os.path.exists(potential_path):
46
+ template_path = potential_path
47
+ break
48
+ else:
49
+ env_path_info = f" (PPT_TEMPLATE_PATH: {os.environ.get('PPT_TEMPLATE_PATH', 'not set')})" if os.environ.get('PPT_TEMPLATE_PATH') else ""
50
+ return {
51
+ "error": f"Template file not found: {template_path}. Searched in {', '.join(search_dirs)}{env_path_info}"
52
+ }
53
+
54
+ # Create presentation from template
55
+ try:
56
+ pres = ppt_utils.create_presentation_from_template(template_path)
57
+ except Exception as e:
58
+ return {
59
+ "error": f"Failed to create presentation from template: {str(e)}"
60
+ }
61
+
62
+ # Generate an ID if not provided
63
+ if id is None:
64
+ id = f"presentation_{len(presentations) + 1}"
65
+
66
+ # Store the presentation
67
+ presentations[id] = pres
68
+
69
+ return {
70
+ "presentation_id": id,
71
+ "message": f"Created new presentation from template '{template_path}' with ID: {id}",
72
+ "template_path": template_path,
73
+ "slide_count": len(pres.slides),
74
+ "layout_count": len(pres.slide_layouts)
75
+ }
76
+
77
+ @app.tool()
78
+ def open_presentation(file_path: str, id: Optional[str] = None) -> Dict:
79
+ """Open an existing PowerPoint presentation from a file."""
80
+ # Check if file exists
81
+ if not os.path.exists(file_path):
82
+ return {
83
+ "error": f"File not found: {file_path}"
84
+ }
85
+
86
+ # Open the presentation
87
+ try:
88
+ pres = ppt_utils.open_presentation(file_path)
89
+ except Exception as e:
90
+ return {
91
+ "error": f"Failed to open presentation: {str(e)}"
92
+ }
93
+
94
+ # Generate an ID if not provided
95
+ if id is None:
96
+ id = f"presentation_{len(presentations) + 1}"
97
+
98
+ # Store the presentation
99
+ presentations[id] = pres
100
+
101
+ return {
102
+ "presentation_id": id,
103
+ "message": f"Opened presentation from {file_path} with ID: {id}",
104
+ "slide_count": len(pres.slides)
105
+ }
106
+
107
+ @app.tool()
108
+ def save_presentation(file_path: str, presentation_id: Optional[str] = None) -> Dict:
109
+ """Save a presentation to a file."""
110
+ # Use the specified presentation or the current one
111
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
112
+
113
+ if pres_id is None or pres_id not in presentations:
114
+ return {
115
+ "error": "No presentation is currently loaded or the specified ID is invalid"
116
+ }
117
+
118
+ # Save the presentation
119
+ try:
120
+ saved_path = ppt_utils.save_presentation(presentations[pres_id], file_path)
121
+ return {
122
+ "message": f"Presentation saved to {saved_path}",
123
+ "file_path": saved_path
124
+ }
125
+ except Exception as e:
126
+ return {
127
+ "error": f"Failed to save presentation: {str(e)}"
128
+ }
129
+
130
+ @app.tool()
131
+ def get_presentation_info(presentation_id: Optional[str] = None) -> Dict:
132
+ """Get information about a presentation."""
133
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
134
+
135
+ if pres_id is None or pres_id not in presentations:
136
+ return {
137
+ "error": "No presentation is currently loaded or the specified ID is invalid"
138
+ }
139
+
140
+ pres = presentations[pres_id]
141
+
142
+ try:
143
+ info = ppt_utils.get_presentation_info(pres)
144
+ info["presentation_id"] = pres_id
145
+ return info
146
+ except Exception as e:
147
+ return {
148
+ "error": f"Failed to get presentation info: {str(e)}"
149
+ }
150
+
151
+ @app.tool()
152
+ def get_template_file_info(template_path: str) -> Dict:
153
+ """Get information about a template file including layouts and properties."""
154
+ # Check if template file exists
155
+ if not os.path.exists(template_path):
156
+ # Try to find the template by searching in configured directories
157
+ search_dirs = get_template_search_directories()
158
+ template_name = os.path.basename(template_path)
159
+
160
+ for directory in search_dirs:
161
+ potential_path = os.path.join(directory, template_name)
162
+ if os.path.exists(potential_path):
163
+ template_path = potential_path
164
+ break
165
+ else:
166
+ return {
167
+ "error": f"Template file not found: {template_path}. Searched in {', '.join(search_dirs)}"
168
+ }
169
+
170
+ try:
171
+ return ppt_utils.get_template_info(template_path)
172
+ except Exception as e:
173
+ return {
174
+ "error": f"Failed to get template info: {str(e)}"
175
+ }
176
+
177
+ @app.tool()
178
+ def set_core_properties(
179
+ title: Optional[str] = None,
180
+ subject: Optional[str] = None,
181
+ author: Optional[str] = None,
182
+ keywords: Optional[str] = None,
183
+ comments: Optional[str] = None,
184
+ presentation_id: Optional[str] = None
185
+ ) -> Dict:
186
+ """Set core document properties."""
187
+ pres_id = presentation_id if presentation_id is not None else get_current_presentation_id()
188
+
189
+ if pres_id is None or pres_id not in presentations:
190
+ return {
191
+ "error": "No presentation is currently loaded or the specified ID is invalid"
192
+ }
193
+
194
+ pres = presentations[pres_id]
195
+
196
+ try:
197
+ ppt_utils.set_core_properties(
198
+ pres,
199
+ title=title,
200
+ subject=subject,
201
+ author=author,
202
+ keywords=keywords,
203
+ comments=comments
204
+ )
205
+
206
+ return {
207
+ "message": "Core properties updated successfully"
208
+ }
209
+ except Exception as e:
210
+ return {
211
+ "error": f"Failed to set core properties: {str(e)}"
212
+ }