Skip to content

pdfminer #1003

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

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion docs/modules/document_loaders/examples/pdf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,48 @@
"data = loader.load()"
]
},
{
"cell_type": "markdown",
"id": "21998d18",
"metadata": {},
"source": [
"## Using PDFMiner"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2f0cc9ff",
"metadata": {},
"outputs": [],
"source": [
"from langchain.document_loaders import PDFMinerLoader"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "42b531e8",
"metadata": {},
"outputs": [],
"source": [
"loader = PDFMinerLoader(\"example_data/layout-parser-paper.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "010d5cdd",
"metadata": {},
"outputs": [],
"source": [
"data = loader.load()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54fb6b62",
"id": "7301c473",
"metadata": {},
"outputs": [],
"source": []
Expand Down
3 changes: 2 additions & 1 deletion langchain/document_loaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from langchain.document_loaders.obsidian import ObsidianLoader
from langchain.document_loaders.online_pdf import OnlinePDFLoader
from langchain.document_loaders.paged_pdf import PagedPDFSplitter
from langchain.document_loaders.pdf import UnstructuredPDFLoader
from langchain.document_loaders.pdf import PDFMinerLoader, UnstructuredPDFLoader
from langchain.document_loaders.powerpoint import UnstructuredPowerPointLoader
from langchain.document_loaders.readthedocs import ReadTheDocsLoader
from langchain.document_loaders.roam import RoamLoader
Expand Down Expand Up @@ -58,4 +58,5 @@
"EveryNoteLoader",
"AirbyteJSONLoader",
"OnlinePDFLoader",
"PDFMinerLoader",
]
23 changes: 23 additions & 0 deletions langchain/document_loaders/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,26 @@ def load(self) -> List[Document]:
text = "\n\n".join([str(el) for el in elements])
metadata = {"source": self.file_path}
return [Document(page_content=text, metadata=metadata)]


class PDFMinerLoader(BaseLoader):
"""Loader that uses PDFMiner to load PDF files."""

def __init__(self, file_path: str):
"""Initialize with file path."""
try:
from pdfminer.high_level import extract_text # noqa:F401
except ImportError:
raise ValueError(
"pdfminer package not found, please install it with "
"`pip install pdfminer.six`"
)
self.file_path = file_path

def load(self) -> List[Document]:
"""Load file."""
from pdfminer.high_level import extract_text

text = extract_text(self.file_path)
metadata = {"source": self.file_path}
return [Document(page_content=text, metadata=metadata)]