2
This code is a Python function that takes a PowerPoint file (ppt_file) and converts it into a PDF file using several third-party libraries and Python built-in modules. Here is what the code does step-by-step:
  1. First, it creates a temporary file with a .pptx extension using the tempfile module and saves the uploaded ppt_file to that temporary file.
  2. Then, it creates another temporary file with a .pdf extension using the tempfile module.
  3. The python-pptx library is used to read the PowerPoint file from the temporary file created in step 1.
  4. The reportlab library is used to create a new PDF file, which will be the output file.
  5. For each slide in the PowerPoint file, the code calculates the dimensions of the slide and sets the PDF canvas size accordingly using landscape() or portrait() from reportlab.
  6. The code then extracts the slide content as HTML and draws it on the PDF canvas using reportlab functions.
  7. If the slide contains an image, the code uses the Pillow library to convert the image to a PNG file and draws it on the PDF canvas.
  8. After all slides are processed, the code saves the PDF file to the temporary file created in step 2.
  9. The temporary PDF file is then read into a BytesIO object and returned as a Flask response using send_file.
  10. Finally, the temporary files are deleted using os.unlink.

The function:


import io
import os
import tempfile
from pptx import Presentation
from reportlab.lib.pagesizes import landscape, portrait
from reportlab.pdfgen import canvas
from flask import send_file
import imgkit
from PIL import Image

def ppt_to_pdf(ppt_file):
    # Save the uploaded file to a temporary file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as temp_ppt_file:
        ppt_file.save(temp_ppt_file.name)
        temp_ppt_file.close()

    # Create a temporary PDF file
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf_file:
        temp_pdf_file.close()

    # Read the PowerPoint file
    prs = Presentation(temp_ppt_file.name)

    # Create a PDF file using reportlab
    pdf = canvas.Canvas(temp_pdf_file.name)

    for slide in prs.slides:
        slide_width = prs.slide_width.pt
        slide_height = prs.slide_height.pt

        # Set the page size based on the slide dimensions
        if slide_width > slide_height:
            pdf.setPageSize(landscape((slide_width, slide_height)))
        else:
            pdf.setPageSize(portrait((slide_width, slide_height)))

        # Extract slide content as HTML
        slide_html = "<html><body>"
        for shape in slide.shapes:
            if shape.has_text_frame:
                slide_html += f"<p>{shape.text}</p>"
            elif shape.shape_type == 13:  # Picture
                with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_image_file:
                    temp_image_file.write(shape.image.blob)
                    temp_image_file.close()

                img = Image.open(temp_image_file.name)

                # Calculate the position and size of the image on the slide
                left = shape.left.pt
                top = shape.top.pt
                width = shape.width.pt
                height = shape.height.pt

                # Draw the image on the PDF canvas
                pdf.drawImage(temp_image_file.name, left,
                              top, width=width, height=height)

                # Clean up the temporary image file
                os.unlink(temp_image_file.name)

        slide_html += "</body></html>"

        # Convert slide content to an image using imgkit
        slide_image = imgkit.from_string(
            slide_html, False, {"width": int(slide_width), "height": int(slide_height)})

        # Save the slide image to a temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_image_file:
            temp_image_file.write(slide_image)
            temp_image_file.close()

        # Draw the slide image on the PDF canvas
        pdf.drawImage(temp_image_file.name, 0, 0,
                      width=slide_width, height=slide_height)

        # Clean up the temporary image file
        os.unlink(temp_image_file.name)

        # Move to the next page
        pdf.showPage()

    pdf.save()

    # Read the temporary PDF file into a BytesIO object
    final_pdf = io.BytesIO()
    with open(temp_pdf_file.name, 'rb') as f:
        final_pdf.write(f.read())
    final_pdf.seek(0)

    # Clean up the temporary files
    os.unlink(temp_ppt_file.name)
    os.unlink(temp_pdf_file.name)

    # Return the PDF file as a Flask response
    return send_file(final_pdf, download_name='output.pdf',
                     as_attachment=True, mimetype='application/pdf')

i'm expecting the function to return a pdf version of the ppt file.

0 Answers0