add-slide-picture-with-caption
Insert a new slide with an image and caption into an existing PowerPoint presentation by specifying the presentation name, slide title, caption text, and image path.
Instructions
Add a new slide with a picture and caption to an existing presentation
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| caption | Yes | Caption text to appear below the picture | |
| image_path | Yes | Path to the image file to insert | |
| presentation_name | Yes | Name of the presentation to add the slide to | |
| title | Yes | Title of the slide |
Implementation Reference
- Core handler function that creates a slide with picture and caption. Inserts image into placeholder, maintains aspect ratio by resizing and centering, sets title and caption, handles errors for missing files and invalid images.def add_picture_with_caption_slide(self, presentation_name: str, title: str, image_path: str, caption_text: str) -> Slide: """ For the given presentation builds a slide with the picture with caption template. Maintains the image's aspect ratio by adjusting the picture object after insertion. Args: presentation_name: The presentation to add the slide to title: The title of the slide image_path: The path to the image to insert caption_text: The caption content """ try: prs = self.presentations[presentation_name] except KeyError as e: raise ValueError(f"Presentation '{presentation_name}' not found") # Add a new slide with layout 8 (Picture with Caption) try: slide_layout = prs.slide_layouts[self.SLIDE_LAYOUT_PICTURE_WITH_CAPTION] slide = prs.slides.add_slide(slide_layout) except IndexError as e: error_message = f"Slide Index does not exist. Error: {str(e)}" raise ValueError(error_message) # Set the title title_shape = slide.shapes.title title_shape.text = title # Get the image placeholder try: placeholder = slide.placeholders[1] except IndexError as e: error_message = f"Placeholder index does not exist. Error {str(e)}" raise ValueError(error_message) # Insert the picture into the placeholder if not os.path.exists(image_path): raise FileNotFoundError(f"Image not found: {image_path}") try: picture = placeholder.insert_picture(image_path) except FileNotFoundError as e: error_message = f"Image not found during insertion: {str(e)}" raise except UnidentifiedImageError as e: error_message = f"Image file {image_path} is not a valid image: {str(e)}" raise ValueError(error_message) except Exception as e: error_message = f"An unexpected error occured during picture insertion: {str(e)}" raise # Get placeholder dimensions after picture insertion available_width = picture.width available_height = picture.height # Get original image dimensions directly from the picture object image_width, image_height = picture.image.size # Calculate aspect ratios placeholder_aspect_ratio = float(available_width) / float(available_height) image_aspect_ratio = float(image_width) / float(image_height) # Store initial position pos_left, pos_top = picture.left, picture.top # Remove any cropping picture.crop_top = 0 picture.crop_left = 0 picture.crop_bottom = 0 picture.crop_right = 0 # Adjust picture dimensions based on aspect ratio comparison if placeholder_aspect_ratio > image_aspect_ratio: # Placeholder is wider than image - adjust width down while maintaining height picture.width = int(image_aspect_ratio * available_height) picture.height = available_height else: # Placeholder is taller than image - adjust height down while maintaining width picture.height = int(available_width / image_aspect_ratio) picture.width = available_width # Center the image within the available space picture.left = pos_left + int((available_width - picture.width) / 2) picture.top = pos_top + int((available_height - picture.height) / 2) # Set the caption caption = slide.placeholders[2] caption.text = caption_text return slide
- src/powerpoint/server.py:461-488 (handler)Tool dispatch handler in server.call_tool that validates input arguments, sanitizes the image file path, calls the presentation manager's add_picture_with_caption_slide method, and returns success message.elif name == "add-slide-picture-with-caption": # Get arguments presentation_name = arguments["presentation_name"] title = arguments["title"] caption = arguments["caption"] file_name = arguments["image_path"] if not all([presentation_name, title, caption, file_name]): raise ValueError("Missing required arguments") if presentation_name not in presentation_manager.presentations: raise ValueError(f"Presentation not found: {presentation_name}") try: safe_file_path = sanitize_path(folder_path, file_name) except ValueError as e: raise ValueError(f"Invalid file path: {str(e)}") try: slide = presentation_manager.add_picture_with_caption_slide(presentation_name, title, str(safe_file_path), caption) except Exception as e: raise ValueError(f"Unable to add slide with caption and picture layout to {presentation_name}.pptx. Error: {str(e)}") return [types.TextContent( type="text", text=f"Successfully added slide with caption and picture layout to {presentation_name}.pptx" )]
- src/powerpoint/server.py:298-323 (registration)Tool registration in list_tools handler, defining the tool name, description, and input schema for validation.types.Tool( name="add-slide-picture-with-caption", description="Add a new slide with a picture and caption to an existing presentation", 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", }, "caption": { "type": "string", "description": "Caption text to appear below the picture" }, "image_path": { "type": "string", "description": "Path to the image file to insert" } }, "required": ["presentation_name", "title", "caption", "image_path"], }, ),