Convert PowerPoint to PDF in Python

Overview

Converting PowerPoint documents into PDF format offers several advantages, including ensuring compatibility across different devices and preserving the layout and formatting of your presentation. This article shows you how to convert presentations to PDF documents, use various options to control image quality, include hidden slides, password protect PDF documents, detect font substitutions, select slides for conversion, and apply compliance standards to output documents.

PowerPoint to PDF Conversions

Using Aspose.Slides, you can convert presentations in these formats to PDF:

  • PPT
  • PPTX
  • ODP

To convert a presentation to PDF in Python, you simply have to pass the file name as an argument in the Presentation class and then save the presentation as a PDF using a Save method. The Presentation class exposes the Save method that is typically used to convert a presentation to PDF.

Aspose.Slides allows you to convert:

  • an entire presentation to PDF
  • specific slides in a presentation to PDF
  • a presentation

Aspose.Slides exports presentations to PDF in a way that makes the contents of the resulting PDFs very similar to those in the original presentations. These known elements and attributes are often rendered properly in presentation to PDF conversions:

  • images
  • text boxes and other shapes
  • texts and their formatting
  • paragraphs and their formatting
  • hyperlinks
  • headers and footers
  • bullets
  • tables

Convert PowerPoint to PDF

The standard PowerPoint PDF conversion operation is executed using default options. In this case, Aspose.Slides tries to convert the provided presentation to PDF using optimal settings at the maximum quality levels. This Python code shows you how to convert a PowerPoint to PDF:

Steps: PowerPoint to PDF Conversions in Python

The following sample code explains these conversions using Python via .NET

Code Steps:

  • Create instance of Presentation class and provide it the PowerPoint file.
    • .ppt extension to load PPT file inside Presentation class.
    • .pptx extension to load PPTX file inside Presentation class.
    • .odp extension to load ODP file inside Presentation class.
    • .pps extension to load PPS file inside Presentation class.
  • Save the Presentation to PDF format by calling Save method and using SaveFormat.PDF enumeration.
import aspose.slides as slides

# Instantiates a Presentation class that represents a PowerPoint file
presentation = slides.Presentation("PowerPoint.ppt")

# Saves the presentation as a PDF
presentation.save("PPT-to-PDF.pdf", slides.export.SaveFormat.PDF)

Convert PowerPoint to PDF with Options

Aspose.Slides provides custom options—properties under the PdfOptions class—that allow you to customize the PDF (resulting from the conversion process), lock the PDF with a password, or even specify how the conversion process should go.

Convert PowerPoint to PDF with Custom Options

Using custom conversion options, you can set your preferred quality setting for raster images, specify how metafiles should be handled, set a compression level for texts, set DPI for images, etc.

The code example below demonstrates an operation in which a PowerPoint presentation is converted to PDF with several custom options:

import aspose.slides as slides

# Instantiates the PdfOptions class
pdf_options = slides.export.PdfOptions()

# Sets the quality for JPG images
pdf_options.jpeg_quality = 90

# Sets DPI for images
pdf_options.sufficient_resolution = 300

# Sets the behavior for metafiles
pdf_options.save_metafiles_as_png = True

# Sets the text compression level for textual content
pdf_options.text_compression = slides.export.PdfTextCompression.FLATE

# Defines the PDF compliance mode
pdf_options.compliance = slides.export.PdfCompliance.PDF15

# Instantiates the Presentation class that represents a PowerPoint document
with slides.Presentation("PowerPoint.pptx") as presentation:
    # Saves the presentation as a PDF document
    presentation.save("PowerPoint-to-PDF.pdf", slides.export.SaveFormat.PDF, pdf_options)

Convert PowerPoint to PDF with Hidden Slides

If a presentation contains hidden slides, you can use a custom option—the show_hidden_slides property from the PdfOptions class—to instruct Aspose.Slides to include the hidden slides as pages in the resulting PDF.

This Python code shows you how to convert a PowerPoint presentation to PDF with hidden slides included:

import aspose.slides as slides

# Instantiates a Presentation class that represents a PowerPoint file
presentation = slides.Presentation("PowerPoint.pptx")

# Instantiates the the PdfOptions class
pdfOptions = slides.export.PdfOptions()

