File size: 3,339 Bytes
f996a9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import argparse
import os
import sys
import tempfile
import uuid
from pdf2image import convert_from_path
from ppt_converter import create_pptx_from_images, reconstruct_pptx_from_zip

def main():
    parser = argparse.ArgumentParser(description="Convert PDF/Images with text to editable PPTX, or assemble from Colab ZIP.")
    parser.add_argument("input", nargs="?", help="Input PDF, image, or ZIP file path")
    parser.add_argument("-o", "--output", help="Output PPTX file path", default="output.pptx")
    parser.add_argument("--lang", help="Comma separated languages for OCR, e.g. ch_tra,en", default="ch_tra,en")
    parser.add_argument("--high-accuracy", action="store_true", help="Enable high accuracy OCR mode (slower but catches more small/faint text)")
    parser.add_argument("--zip", help="Directly assemble from a Colab ZIP package path")
    
    args = parser.parse_args()
    
    input_path = args.input
    zip_path = args.zip
    output_path = args.output
    lang_list = args.lang.split(",")
    high_accuracy = args.high_accuracy
    
    # Check if we are running in ZIP reconstruction mode
    if zip_path or (input_path and input_path.lower().endswith(".zip")):
        target_zip = zip_path if zip_path else input_path
        if not os.path.exists(target_zip):
            print(f"Error: ZIP file '{target_zip}' not found.")
            sys.exit(1)
        print(f"Assembling PPTX from ZIP package '{target_zip}'...")
        success = reconstruct_pptx_from_zip(target_zip, output_path)
        if success:
            print(f"Assembly completed! PPTX saved to '{output_path}'")
        else:
            print("Assembly failed.")
            sys.exit(1)
        sys.exit(0)
        
    if not input_path:
        parser.print_help()
        sys.exit(1)
        
    if not os.path.exists(input_path):
        print(f"Error: Input file '{input_path}' not found.")
        sys.exit(1)
        
    ext = os.path.splitext(input_path)[1].lower()
    
    image_paths = []
    temp_dir = tempfile.mkdtemp()
    
    try:
        if ext == ".pdf":
            print(f"Converting PDF '{input_path}' to images...")
            try:
                images = convert_from_path(input_path)
                for i, img in enumerate(images):
                    page_id = str(uuid.uuid4())[:8]
                    temp_img_path = os.path.join(temp_dir, f"temp_page_{page_id}_{i}.png")
                    img.save(temp_img_path, "PNG")
                    image_paths.append(temp_img_path)
            except Exception as e:
                print(f"Error converting PDF. Make sure poppler is installed. Details: {e}")
                sys.exit(1)
        elif ext in [".png", ".jpg", ".jpeg", ".webp"]:
            image_paths = [input_path]
        else:
            print(f"Unsupported file format: {ext}")
            sys.exit(1)
            
        mode_str = "High Accuracy" if high_accuracy else "Standard"
        print(f"Starting OCR and PPTX generation ({mode_str} mode)...")
        create_pptx_from_images(image_paths, output_path, lang_list, high_accuracy=high_accuracy)
        print(f"Done! PPTX saved to '{output_path}'")
        
    finally:
        import shutil
        if temp_dir and os.path.exists(temp_dir):
            shutil.rmtree(temp_dir, ignore_errors=True)

if __name__ == "__main__":
    main()