Load a document for parallel processing. Parameters: path_or_stream: File path or BytesIO object. password: Optional password for protected files. page_numbers: Optional 1-indexed physical pages to schedule. Returns: str: The document
(
self,
path_or_stream: Union[str, Path, BytesIO],
password: str | None = None,
page_numbers: Sequence[int] | None = None,
)
| 1437 | ) |
| 1438 | |
| 1439 | def load( |
| 1440 | self, |
| 1441 | path_or_stream: Union[str, Path, BytesIO], |
| 1442 | password: str | None = None, |
| 1443 | page_numbers: Sequence[int] | None = None, |
| 1444 | ) -> str: |
| 1445 | """Load a document for parallel processing. |
| 1446 | |
| 1447 | Parameters: |
| 1448 | path_or_stream: File path or BytesIO object. |
| 1449 | password: Optional password for protected files. |
| 1450 | page_numbers: Optional 1-indexed physical pages to schedule. |
| 1451 | |
| 1452 | Returns: |
| 1453 | str: The document key. |
| 1454 | """ |
| 1455 | if isinstance(path_or_stream, str): |
| 1456 | path_or_stream = Path(path_or_stream) |
| 1457 | |
| 1458 | if isinstance(path_or_stream, Path): |
| 1459 | key = f"key={path_or_stream!s}" |
| 1460 | success = self._parser.load_document( |
| 1461 | key=key, |
| 1462 | filename=str(path_or_stream).encode("utf8"), |
| 1463 | password=password, |
| 1464 | page_numbers=list(page_numbers) if page_numbers is not None else None, |
| 1465 | ) |
| 1466 | elif isinstance(path_or_stream, BytesIO): |
| 1467 | hasher = hashlib.sha256(usedforsecurity=False) |
| 1468 | while chunk := path_or_stream.read(8192): |
| 1469 | hasher.update(chunk) |
| 1470 | path_or_stream.seek(0) |
| 1471 | hash_val = hasher.hexdigest() |
| 1472 | |
| 1473 | key = f"key={hash_val}" |
| 1474 | success = self._parser.load_document_from_bytesio( |
| 1475 | key=key, |
| 1476 | bytes_io=path_or_stream, |
| 1477 | password=password, |
| 1478 | page_numbers=list(page_numbers) if page_numbers is not None else None, |
| 1479 | ) |
| 1480 | else: |
| 1481 | raise TypeError( |
| 1482 | f"Expected str, Path, or BytesIO, got {type(path_or_stream)}" |
| 1483 | ) |
| 1484 | |
| 1485 | if not success: |
| 1486 | raise RuntimeError(f"Failed to load document with key {key}") |
| 1487 | |
| 1488 | self._page_counts[key] = self._parser.number_of_pages(key) |
| 1489 | self._scheduled_page_counts[key] = self._parser.scheduled_number_of_pages(key) |
| 1490 | return key |
| 1491 | |
| 1492 | def page_count(self, doc_key: str) -> int: |
| 1493 | """Return the total page count for a loaded document.""" |