从 PDF 提取文本
(pdf_path: str)
| 485 | |
| 486 | |
| 487 | def extract_text_from_pdf(pdf_path: str) -> str: |
| 488 | """从 PDF 提取文本""" |
| 489 | pdf_file = Path(pdf_path) |
| 490 | if not pdf_file.exists(): |
| 491 | raise FileNotFoundError(f"PDF not found: {pdf_path}") |
| 492 | |
| 493 | primary_text = "" |
| 494 | if HAS_PYMUPDF: |
| 495 | primary_text = _extract_text_from_pdf_with_pymupdf(pdf_path) |
| 496 | elif HAS_PDFPLUMBER: |
| 497 | primary_text = _extract_text_from_pdf_with_pdfplumber(pdf_path) |
| 498 | else: |
| 499 | raise ImportError("No PDF library available. Install pymupdf or pdfplumber.") |
| 500 | |
| 501 | if _should_try_ocr(primary_text, pdf_path): |
| 502 | ocr_text = _extract_text_from_pdf_with_ocr(pdf_path) |
| 503 | if (not primary_text and ocr_text) or len(ocr_text) > len(primary_text) + 80: |
| 504 | return ocr_text |
| 505 | |
| 506 | return primary_text |
| 507 | |
| 508 | |
| 509 | def extract_metadata(text: str) -> Dict: |
no test coverage detected