Spaces:
Sleeping
Sleeping
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import img2pdf
|
| 4 |
+
|
| 5 |
+
# --- 全局配置 ---
|
| 6 |
+
BASE_DIR = "download_cache"
|
| 7 |
+
PDF_FINAL_DIR = os.path.join(BASE_DIR, "final_pdfs")
|
| 8 |
+
|
| 9 |
+
# 确保基础目录存在
|
| 10 |
+
os.makedirs(PDF_FINAL_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# --- 工具函数 ---
|
| 13 |
+
|
| 14 |
+
def natural_sort_key(s):
|
| 15 |
+
"""自然排序:让 1.jpg, 2.jpg, 10.jpg 正确排序"""
|
| 16 |
+
return [int(text) if text.isdigit() else text.lower()
|
| 17 |
+
for text in re.split(r'(\d+)', s)]
|
| 18 |
+
|
| 19 |
+
def sanitize_filename(name):
|
| 20 |
+
"""清理文件名:去除非法字符"""
|
| 21 |
+
return re.sub(r'[\\/*?:"<>|]', '_', name)
|
| 22 |
+
|
| 23 |
+
def manual_merge_pdf(album_dir, output_pdf_path):
|
| 24 |
+
"""核心功能:将目录下的图片合并为 PDF"""
|
| 25 |
+
image_paths = []
|
| 26 |
+
for root, _, files in os.walk(album_dir):
|
| 27 |
+
for file in files:
|
| 28 |
+
if file.lower().endswith(('.jpg', '.jpeg', '.png', '.webp')):
|
| 29 |
+
image_paths.append(os.path.join(root, file))
|
| 30 |
+
|
| 31 |
+
if not image_paths:
|
| 32 |
+
raise Exception("目录中未找到任何图片")
|
| 33 |
+
|
| 34 |
+
# 按文件名自然排序
|
| 35 |
+
image_paths.sort(key=natural_sort_key)
|
| 36 |
+
|
| 37 |
+
# 转换为 PDF
|
| 38 |
+
with open(output_pdf_path, "wb") as f:
|
| 39 |
+
f.write(img2pdf.convert(image_paths))
|