PDF file parsing, supports progress callback
(file_path, save_path, progress_callback=None)
| 49 | return save_file_path |
| 50 | |
| 51 | def pdf_parse(file_path, save_path, progress_callback=None): |
| 52 | """PDF file parsing, supports progress callback""" |
| 53 | import fitz |
| 54 | |
| 55 | # Open PDF file |
| 56 | pdf_document = fitz.open(file_path) |
| 57 | total_pages = pdf_document.page_count |
| 58 | |
| 59 | # Check if it's a scanned image PDF |
| 60 | is_scanned = True |
| 61 | for page in pdf_document: |
| 62 | if page.get_text().strip(): |
| 63 | is_scanned = False |
| 64 | break |
| 65 | |
| 66 | results = [] |
| 67 | if is_scanned: |
| 68 | for page_num in range(total_pages): |
| 69 | |
| 70 | page = pdf_document[page_num] |
| 71 | pix = page.get_pixmap() |
| 72 | img_data = pix.tobytes() |
| 73 | img = Image.frombytes("RGB", [pix.width, pix.height], img_data) |
| 74 | |
| 75 | text = single_ocr(img) |
| 76 | results.append(text) |
| 77 | |
| 78 | if progress_callback: |
| 79 | # Update progress, reserve 20% for file saving phase |
| 80 | progress = int((page_num + 1) / total_pages * 80) |
| 81 | progress_callback(progress) |
| 82 | else: |
| 83 | # Process text PDF |
| 84 | for page_num in range(total_pages): |
| 85 | page = pdf_document[page_num] |
| 86 | text = page.get_text() |
| 87 | results.append(text) |
| 88 | |
| 89 | if progress_callback: |
| 90 | # Update progress, reserve 20% for file saving phase |
| 91 | progress = int((page_num + 1) / total_pages * 80) |
| 92 | progress_callback(progress) |
| 93 | |
| 94 | # Save results |
| 95 | save_file_path = os.path.join( |
| 96 | save_path, |
| 97 | f"{os.path.splitext(os.path.basename(file_path))[0]}.txt" |
| 98 | ) |
| 99 | |
| 100 | with open(save_file_path, 'w', encoding='utf-8') as f: |
| 101 | f.write('\n'.join(results)) |
| 102 | |
| 103 | if progress_callback: |
| 104 | progress_callback(100) # Complete |
| 105 | |
| 106 | pdf_document.close() |
| 107 | return save_file_path |
| 108 |