Repository navigation
Expand file tree
/
Copy pathtest_local.py
More file actions
87 lines (66 loc) · 2.55 KB
/
Copy pathtest_local.py
File metadata and controls
87 lines (66 loc) · 2.55 KB
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
85
86
87
#!/usr/bin/env python3
"""
Local testing script for PDF outline extraction
"""
import sys
import os
import json
from pathlib import Path
# Add src to path
sys.path.insert(0, 'src')
from pdf_processor import PDFProcessor
def test_pdf_processing(pdf_path: str, output_dir: Path = Path("data/output")):
"""Test PDF processing locally or via Docker"""
print(f"Testing PDF: {pdf_path}")
if not os.path.exists(pdf_path):
print(f"Error: PDF file not found: {pdf_path}")
return
try:
processor = PDFProcessor()
result = processor.extract_outline(pdf_path)
print("=" * 50)
print("RESULTS:")
print("=" * 50)
print(f"Title: {result['title']}")
print(f"Number of outline items: {len(result['outline'])}")
print()
for i, item in enumerate(result['outline'][:10]): # Show first 10 items
print(f"{i+1}. [{item['level']}] {item['text']} (Page {item['page']})")
if len(result['outline']) > 10:
print(f"... and {len(result['outline']) - 10} more items")
# Save result to the given output_dir (default = data/output)
output_dir.mkdir(parents=True, exist_ok=True)
output_path = output_dir / f"{Path(pdf_path).stem}_output.json"
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(result, f, indent=2, ensure_ascii=False)
print(f"\nResult saved to: {output_path}")
except Exception as e:
print(f"Error processing PDF: {str(e)}")
import traceback
traceback.print_exc()
def test_all_pdfs():
"""Test all PDFs in the data/input directory"""
input_dir = Path("data/input")
if not input_dir.exists():
print("data/input directory not found. Please create it and add PDF files.")
return
pdf_files = list(input_dir.glob("*.pdf"))
if not pdf_files:
print("No PDF files found in data/input directory.")
print("Please add some PDF files to test.")
return
print(f"Found {len(pdf_files)} PDF files to test:")
for pdf_file in pdf_files:
print(f" - {pdf_file.name}")
print("\nProcessing all PDFs...")
for pdf_file in pdf_files:
print(f"\n{'='*60}")
test_pdf_processing(str(pdf_file))
if __name__ == "__main__":
if len(sys.argv) > 1:
# Test specific PDF file
pdf_path = sys.argv[1]
test_pdf_processing(pdf_path)
else:
# Test all PDFs in data/input directory
test_all_pdfs()