Spaces:
Sleeping
Sleeping
Upload app_with_import.py
Browse files- app_with_import.py +73 -0
app_with_import.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
import os, sys, shutil, subprocess, tempfile, uuid
|
4 |
+
from pathlib import Path
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# 导入字体安装函数
|
8 |
+
from install_fonts import install_fonts_from_repository
|
9 |
+
|
10 |
+
BASE_DIR = Path(__file__).parent.resolve()
|
11 |
+
BINARY_PATH = BASE_DIR / "PptxToJpgZip"
|
12 |
+
os.chmod(BINARY_PATH, 0o755) # 忽略异常
|
13 |
+
|
14 |
+
def _safe_name() -> str:
|
15 |
+
"""生成仅 ASCII 的临时文件名"""
|
16 |
+
return f"input_{uuid.uuid4().hex}.pptx"
|
17 |
+
|
18 |
+
def convert_pptx_to_zip(pptx_file) -> str:
|
19 |
+
"""调用 PptxToJpgZip,把 PPTX 转成图片 ZIP;若仅有文件夹则自动 zip。"""
|
20 |
+
tmpdir = Path(tempfile.mkdtemp())
|
21 |
+
src = tmpdir / _safe_name()
|
22 |
+
shutil.copy(pptx_file.name, src)
|
23 |
+
|
24 |
+
# 调用外部可执行文件
|
25 |
+
proc = subprocess.run(
|
26 |
+
[str(BINARY_PATH), str(src)],
|
27 |
+
cwd=tmpdir,
|
28 |
+
stdout=subprocess.PIPE,
|
29 |
+
stderr=subprocess.PIPE,
|
30 |
+
)
|
31 |
+
if proc.returncode != 0:
|
32 |
+
raise RuntimeError(
|
33 |
+
f"PptxToJpgZip 失败,退出码 {proc.returncode}\n"
|
34 |
+
f"stderr:\n{proc.stderr.decode(errors='ignore')}"
|
35 |
+
)
|
36 |
+
|
37 |
+
# ---- ① 优先找 *_images.zip ----
|
38 |
+
zips = list(tmpdir.glob("*_images.zip"))
|
39 |
+
if zips:
|
40 |
+
return str(zips[0])
|
41 |
+
|
42 |
+
# ---- ② 若无 ZIP,再找 *_images 目录,自动打包 ----
|
43 |
+
img_dirs = [p for p in tmpdir.glob("*_images") if p.is_dir()]
|
44 |
+
if img_dirs:
|
45 |
+
folder = img_dirs[0]
|
46 |
+
zip_path = folder.with_suffix(".zip") # <name>_images.zip
|
47 |
+
shutil.make_archive(zip_path.with_suffix(""), "zip", folder)
|
48 |
+
return str(zip_path)
|
49 |
+
|
50 |
+
# ---- ③ 仍没找到:列出 tmpdir 内容方便调试 ----
|
51 |
+
contents = "\n".join([" • " + p.name for p in tmpdir.iterdir()])
|
52 |
+
raise FileNotFoundError(
|
53 |
+
"未找到输出 ZIP,也未发现 *_images 文件夹。\n"
|
54 |
+
f"临时目录内容:\n{contents}"
|
55 |
+
)
|
56 |
+
|
57 |
+
# ----------------- Gradio UI 保持不变 ----------------- #
|
58 |
+
with gr.Blocks(title="PPTX→JPG→ZIP 转换器") as demo:
|
59 |
+
gr.Markdown(
|
60 |
+
"""
|
61 |
+
# PPTX→JPG→ZIP
|
62 |
+
上传 `.pptx`,后台调用 **PptxToJpgZip**,将每页导出为 JPG 并打包 ZIP。"""
|
63 |
+
)
|
64 |
+
with gr.Row():
|
65 |
+
pptx_in = gr.File(label="上传 PPTX (.pptx)", file_types=[".pptx"])
|
66 |
+
btn = gr.Button("开始转换")
|
67 |
+
zip_out = gr.File(label="下载 ZIP 文件")
|
68 |
+
btn.click(fn=convert_pptx_to_zip, inputs=pptx_in, outputs=zip_out)
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
# 启动应用前安装字体(通过引用方式调用)
|
72 |
+
install_fonts_from_repository()
|
73 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|