PPTX-to-PDF-Linux / pptx2pdf_web_en.py
innoai's picture
Upload 2 files
508b543 verified
raw
history blame
3.02 kB
import gradio as gr
import os
import shutil
import subprocess
import tempfile
from zipfile import ZipFile
def convert_pptx_to_pdf(pptx_path, output_dir):
# Call the PptxToPDF console application
pdf_path = os.path.join(output_dir, os.path.splitext(os.path.basename(pptx_path))[0] + ".pdf")
result = subprocess.run([
'./PptxToPDF', # Executable name on Linux
pptx_path
], cwd=output_dir, capture_output=True, text=True)
if result.returncode != 0 or not os.path.exists(pdf_path):
raise RuntimeError(f"Conversion failed: {result.stderr}")
return pdf_path
def make_zip_file(files, zip_path):
with ZipFile(zip_path, 'w') as zipf:
for f in files:
zipf.write(f, os.path.basename(f))
return zip_path
def pptx2pdf_web(pptx_file):
with tempfile.TemporaryDirectory() as tmpdir:
pptx_path = os.path.join(tmpdir, pptx_file.name)
with open(pptx_path, 'wb') as f:
f.write(pptx_file.read())
# Convert
pdf_path = convert_pptx_to_pdf(pptx_path, tmpdir)
# Package
zip_path = os.path.join(tmpdir, 'converted.zip')
make_zip_file([pdf_path], zip_path)
return pdf_path, zip_path
with gr.Blocks(title="PPTX to PDF Converter - Easily Convert PowerPoint to PDF") as demo:
gr.Markdown("# PPTX to PDF Converter") # Added main title here
with gr.Accordion("Application Introduction and Instructions", open=True):
gr.Markdown("""
Welcome to the PPTX to PDF Converter! This tool allows you to easily convert your PowerPoint (PPTX) files into PDF documents.
**Features:**
- **Simple Upload:** Easily upload your PPTX file through the web interface.
- **One-Click Conversion:** Convert your file to PDF with a single click.
- **Direct PDF Download:** Download the converted PDF file directly.
- **Zipped Archive:** Optionally, download a ZIP file containing the converted PDF for convenience.
**How to Use:**
1. **Upload PPTX File:** Click on the "Upload PPTX File" area or drag and drop your `.pptx` file.
2. **Click Convert:** Press the "Convert" button to start the conversion process.
3. **Download Files:** Once the conversion is complete, download links for the PDF file and a ZIP archive (containing the PDF) will appear. Click on your preferred option to download.
""")
gr.Markdown("---")
with gr.Row():
with gr.Column():
pptx_input = gr.File(label="Upload PPTX File", file_types=[".pptx"])
convert_btn = gr.Button("Convert")
with gr.Column():
pdf_output = gr.File(label="Download PDF File")
zip_output = gr.File(label="Download ZIP Archive")
convert_btn.click(pptx2pdf_web, inputs=[pptx_input], outputs=[pdf_output, zip_output])
demo.queue(default_concurrency_limit=10, max_size=50)
demo.launch(show_api=False)