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

Eliminated duplicate processes. #255

Merged
merged 1 commit into from
Nov 9, 2024
Merged
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
15 changes: 11 additions & 4 deletions camelot/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def _get_pages(self, pages):
result.extend(range(p["start"], p["end"] + 1))
return sorted(set(result))

def _save_page(self, filepath: StrByteType | Path, page: int, temp: str):
def _save_page(
self, filepath: StrByteType | Path, page: int, temp: str, **layout_kwargs
):
"""Saves specified page from PDF into a temporary directory.

Parameters
Expand Down Expand Up @@ -152,7 +154,7 @@ def _save_page(self, filepath: StrByteType | Path, page: int, temp: str):
outfile.add_page(p)
with open(fpath, "wb") as f:
outfile.write(f)
layout, dim = get_page_layout(fpath)
layout, dimensions = get_page_layout(fpath, **layout_kwargs)
# fix rotated PDF
chars = get_text_objects(layout, ltype="char")
horizontal_text = get_text_objects(layout, ltype="horizontal_text")
Expand All @@ -174,7 +176,11 @@ def _save_page(self, filepath: StrByteType | Path, page: int, temp: str):
outfile.add_page(p)
with open(fpath, "wb") as f:
outfile.write(f)
# Only recompute layout and dimension after rotating the pdf
layout, dimensions = get_page_layout(fpath, **layout_kwargs)
instream.close()
return layout, dimensions
return layout, dimensions

def parse(
self,
Expand Down Expand Up @@ -264,9 +270,10 @@ def _parse_page(
List of tables found in PDF.

"""
self._save_page(self.filepath, page, tempdir)
layout, dimensions = self._save_page(
self.filepath, page, tempdir, **layout_kwargs
)
page_path = os.path.join(tempdir, f"page-{page}.pdf")
layout, dimensions = get_page_layout(page_path, **layout_kwargs)
parser.prepare_page_parse(
page_path, layout, dimensions, page, layout_kwargs=layout_kwargs
)
Expand Down
Loading