mirror of
https://github.com/run-llama/llama-hub.git
synced 2025-08-17 13:11:37 +00:00

* cr * cr * cr --------- Co-authored-by: Jerry Liu <jerry@robustintelligence.com> Co-authored-by: Jesse Zhang <jessetanzhang@gmail.com>
35 lines
953 B
Python
35 lines
953 B
Python
"""Read PDF files."""
|
|
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional
|
|
|
|
from llama_index.readers.base import BaseReader
|
|
from llama_index.readers.schema.base import Document
|
|
|
|
|
|
class PDFReader(BaseReader):
|
|
"""PDF reader."""
|
|
|
|
def load_data(
|
|
self, file: Path, extra_info: Optional[Dict] = None
|
|
) -> List[Document]:
|
|
"""Parse file."""
|
|
import PyPDF2
|
|
|
|
text_list = []
|
|
with open(file, "rb") as fp:
|
|
# Create a PDF object
|
|
pdf = PyPDF2.PdfReader(fp)
|
|
|
|
# Get the number of pages in the PDF document
|
|
num_pages = len(pdf.pages)
|
|
|
|
# Iterate over every page
|
|
for page in range(num_pages):
|
|
# Extract the text from the page
|
|
page_text = pdf.pages[page].extract_text()
|
|
text_list.append(page_text)
|
|
text = "\n".join(text_list)
|
|
|
|
return [Document(text, extra_info=extra_info)]
|