| 132 | |
| 133 | |
| 134 | def extract_page_texts(pdf_path: Path, max_pages: int | None) -> list[dict[str, Any]]: |
| 135 | if fitz is None: |
| 136 | raise RuntimeError("PyMuPDF is required for source text extraction.") |
| 137 | doc = fitz.open(pdf_path) |
| 138 | try: |
| 139 | page_limit = len(doc) if max_pages is None else min(len(doc), max_pages) |
| 140 | return [ |
| 141 | {"page": page_index + 1, "text": doc[page_index].get_text("text")} |
| 142 | for page_index in range(page_limit) |
| 143 | ] |
| 144 | finally: |
| 145 | doc.close() |
| 146 | |
| 147 | |
| 148 | def extract_raw_sections(page_texts: list[dict[str, Any]]) -> list[dict[str, Any]]: |