File size: 1,664 Bytes
f434b4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import fitz

input_dir = Path("/Users/productivity/Downloads/Approaching_Religion/AllPDFS")
output_dir = Path("/Users/productivity/Downloads/Approaching_Religion/PNGs")

DPI = 300

output_dir.mkdir(parents=True, exist_ok=True)

pdf_files = sorted(
    p for p in input_dir.glob("*.pdf")
    if p.is_file()
)

total_pages = 0
failed_files = []

print(f"Found {len(pdf_files)} PDF files.")

for file_number, pdf_path in enumerate(pdf_files, start=1):
    try:
        with fitz.open(pdf_path) as pdf:
            pdf_output = output_dir / pdf_path.stem
            pdf_output.mkdir(parents=True, exist_ok=True)

            page_count = len(pdf)
            print(
                f"[{file_number}/{len(pdf_files)}] "
                f"{pdf_path.name}: {page_count} pages"
            )

            for page_number, page in enumerate(pdf, start=1):
                pix = page.get_pixmap(
                    dpi=DPI,
                    colorspace=fitz.csRGB,
                    alpha=False
                )

                output_file = (
                    pdf_output /
                    f"{pdf_path.stem}_page_{page_number:04d}.png"
                )
                pix.save(output_file)

            total_pages += page_count

    except Exception as exc:
        failed_files.append((pdf_path.name, str(exc)))
        print(f"ERROR: {pdf_path.name}: {exc}")

print("\nConversion complete.")
print(f"Successfully converted pages: {total_pages}")
print(f"Failed PDFs: {len(failed_files)}")

if failed_files:
    print("\nFiles requiring attention:")
    for filename, error in failed_files:
        print(f"- {filename}: {error}")