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

Prevent error loading pdf: Added pymupdf/fitz open to get page count #225

Merged
merged 1 commit into from
Jan 22, 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
9 changes: 7 additions & 2 deletions paperqa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from typing import BinaryIO, List, Union

import fitz
import pypdf
from langchain.base_language import BaseLanguageModel

Expand Down Expand Up @@ -64,8 +65,12 @@ def strings_similarity(s1: str, s2: str) -> float:

def count_pdf_pages(file_path: StrPath) -> int:
with open(file_path, "rb") as pdf_file:
pdf_reader = pypdf.PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
try: # try fitz by default
doc = fitz.open(file_path)
num_pages = len(doc)
except: # pypdf instead
pdf_reader = pypdf.PdfReader(pdf_file)
num_pages = len(pdf_reader.pages)
return num_pages


Expand Down
Loading