from_pdf_to_ppt / app.py
rongo1
input error
79dcd5a
raw
history blame
1.23 kB
import gradio as gr
from pptx import Presentation
from fpdf import FPDF
import tempfile
import os
from PIL import Image
def pptx_to_pdf(pptx_file):
# Create a temporary directory to store intermediate images
with tempfile.TemporaryDirectory() as tmpdirname:
pdf = FPDF()
# Load the PowerPoint file
presentation = Presentation(pptx_file.name)
for i, slide in enumerate(presentation.slides):
# Save each slide as an image
img_path = os.path.join(tmpdirname, f"slide_{i}.png")
slide.shapes[0].image.save(img_path)
# Add the image to the PDF
pdf.add_page()
pdf.image(img_path, 0, 0, 210, 297) # A4 size (210mm x 297mm)
# Save the PDF to a file
pdf_output_path = os.path.join(tmpdirname, "output.pdf")
pdf.output(pdf_output_path)
return pdf_output_path
# Create a Gradio interface
iface = gr.Interface(
fn=pptx_to_pdf,
inputs=gr.File(file_types=[".pptx"]),
outputs=gr.File(file_types=[".pdf"]),
title="PPTX to PDF Converter",
description="Upload a PowerPoint file to convert it to a PDF without watermarks."
)
# Launch the app
iface.launch()