Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Scanning contents" progress bar with --redo-ocr #1441

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions src/ocrmypdf/pdfinfo/layout.py
Original file line number Diff line number Diff line change
@@ -338,13 +338,15 @@
self.infile = infile
self.rman = pdfminer.pdfinterp.PDFResourceManager(caching=True)
self.disable_boxes_flow = None
self.page_iter = None
self.page_cache: list[PDFPage] = []
self.pscript5_mode = pscript5_mode
self.file = None

def __enter__(self):
"""Enter the context manager."""
self.file = Path(self.infile).open('rb')
self.page_iter = PDFPage.get_pages(self.file)
return self

def __exit__(self, exc_type, exc_value, traceback):
@@ -353,27 +355,20 @@
self.file.close()
return True

def _load_page_cache(self):
"""Load the page cache."""
try:
self.page_cache = list(PDFPage.get_pages(self.file))
if not self.page_cache:
raise InputFileError(
"pdfminer did not find any pages in the input file."
)
for n, page in enumerate(self.page_cache):
if page is None:
raise InputFileError(
f"pdfminer could not process page {n} (counting from 0)."
)
except PDFTextExtractionNotAllowed as e:
raise EncryptedPdfError() from e

def get_page_analysis(self, pageno: int):
"""Get the page analysis for a given page."""
if not self.page_cache:
self._load_page_cache()
while len(self.page_cache) <= pageno:
try:
self.page_cache.append(next(self.page_iter))
except StopIteration:
raise InputFileError(
f"pdfminer did not find page {pageno} in the input file."
)
page = self.page_cache[pageno]
if not page:
raise InputFileError(

Check warning on line 369 in src/ocrmypdf/pdfinfo/layout.py

Codecov / codecov/patch

src/ocrmypdf/pdfinfo/layout.py#L369

Added line #L369 was not covered by tests
f"pdfminer could not process page {pageno} (counting from 0)."
)
dev = TextPositionTracker(
self.rman,
laparams=LAParams(
Loading