Spaces:
Sleeping
Sleeping
Upload pptx2pdf_web_en.py
Browse files- pptx2pdf_web_en.py +22 -10
pptx2pdf_web_en.py
CHANGED
@@ -6,19 +6,31 @@ import sys
|
|
6 |
import tempfile
|
7 |
from zipfile import ZipFile
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def convert_pptx_to_pdf(pptx_path, output_dir):
|
10 |
-
#
|
11 |
-
if
|
12 |
-
|
13 |
-
|
14 |
-
exe_path = os.path.join('.', 'PptxToPDF')
|
15 |
-
# Ensure the executable has execute permissions (Linux/Mac)
|
16 |
-
if not os.access(exe_path, os.X_OK):
|
17 |
-
import stat
|
18 |
-
os.chmod(exe_path, os.stat(exe_path).st_mode | stat.S_IEXEC)
|
19 |
pdf_path = os.path.join(output_dir, os.path.splitext(os.path.basename(pptx_path))[0] + ".pdf")
|
20 |
result = subprocess.run([
|
21 |
-
|
22 |
pptx_path
|
23 |
], cwd=output_dir, capture_output=True, text=True)
|
24 |
if result.returncode != 0 or not os.path.exists(pdf_path):
|
|
|
6 |
import tempfile
|
7 |
from zipfile import ZipFile
|
8 |
|
9 |
+
# 程序根目录
|
10 |
+
BASE_DIR = os.path.dirname(__file__)
|
11 |
+
|
12 |
+
# 可执行文件路径:根据操作系统确定
|
13 |
+
if sys.platform.startswith('win'):
|
14 |
+
BINARY_PATH = os.path.join(BASE_DIR, "PptxToPDF.exe")
|
15 |
+
else:
|
16 |
+
BINARY_PATH = os.path.join(BASE_DIR, "PptxToPDF")
|
17 |
+
|
18 |
+
# 启动时确保二进制有执行权限(在 Hugging Face Spaces 里无法手动 chmod)
|
19 |
+
try:
|
20 |
+
if not sys.platform.startswith('win'):
|
21 |
+
os.chmod(BINARY_PATH, 0o755)
|
22 |
+
except Exception as e:
|
23 |
+
# 如果权限设置失败,也无需中断,只要后续能调用即可
|
24 |
+
print(f"⚠️ 警告:设置执行权限失败:{e}")
|
25 |
+
|
26 |
def convert_pptx_to_pdf(pptx_path, output_dir):
|
27 |
+
# 检查可执行文件是否存在
|
28 |
+
if not os.path.exists(BINARY_PATH):
|
29 |
+
raise FileNotFoundError(f"PptxToPDF 可执行文件未找到: {BINARY_PATH}")
|
30 |
+
|
|
|
|
|
|
|
|
|
|
|
31 |
pdf_path = os.path.join(output_dir, os.path.splitext(os.path.basename(pptx_path))[0] + ".pdf")
|
32 |
result = subprocess.run([
|
33 |
+
BINARY_PATH,
|
34 |
pptx_path
|
35 |
], cwd=output_dir, capture_output=True, text=True)
|
36 |
if result.returncode != 0 or not os.path.exists(pdf_path):
|