Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import shutil | |
| import subprocess | |
| import sys | |
| import tempfile | |
| from zipfile import ZipFile | |
| # 程序根目录 | |
| BASE_DIR = os.path.dirname(__file__) | |
| # 可执行文件路径:根据操作系统确定 | |
| if sys.platform.startswith('win'): | |
| BINARY_PATH = os.path.join(BASE_DIR, "PptxToPDF.exe") | |
| else: | |
| BINARY_PATH = os.path.join(BASE_DIR, "PptxToPDF") | |
| # 启动时确保二进制有执行权限(在 Hugging Face Spaces 里无法手动 chmod) | |
| try: | |
| if not sys.platform.startswith('win'): | |
| os.chmod(BINARY_PATH, 0o755) | |
| except Exception as e: | |
| # 如果权限设置失败,也无需中断,只要后续能调用即可 | |
| print(f"⚠️ 警告:设置执行权限失败:{e}") | |
| def convert_pptx_to_pdf(pptx_path, output_dir): | |
| # 检查可执行文件是否存在 | |
| if not os.path.exists(BINARY_PATH): | |
| raise FileNotFoundError(f"PptxToPDF 可执行文件未找到: {BINARY_PATH}") | |
| pdf_path = os.path.join(output_dir, os.path.splitext(os.path.basename(pptx_path))[0] + ".pdf") | |
| result = subprocess.run([ | |
| BINARY_PATH, | |
| 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): # pptx_file is the Gradio file object | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| gradio_temp_file_path = pptx_file.name # Path to the file uploaded by Gradio | |
| # Create a path for our copy of the PPTX inside our own tmpdir | |
| # Use the original basename of the uploaded file | |
| target_pptx_filename = os.path.basename(gradio_temp_file_path) | |
| our_copy_of_pptx_path = os.path.join(tmpdir, target_pptx_filename) | |
| # Copy the uploaded PPTX from Gradio's temp location to our temp location | |
| shutil.copyfile(gradio_temp_file_path, our_copy_of_pptx_path) | |
| # Now, our_copy_of_pptx_path is the path to the PPTX file within tmpdir. | |
| # convert_pptx_to_pdf will create the PDF also within tmpdir. | |
| pdf_path = convert_pptx_to_pdf(our_copy_of_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) | |