PPTXToolkit

class PPTXToolkit(BaseToolkit):

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

def __init__(self, output_dir: str = './', timeout: Optional[float] = None):

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

def _resolve_filepath(self, file_path: str):

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

def _sanitize_filename(self, filename: str):

Sanitize a filename by replacing special characters and spaces.

Parameters:

  • filename (str): The filename to sanitize.

Returns:

str: The sanitized filename.

_format_text

def _format_text(
    self,
    frame_paragraph,
    text: str,
    set_color_to_white = False
):

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

def _add_bulleted_items(
    self,
    text_frame: 'TextFrame',
    flat_items_list: List[Tuple[str, int]],
    set_color_to_white: bool = False
):

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

def _get_flat_list_of_contents(self, items: List[Union[str, List[Any]]], level: int):

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

def _get_slide_width_height_inches(self, presentation: 'presentation.Presentation'):

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

def _write_pptx_file(
    self,
    file_path: Path,
    content: List[Dict[str, Any]],
    template: Optional[str] = None
):

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

def create_presentation(
    self,
    content: str,
    filename: str,
    template: Optional[str] = None
):

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

def _handle_default_display(
    self,
    presentation: 'presentation.Presentation',
    slide_json: Dict[str, Any]
):

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

def _handle_display_image__in_foreground(
    self,
    presentation: 'presentation.Presentation',
    slide_json: Dict[str, Any]
):

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

def _handle_table(
    self,
    presentation: 'presentation.Presentation',
    slide_json: Dict[str, Any]
):

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

def _handle_step_by_step_process(
    self,
    presentation: 'presentation.Presentation',
    slide_json: Dict[str, Any],
    slide_width_inch: float,
    slide_height_inch: float
):

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

def _remove_slide_number_from_heading(self, header: str):

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

def _get_slide_placeholders(self, slide: 'Slide'):

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

def get_tools(self):

Returns:

List[FunctionTool]: A list of FunctionTool objects representing the functions in the toolkit.