Convert Image to PDF in Python

Place an image on a slide and export it as PDF with a cross-platform Python API

Convert Image to PDF in Python

Aspose.Slides for Python via .NET lets you place a raster image on a slide and export the presentation as a PDF document. Add the image to Presentation.images, create a picture frame with ShapeCollection.add_picture_frame, and call Presentation.save with SaveFormat.PDF.

The example below creates a one-page PDF whose page size matches the source image. This workflow preserves the image’s aspect ratio and does not require Microsoft PowerPoint or a PDF printer.

Convert an Image to PDF Using Python

Create a Presentation, add the source image to its first Slide, and save the presentation as a PDF document.

Python code for converting an image to PDF

with slides.Presentation() as presentation:
    slide = presentation.slides[0]

    with slides.Images.from_file("image.png") as source_image:
        presentation_image = presentation.images.add_image(source_image)

    presentation.slide_size.set_size(
        presentation_image.width,
        presentation_image.height,
        slides.SlideSizeScaleType.DO_NOT_SCALE,
    )

    slide.shapes.add_picture_frame(
        slides.ShapeType.RECTANGLE,
        0,
        0,
        presentation_image.width,
        presentation_image.height,
        presentation_image,
    )
    presentation.save("image.pdf", slides.export.SaveFormat.PDF)

How to Convert an Image to PDF Using the Aspose.Slides Python API

Follow these steps to convert an image to PDF in Python.

  1. Install Aspose.Slides for Python via .NET .

  2. Create a Presentation and access its first Slide.

  3. Load the source image with Images.from_file, add it to Presentation.images, and use its dimensions for the slide and picture frame.

  4. Call Presentation.save with the output file path and SaveFormat.PDF.

Convert Images to Other Supported Formats

You can also convert images and save them in other supported formats.