# Adds hidden slides
pdfOptions.show_hidden_slides = True

# Saves the presentation as a PDF
presentation.save("PowerPoint-to-PDF.pdf", slides.export.SaveFormat.PDF, pdfOptions)

Convert PowerPoint to Password Protected PDF

This Python code shows you how to convert a PowerPoint to a password-protected PDF (using protection parameters from the PdfOptions class):

import aspose.slides as slides

# Instantiates a Presentation object that represents a PowerPoint file
presentation = slides.Presentation("PowerPoint.pptx")

# Instantiates the PdfOptions class
pdfOptions = slides.export.PdfOptions()

# Sets PDF password and access permissions
pdfOptions.password = "password"
pdfOptions.access_permissions = slides.export.PdfAccessPermissions.PRINT_DOCUMENT | slides.export.PdfAccessPermissions.HIGH_QUALITY_PRINT

# Saves the presentation as a PDF
presentation.save("PPTX-to-PDF.pdf", slides.export.SaveFormat.PDF, pdfOptions)

Detect Font Substitutions**

Aspose.Slides provides the warning_callback property under the SaveOptions class to allow you to detect font substitutions in a presentation to PDF conversion process.

This Python code shows you how to detect font substitutions:

[TODO[SLIDESPYNET-91]: callbacks are not supported for now]

Convert Selected Slides in PowerPoint to PDF

This Python code shows you how to convert specific slides in a PowerPoint presentation to PDF:

import aspose.slides as slides

# Instantiates a Presentation object that represents a PowerPoint file
presentation = slides.Presentation("PowerPoint.pptx")

# Sets an array of slides positions
slides_array = [ 1, 3 ]

# Saves the presentation as a PDF
presentation.save("PPTX-to-PDF.pdf", slides_array, slides.export.SaveFormat.PDF)

Convert PowerPoint to PDF with Custom Slide Size

This Python code shows you how to convert a PowerPoint when its slide size is specified to a PDF:

import aspose.slides as slides

# Instantiates a Presentation object that represents a PowerPoint file 
presentation = slides.Presentation("SelectedSlides.pptx")
auxPresentation = slides.Presentation()

slide = presentation.slides[0]

auxPresentation.slides.insert_clone(0, slide)

# Sets the slide type and size 
auxPresentation.slide_size.set_size(612, 792, slides.SlideSizeScaleType.ENSURE_FIT)

pdfOptions = slides.export.PdfOptions()
pdfOptions.notes_comments_layouting.notes_position = slides.export.NotesPositions.BOTTOM_FULL

auxPresentation.save("PDFnotes_out.pdf", slides.export.SaveFormat.PDF, pdfOptions)

Convert PowerPoint to PDF in Notes Slide View

This Python code shows you how to convert a PowerPoint to PDF notes:

import aspose.slides as slides

# Instantiates a Presentation class that represents a PowerPoint file
presentation = slides.Presentation("NotesFile.pptx")

pdfOptions = slides.export.PdfOptions()
pdfOptions.notes_comments_layouting.notes_position = slides.export.NotesPositions.BOTTOM_FULL

# Saves the presentation to PDF notes
presentation.Save("Pdf_Notes_out.tiff", slides.export.SaveFormat.PDF, pdfOptions)

Accessibility and Compliance Standards for PDF

Aspose.Slides allows you to use a conversion procedure that complies with Web Content Accessibility Guidelines (WCAG). You can export a PowerPoint document to PDF using any of these compliance standards: PDF/A1a, PDF/A1b, and PDF/UA.

This Python code demonstrates a PowerPoint to PDF conversion operation in which multiple PDFs based on different compliance standards are obtained:

import aspose.slides as slides

pres = slides.Presentation("pres.pptx")

options = slides.export.PdfOptions()

options.compliance = slides.export.PdfCompliance.PDF_A1A
pres.save("pres-a1a-compliance.pdf", slides.export.SaveFormat.PDF, options)

options.compliance = slides.export.PdfCompliance.PDF_A1B
pres.save("pres-a1b-compliance.pdf", slides.export.SaveFormat.PDF, options)

options.compliance = slides.export.PdfCompliance.PDF_UA
pres.save("pres-ua-compliance.pdf", slides.export.SaveFormat.PDF, options)