Import Presentations from PDF or HTML in Python

Introduction

Aspose.Slides for Python via .NET can turn PDF pages or HTML content into PowerPoint slides without Microsoft PowerPoint. The SlideCollection class provides add_from_pdf and add_from_html for appending imported content to a presentation.

For more control over HTML placement, SlideCollection.insert_from_html can insert generated slides at a collection index or begin filling available space on an existing slide. Long HTML is paginated across additional slides automatically, the source can be supplied as a string or stream, and external assets can be loaded through IExternalResourceResolver with a base URI. The returned ISlide array identifies the affected and newly created slides.

Import from PDF

To convert a PDF document to a PowerPoint presentation, import its content into the slide collection and save the result as a PPTX file.

pdf-to-powerpoint

  1. Create a new Presentation object.
  2. Call add_from_pdf with the path to the PDF file.
  3. Call save with SaveFormat.PPTX to write the presentation to a PPTX file.

The following Python example imports a PDF document and saves the generated slides as a PowerPoint presentation:

import aspose.slides as slides

with slides.Presentation() as presentation:
    presentation.slides.add_from_pdf("document.pdf")
    presentation.save("presentation.pptx", slides.export.SaveFormat.PPTX)

The add_from_pdf method returns the slides it adds, which is useful when you need to process only the imported slides.

Import from HTML

Aspose.Slides can also create slides from an HTML document. The source can be supplied as HTML text or a stream. The following steps use a file stream:

  1. Create a new Presentation object.
  2. Open the HTML file for reading and pass the stream to add_from_html.
  3. Call save with SaveFormat.PPTX to write the result to a PPTX file.

The following Python example imports an HTML document and saves the generated slides as a PowerPoint presentation:

import aspose.slides as slides

with slides.Presentation() as presentation:
    with open("page.html", "rb") as html_stream:
        presentation.slides.add_from_html(html_stream)

    presentation.save("presentation.pptx", slides.export.SaveFormat.PPTX)

Insert HTML Content

Use SlideCollection.insert_from_html when HTML-generated slides must be placed at a specific position instead of appended. The index is zero-based and identifies the position at which the import starts. The method is also available through ISlideCollection.

The use_slide_with_index_as_start argument controls how the importer uses that position:

  • When it is False, the importer creates new slides at the specified index and shifts the slides that follow them.
  • When it is True, the importer starts placing content in the available space on the existing slide at that index. If the HTML does not fit, Aspose.Slides paginates it automatically and inserts additional slides immediately after the starting slide.

SlideCollection.insert_from_html returns an array of ISlide objects. When insertion starts on new slides, every returned item is newly created. When an existing slide is used as the start, the array includes that affected slide followed by any new overflow slides. You can inspect this array instead of calculating the affected range from the presentation’s slide count.

Insert HTML as New Slides

The following example supplies HTML as a string and inserts the generated slides at collection index 1. Passing False leaves the existing slides unchanged except for shifting them to make room.

import aspose.slides as slides

with slides.Presentation() as presentation:
    layout_slide = presentation.layout_slides[0]
    presentation.slides.add_empty_slide(layout_slide)
    presentation.slides.add_empty_slide(layout_slide)

    insert_index = 1
    html = "<html><body><h1>Quarterly update</h1><p>This content is inserted before the slide that was at index 1.</p></body></html>"
    inserted_slides = presentation.slides.insert_from_html(insert_index, html, False)

    for slide in inserted_slides:
        print(f"Inserted slide index: {presentation.slides.index_of(slide)}")

    presentation.save("presentation-with-inserted-html.pptx", slides.export.SaveFormat.PPTX)

Start on an Existing Slide

The next example supplies the HTML through a stream. It keeps a header shape on the existing template slide, starts importing below the occupied area, and lets the long body continue onto new slides.

The HTML also contains a relative image URL. An IExternalResourceResolver obtains the resource, while the base URI tells the importer how to resolve images/logo.png. In this example, that file is expected at html-assets/images/logo.png.

from io import BytesIO
from pathlib import Path

import aspose.slides as slides

with slides.Presentation() as presentation:
    template_slide = presentation.slides[0]
    header = template_slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 680, 60)
    header.text_frame.text = "Product roadmap"

    html_parts = ["<html><body><img src='images/logo.png' width='120' height='60'><h2>Roadmap details</h2>"]
    for item_index in range(1, 61):
        html_parts.append(f"<p style='font-size:24pt'>Roadmap item {item_index}: detailed implementation notes.</p>")
    html_parts.append("</body></html>")

    html = "".join(html_parts)
    html_stream = BytesIO(html.encode("utf-8"))
    resolver = slides.importing.ExternalResourceResolver()
    base_uri = Path("html-assets").resolve().as_uri() + "/"
    affected_slides = presentation.slides.insert_from_html(0, html_stream, resolver, base_uri, True)

    for slide in affected_slides:
        print(f"Affected slide index: {presentation.slides.index_of(slide)}")

    presentation.save("presentation-with-html-overflow.pptx", slides.export.SaveFormat.PPTX)

FAQ

Can Aspose.Slides detect tables when importing a PDF?

Yes. Create a PdfImportOptions object, set its detect_tables property to True, and pass the options to add_from_pdf. The quality of table recognition depends on the structure and complexity of the source PDF.