from_pdf_to_ppt / app copy.py
rongo1
another approach
53fe756
raw
history blame
2.17 kB
import os
import subprocess
import gradio as gr
from pptx import Presentation
from pptx.util import Inches
from pdf2image import convert_from_path
# Run the setup script to install unoconv and libreoffice
subprocess.run(['bash', 'setup.sh'], check=True)
# Function to convert PowerPoint to PDF using unoconv
def pptx_to_pdf(input_pptx, output_pdf):
subprocess.run(['unoconv', '-f', 'pdf', '-o', output_pdf, input_pptx], check=True)
# Function to convert PDF to PowerPoint
def pdf_to_pptx(input_pdf, output_pptx):
# Specify a directory to save images
output_dir = "pdf_images"
os.makedirs(output_dir, exist_ok=True)
# Convert PDF to images and save in the specified directory
images = convert_from_path(input_pdf, output_folder=output_dir)
# Create a new PowerPoint presentation
presentation = Presentation()
for i, img in enumerate(images):
img_path = os.path.join(output_dir, f"page_{i+1}.png")
img.save(img_path, 'PNG')
slide_layout = presentation.slide_layouts[5] # Use a blank layout
slide = presentation.slides.add_slide(slide_layout)
left = top = Inches(0)
pic = slide.shapes.add_picture(img_path, left, top, width=Inches(10), height=Inches(7.5))
presentation.save(output_pptx)
def convert_pptx_to_pdf(pptx_file):
output_pdf = "output.pdf"
pptx_to_pdf(pptx_file.name, output_pdf)
return output_pdf
def convert_pdf_to_pptx(pdf_file):
output_pptx = "output_converted.pptx"
pdf_to_pptx(pdf_file.name, output_pptx)
return output_pptx
pptx_to_pdf_interface = gr.Interface(
fn=convert_pptx_to_pdf,
inputs=gr.File(label="Upload PPTX File", file_count="single"),
outputs=gr.File(label="Download PDF File"),
title="Convert PPTX to PDF"
)
pdf_to_pptx_interface = gr.Interface(
fn=convert_pdf_to_pptx,
inputs=gr.File(label="Upload PDF File", file_count="single"),
outputs=gr.File(label="Download PPTX File"),
title="Convert PDF to PPTX"
)
app = gr.TabbedInterface([pptx_to_pdf_interface, pdf_to_pptx_interface], ["PPTX to PDF", "PDF to PPTX"])
if __name__ == "__main__":
app.launch()