Figure Operations#

The figure_ops module provides functionality for converting PowerPoint and Keynote presentations to images and PDFs, with options for cropping and reformatting.

Main Functions#

slides_to_images#

cocopack.figure_ops.slides_to_images(input_path, output_path, filename_format='figure{:01d}.png', crop_images=True, margin_size='1cm', dpi=300)[source]

Convert presentation slides to image files.

Parameters:
  • input_path (str) – Path to the presentation file (.ppt, .pptx, or .key).

  • output_path (str) – Directory path where the images will be saved.

  • filename_format (str, optional) – Format string for the output filenames. Defaults to ‘figure{:01d}.png’.

  • crop_images (bool, optional) – Whether to crop whitespace around images. Defaults to True.

  • margin_size (str, optional) – Margin size to add around cropped images. Defaults to ‘1cm’.

  • dpi (int, optional) – DPI for the output images. Defaults to 300.

This is the primary function that detects file type (.key, .ppt, .pptx) and applies the appropriate conversion method.

Example:

from cocopack.figure_ops import slides_to_images

# Convert a presentation to PNG images, cropping whitespace
slides_to_images('presentation.pptx', 'output_folder',
                filename_format='figure{:02d}.png',
                crop_images=True,
                margin_size='0.5cm')

Presentation Conversion#

keynote_to_images#

cocopack.figure_ops.keynote_to_images(input_path, output_path, filename_format='figure{:01d}.png')[source]

Convert Keynote slides to image files using AppleScript.

Parameters:
  • input_path (str) – Path to the Keynote file.

  • output_path (str) – Directory path where the images will be saved.

  • filename_format (str, optional) – Format string for the output filenames. Defaults to ‘figure{:01d}.png’.

Note

This function only works on macOS systems with Keynote installed. Source: https://iworkautomation.com/keynote/document-export.html

Example:

from cocopack.figure_ops import keynote_to_images

# Convert a Keynote presentation to PNG images
keynote_to_images('presentation.key', 'output_folder')

powerpoint_to_images#

cocopack.figure_ops.powerpoint_to_images(input_path, output_path, filename_format='figure{:01d}.png')[source]

Convert PowerPoint slides to image files.

Parameters:
  • input_path (str) – Path to the PowerPoint file (.ppt or .pptx).

  • output_path (str) – Directory path where the images will be saved.

  • filename_format (str, optional) – Format string for the output filenames. Defaults to ‘figure{:01d}.png’.

Note

This function uses different methods depending on the operating system: - On macOS: Uses AppleScript with PowerPoint - On Windows: Uses win32com.client - On other platforms: Attempts to use LibreOffice or python-pptx

Example:

from cocopack.figure_ops import powerpoint_to_images

# Convert a PowerPoint presentation to PNG images
powerpoint_to_images('presentation.pptx', 'output_folder')

Image Processing#

crop_whitespace#

cocopack.figure_ops.crop_whitespace(image_path, output_path=None, margin_size='1cm', dpi=300)[source]

Crop whitespace around images and add a specified margin.

Parameters:
  • image_path (str) – Path to an image file or a directory containing image files.

  • output_path (str, optional) – Path where the cropped images will be saved. If None, overwrites the original files. Defaults to None.

  • margin_size (str, optional) – Margin size to add around cropped images in cm. Defaults to ‘1cm’.

  • dpi (int, optional) – DPI for the output images, used for margin calculation. Defaults to 300.

Example:

from cocopack.figure_ops import crop_whitespace

# Crop whitespace from all images in a folder
crop_whitespace('output_folder', margin_size='1cm')

PDF Conversion#

convert_to_pdf#

cocopack.figure_ops.convert_to_pdf(image_path, output_path=None, dpi=300, **kwargs)[source]

Convert {PNG, JPEG, TIFF} images to high-quality PDF files.

Parameters:
  • image_path (str) – Path to an image file or a directory containing image files.

  • output_path (str, optional) – Path where the PDF files will be saved. If None, uses the same location as the input. Defaults to None.

  • dpi (int, optional) – DPI for the output PDF files. Defaults to 300.

  • **kwargs – Additional keyword arguments. pdf_only (bool): If True, removes the original image files. Defaults to False.

Returns:

None

Example:

from cocopack.figure_ops import convert_to_pdf

# Convert a PNG image to PDF
convert_to_pdf('image.png', dpi=300)

convert_images_to_pdf#

cocopack.figure_ops.convert_images_to_pdf(input_path, dpi=300, **kwargs)[source]

Convert all {PNG, JPEG, TIFF} images in a directory and its subdirectories to PDF files.

Parameters:
  • input_path (str) – Path to the directory containing {PNG, JPEG, TIFF} images.

  • dpi (int, optional) – DPI for the output PDF files. Defaults to 300.

  • **kwargs – Additional keyword arguments passed to convert_to_pdf. pdf_only (bool): If True, removes the original image files. Defaults to False.

Example:

from cocopack.figure_ops import convert_images_to_pdf

# Convert all PNG images in a folder to PDFs
convert_images_to_pdf('output_folder', dpi=300)

Platform Support#

The module provides platform-specific implementations:

  • macOS: Uses AppleScript to interact with Keynote and PowerPoint alike

  • Windows: Uses the COM interface (via pywin32) to control PowerPoint

  • Linux/Other: Uses LibreOffice command-line tools with python-pptx as a fallback