Camel.toolkits.pptx toolkit
PPTXToolkit
A toolkit for creating and writing PowerPoint presentations (PPTX files).
This class provides cross-platform support for creating PPTX files with title slides, content slides, text formatting, and image embedding.
init
Initialize the PPTXToolkit.
Parameters:
- output_dir (str): The default directory for output files. Defaults to the current working directory.
- timeout (Optional[float]): The timeout for the toolkit. (default: :obj:
None
)
_resolve_filepath
Convert the given string path to a Path object.
If the provided path is not absolute, it is made relative to the default output directory. The filename part is sanitized to replace spaces and special characters with underscores, ensuring safe usage in downstream processing.
Parameters:
- file_path (str): The file path to resolve.
Returns:
Path: A fully resolved (absolute) and sanitized Path object.
_sanitize_filename
Sanitize a filename by replacing special characters and spaces.
Parameters:
- filename (str): The filename to sanitize.
Returns:
str: The sanitized filename.
_format_text
Apply bold and italic formatting while preserving the original word order.
Parameters:
- frame_paragraph: The paragraph to format.
- text (str): The text to format.
- set_color_to_white (bool): Whether to set the color to white. (default: :obj:
False
)
_add_bulleted_items
Add a list of texts as bullet points and apply formatting.
Parameters:
- text_frame (TextFrame): The text frame where text is to be displayed.
- flat_items_list (List[Tuple[str, int]]): The list of items to be displayed.
- set_color_to_white (bool): Whether to set the font color to white. (default: :obj:
False
)
_get_flat_list_of_contents
Flatten a hierarchical list of bullet points to a single list.
Parameters:
- items (List[Union[str, List[Any]]]): A bullet point (string or list).
- level (int): The current level of hierarchy.
Returns:
List[Tuple[str, int]]: A list of (bullet item text, hierarchical level) tuples.
_get_slide_width_height_inches
Get the dimensions of a slide in inches.
Parameters:
- presentation (presentation.Presentation): The presentation object.
Returns:
Tuple[float, float]: The width and height in inches.
_write_pptx_file
Write text content to a PPTX file with enhanced formatting.
Parameters:
- file_path (Path): The target file path.
- content (List[Dict[str, Any]]): The content to write to the PPTX file. Must be a list of dictionaries where: - First element: Title slide with keys ‘title’ and ‘subtitle’ - Subsequent elements: Content slides with keys ‘title’, ‘text’
- template (Optional[str]): The name of the template to use. If not provided, the default template will be used. (default: :obj:
None
)
create_presentation
Create a PowerPoint presentation (PPTX) file.
Parameters:
- content (str): The content to write to the PPTX file as a JSON string. Must represent a list of dictionaries with the following structure: - First dict: title slide
{"title": str, "subtitle": str}
- Other dicts: content slides, which can be one of: * Bullet/step slides:{"heading": str, "bullet_points": list of str or nested lists, "img_keywords": str (optional)}
- If any bullet point starts with ’>> ’, it will be rendered as a step-by-step process. - “img_keywords” can be a URL or search keywords for an image (optional). * Table slides:{"heading": str, "table": {"headers": list of str, "rows": list of list of str}}
- filename (str): The name or path of the file. If a relative path is supplied, it is resolved to self.output_dir.
- template (Optional[str]): The path to the template PPTX file. Initializes a presentation from a given template file Or PPTX file. (default: :obj:
None
)
Returns:
str: A success message indicating the file was created.
_handle_default_display
Display a list of text in a slide.
Parameters:
- presentation (presentation.Presentation): The presentation object.
- slide_json (Dict[str, Any]): The content of the slide as JSON data.
_handle_display_image__in_foreground
Create a slide with text and image using a picture placeholder layout.
Parameters:
- presentation (presentation.Presentation): The presentation object.
- slide_json (Dict[str, Any]): The content of the slide as JSON data.
Returns:
bool: True if the slide has been processed.
_handle_table
Add a table to a slide.
Parameters:
- presentation (presentation.Presentation): The presentation object.
- slide_json (Dict[str, Any]): The content of the slide as JSON data.
_handle_step_by_step_process
Add shapes to display a step-by-step process in the slide.
Parameters:
- presentation (presentation.Presentation): The presentation object.
- slide_json (Dict[str, Any]): The content of the slide as JSON data.
- slide_width_inch (float): The width of the slide in inches.
- slide_height_inch (float): The height of the slide in inches.
_remove_slide_number_from_heading
Remove the slide number from a given slide header.
Parameters:
- header (str): The header of a slide.
Returns:
str: The header without slide number.
_get_slide_placeholders
Return the index and name of all placeholders present in a slide.
Parameters:
- slide (Slide): The slide.
Returns:
List[Tuple[int, str]]: A list containing placeholders (idx, name) tuples.
get_tools
Returns:
List[FunctionTool]: A list of FunctionTool objects representing the functions in the toolkit.