File size: 2,174 Bytes
d332c6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import subprocess
import gradio as gr

# 程序根目录
BASE_DIR = os.path.dirname(__file__)
# 可执行文件路径:app.py 与 PptxToJpgZip 同目录
BINARY_PATH = os.path.join(BASE_DIR, "PptxToJpgZip")

# 启动时确保二进制有执行权限(在 Hugging Face Spaces 里无法手动 chmod)
try:
    os.chmod(BINARY_PATH, 0o755)
except Exception as e:
    # 如果权限设置失败,也无需中断,只要后续能调用即可
    print(f"⚠️ 警告:设置执行权限失败:{e}")

def convert_pptx_to_zip(pptx_file):
    """
    调用外部 PptxToJpgZip,可执行文件将上传的 PPTX 转为 JPG 并打包 ZIP。
    pptx_file.name 是 Gradio 上传后的临时文件路径。
    """
    src_path = pptx_file.name  # Gradio 会在后台保存上传文件,并提供 .name 属性

    # 调用外部程序:PptxToJpgZip <输入文件.pptx>
    proc = subprocess.run(
        [BINARY_PATH, src_path],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    if proc.returncode != 0:
        stderr = proc.stderr.decode("utf-8", errors="ignore")
        raise RuntimeError(f"转换失败:\n{stderr}")

    # 可执行程序会在同目录生成 <原文件名>_images.zip
    zip_path = os.path.splitext(src_path)[0] + "_images.zip"
    if not os.path.isfile(zip_path):
        raise FileNotFoundError(f"转换完成,但未找到输出 ZIP:{zip_path}")

    return zip_path  # Gradio 会自动为此路径生成下载链接

# —— 构建 Gradio 界面 ——
with gr.Blocks(title="PPTX→JPG→ZIP 转换器") as demo:
    gr.Markdown("""
# PPTX→JPG→ZIP  
上传一个 `.pptx` 文件,后台调用 `PptxToJpgZip` 可执行程序,  
将每页幻灯片导出为高质量 JPG,并打包成 ZIP,最后提供下载。
""")
    with gr.Row():
        pptx_in = gr.File(label="上传 PPTX (.pptx)", file_types=[".pptx"])
        btn = gr.Button("开始转换")
    zip_out = gr.File(label="下载 ZIP 文件")

    btn.click(fn=convert_pptx_to_zip, inputs=pptx_in, outputs=zip_out)

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )