
Picture by Creator | Canva
PDF information are in every single place. You’ve in all probability seen them in numerous locations, comparable to school papers, electrical energy payments, workplace contracts, product manuals, and extra. They’re tremendous frequent, however working with them just isn’t as straightforward because it appears. Let’s say you need to extract helpful info from a PDF, like studying the textual content, splitting it into sections, or getting a fast abstract. This will sound easy, however you’ll see it’s not so easy when you strive.
In contrast to Phrase or HTML information, PDFs don’t retailer content material in a neat, readable means. As an alternative, they’re designed to look good, to not be learn by packages. The textual content might be in every single place, break up into bizarre blocks, scattered throughout the web page, or blended up with tables and pictures. This makes it arduous to get clear, structured information from them.
On this article, we’re going to construct one thing that may deal with this mess. We’ll create a customized PDF parser that may:
- Extract and clear textual content from PDFs on the web page stage, with elective structure preservation for higher formatting
- Deal with picture metadata extraction
- Take away undesirable headers and footers by detecting repeated strains throughout pages to cut back noise
- Retrieve detailed doc and page-level metadata, comparable to creator, title, creation date, rotation, and web page dimension
- Chunk the content material into manageable items for additional NLP or LLM processing
Let’s get began.
Folder Construction
Earlier than beginning, it’s good to arrange your undertaking information for readability and scalability.
custom_pdf_parser/
│
├── parser.py
├── langchain_loader.py
├── pipeline.py
├── instance.py
├── necessities.txt # Dependencies listing
└── __init__.py # (Optionally available) to mark listing as Python bundle
You possibly can go away the __init.py__ file empty, as its important function is just to point that this listing must be handled as a Python bundle. I’ll clarify the aim of every of the remaining information step-by-step.
Instruments Required(necessities.txt)
The mandatory libraries required are:
- PyPDF: A pure Python library to learn and write PDF information. It will likely be used to extract the textual content from PDF information
- LangChain: A framework to construct context-aware purposes with language fashions (we’ll use it to course of and chain doc duties). It will likely be used to course of and manage the textual content correctly.
Set up them with:
pip set up pypdf langchain
If you wish to handle dependencies neatly, create a necessities.txt file with:
And run:
pip set up -r necessities.txt
Step 1: Set Up the PDF Parser(parser.py)
The core class CustomPDFParser makes use of PyPDF to extract textual content and metadata from every PDF web page. It additionally consists of strategies to scrub textual content, extract picture info (elective), and take away repeated headers or footers that usually seem on every web page.
- It helps preserving structure formatting
- It extracts metadata like web page quantity, rotation, and media field dimensions
- It might probably filter out pages with too little content material
- Textual content cleansing removes extreme whitespace whereas preserving paragraph breaks
The logic that implements all of those is:
import os
import logging
from pathlib import Path
from typing import Listing, Dict, Any
import pypdf
from pypdf import PdfReader
# Configure logging to indicate data and above messages
logging.basicConfig(stage=logging.INFO)
logger = logging.getLogger(__name__)
class CustomPDFParser:
def __init__(
self,extract_images: bool = False,preserve_layout: bool = True,remove_headers_footers: bool = True,min_text_length: int = 10
):
"""
Initialize the parser with choices to extract photos, protect structure, take away repeated headers/footers, and minimal textual content size for pages.
Args:
extract_images: Whether or not to extract picture data from pages
preserve_layout: Whether or not to maintain structure spacing in textual content extraction
remove_headers_footers: Whether or not to detect and take away headers/footers
min_text_length: Minimal size of textual content for a web page to be thought of legitimate
"""
self.extract_images = extract_images
self.preserve_layout = preserve_layout
self.remove_headers_footers = remove_headers_footers
self.min_text_length = min_text_length
def extract_text_from_page(self, web page: pypdf.PageObject, page_num: int) -> Dict[str, Any]:
"""
Extract textual content and metadata from a single PDF web page.
Args:
web page: PyPDF web page object
page_num: zero-based web page quantity
Returns:
dict with keys:
- 'textual content': extracted and cleaned textual content string,
- 'metadata': web page metadata dict,
- 'word_count': variety of phrases in extracted textual content
"""
strive:
# Extract textual content, optionally preserving the structure for higher formatting
if self.preserve_layout:
textual content = web page.extract_text(extraction_mode="structure")
else:
textual content = web page.extract_text()
# Clear textual content: take away further whitespace and normalize paragraphs
textual content = self._clean_text(textual content)
# Collect web page metadata (web page quantity, rotation angle, mediabox)
metadata = {
"page_number": page_num + 1, # 1-based numbering
"rotation": getattr(web page, "rotation", 0),
"mediabox": str(getattr(web page, "mediabox", None)),
}
# Optionally, extract picture data from web page if requested
if self.extract_images:
metadata["images"] = self._extract_image_info(web page)
# Return dictionary with textual content and metadata for this web page
return {
"textual content": textual content,
"metadata": metadata,
"word_count": len(textual content.break up()) if textual content else 0
}
besides Exception as e:
# Log error and return empty information for problematic pages
logger.error(f"Error extracting web page {page_num}: {e}")
return {
"textual content": "",
"metadata": {"page_number": page_num + 1, "error": str(e)},
"word_count": 0
}
def _clean_text(self, textual content: str) -> str:
"""
Clear and normalize extracted textual content, preserving paragraph breaks.
Args:
textual content: uncooked textual content extracted from PDF web page
Returns:
cleaned textual content string
"""
if not textual content:
return ""
strains = textual content.break up('n')
cleaned_lines = []
for line in strains:
line = line.strip() # Take away main/trailing whitespace
if line:
# Non-empty line; hold it
cleaned_lines.append(line)
elif cleaned_lines and cleaned_lines[-1]:
# Protect paragraph break by conserving empty line provided that earlier line exists
cleaned_lines.append("")
cleaned_text="n".be part of(cleaned_lines)
#Scale back any cases of greater than two consecutive clean strains to 2
whereas 'nnn' in cleaned_text:
cleaned_text = cleaned_text.change('nnn', 'nn')
return cleaned_text.strip()
def _extract_image_info(self, web page: pypdf.PageObject) -> Listing[Dict[str, Any]]:
"""
Extract primary picture metadata from web page, if obtainable.
Args:
web page: PyPDF web page object
Returns:
Listing of dictionaries with picture data (index, identify, width, top)
"""
photos = []
strive:
# PyPDF pages can have an 'photos' attribute itemizing embedded photos
if hasattr(web page, 'photos'):
for i, picture in enumerate(web page.photos):
photos.append({
"image_index": i,
"identify": getattr(picture, 'identify', f"image_{i}"),
"width": getattr(picture, 'width', None),
"top": getattr(picture, 'top', None)
})
besides Exception as e:
logger.warning(f"Picture extraction failed: {e}")
return photos
def _remove_headers_footers(self, pages_data: Listing[Dict[str, Any]]) -> Listing[Dict[str, Any]]:
"""
Take away repeated headers and footers that seem on many pages.
That is completed by figuring out strains showing on over 50% of pages
in the beginning or finish of web page textual content, then eradicating these strains.
Args:
pages_data: Listing of dictionaries representing every web page's extracted information.
Returns:
Up to date listing of pages with headers/footers eliminated
"""
# Solely try elimination if sufficient pages and choice enabled
if len(pages_data) threshold and line.strip()]
potential_footers = [line for line in set(last_lines)
if last_lines.count(line) > threshold and line.strip()]
# Take away recognized headers and footers from every web page's textual content
for page_data in pages_data:
strains = page_data["text"].break up('n')
# Take away header if it matches a frequent header
if strains and potential_headers:
for header in potential_headers:
if strains[0].strip() == header.strip():
strains = strains[1:]
break
# Take away footer if it matches a frequent footer
if strains and potential_footers:
for footer in potential_footers:
if strains[-1].strip() == footer.strip():
strains = strains[:-1]
break
page_data["text"] = 'n'.be part of(strains).strip()
return pages_data
def _extract_document_metadata(self, pdf_reader: PdfReader, pdf_path: str) -> Dict[str, Any]:
"""
Extract metadata from the PDF doc itself.
Args:
pdf_reader: PyPDF PdfReader occasion
pdf_path: path to PDF file
Returns:
Dictionary of metadata together with file data and PDF doc metadata
"""
metadata = {
"file_path": pdf_path,
"file_name": Path(pdf_path).identify,
"file_size": os.path.getsize(pdf_path) if os.path.exists(pdf_path) else None,
}
strive:
if pdf_reader.metadata:
# Extract frequent PDF metadata keys if obtainable
metadata.replace({
"title": pdf_reader.metadata.get('/Title', ''),
"creator": pdf_reader.metadata.get('/Creator', ''),
"topic": pdf_reader.metadata.get('/Topic', ''),
"creator": pdf_reader.metadata.get('/Creator', ''),
"producer": pdf_reader.metadata.get('/Producer', ''),
"creation_date": str(pdf_reader.metadata.get('/CreationDate', '')),
"modification_date": str(pdf_reader.metadata.get('/ModDate', '')),
})
besides Exception as e:
logger.warning(f"Metadata extraction failed: {e}")
return metadata
def parse_pdf(self, pdf_path: str) -> Dict[str, Any]:
"""
Parse the complete PDF file. Opens the file, extracts textual content and metadata web page by web page, removes headers/footers if configured, and aggregates outcomes.
Args:
pdf_path: Path to the PDF file
Returns:
Dictionary with keys:
- 'full_text': mixed textual content from all pages,
- 'pages': listing of page-wise dicts with textual content and metadata,
- 'document_metadata': file and PDF metadata,
- 'total_pages': whole pages in PDF,
- 'processed_pages': variety of pages saved after filtering,
- 'total_words': whole phrase depend of parsed textual content
"""
strive:
with open(pdf_path, 'rb') as file:
pdf_reader = PdfReader(file)
doc_metadata = self._extract_document_metadata(pdf_reader, pdf_path)
pages_data = []
# Iterate over all pages and extract information
for i, web page in enumerate(pdf_reader.pages):
page_data = self.extract_text_from_page(web page, i)
# Solely hold pages with adequate textual content size
if len(page_data["text"]) >= self.min_text_length:
pages_data.append(page_data)
# Take away repeated headers and footers
pages_data = self._remove_headers_footers(pages_data)
# Mix all web page texts with a double newline as a separator
full_text="nn".be part of(web page["text"] for web page in pages_data if web page["text"])
# Return last structured information
return {
"full_text": full_text,
"pages": pages_data,
"document_metadata": doc_metadata,
"total_pages": len(pdf_reader.pages),
"processed_pages": len(pages_data),
"total_words": sum(web page["word_count"] for web page in pages_data)
}
besides Exception as e:
logger.error(f"Did not parse PDF {pdf_path}: {e}")
increase
Step 2: Combine with LangChain (langchain_loader.py)
The LangChainPDFLoader class wraps the customized parser and converts parsed pages into LangChain Doc objects, that are the constructing blocks for LangChain pipelines.
- It permits chunking of paperwork into smaller items utilizing LangChain’s RecursiveCharacterTextSplitter
- You possibly can customise chunk sizes and overlap for downstream LLM enter
- This loader helps clear integration between uncooked PDF content material and LangChain’s doc abstraction
The logic behind that is:
from typing import Listing, Optionally available, Dict, Any
from langchain.schema import Doc
from langchain.document_loaders.base import BaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from parser import CustomPDFParser # import the parser outlined above
class LangChainPDFLoader(BaseLoader):
def __init__(
self,file_path: str,parser_config: Optionally available[Dict[str, Any]] = None,chunk_size: int = 500, chunk_overlap: int = 50
):
"""
Initialize the loader with the PDF file path, parser configuration, and chunking parameters.
Args:
file_path: path to PDF file
parser_config: dictionary of parser choices
chunk_size: chunk dimension for splitting lengthy texts
chunk_overlap: chunk overlap for splitting
"""
self.file_path = file_path
self.parser_config = parser_config or {}
self.chunk_size = chunk_size
self.chunk_overlap = chunk_overlap
self.parser = CustomPDFParser(**self.parser_config)
def load(self) -> Listing[Document]:
"""
Load PDF, parse pages, and convert every web page to a LangChain Doc.
Returns:
Listing of Doc objects with web page textual content and mixed metadata.
"""
parsed_data = self.parser.parse_pdf(self.file_path)
paperwork = []
# Convert every web page dict to a LangChain Doc
for page_data in parsed_data["pages"]:
if page_data["text"]:
# Merge document-level and page-level metadata
metadata = {**parsed_data["document_metadata"], **page_data["metadata"]}
doc = Doc(page_content=page_data["text"], metadata=metadata)
paperwork.append(doc)
return paperwork
def load_and_split(self) -> Listing[Document]:
"""
Load the PDF and break up massive paperwork into smaller chunks.
Returns:
Listing of Doc objects after splitting massive texts.
"""
paperwork = self.load()
# Initialize a textual content splitter with the specified chunk dimension and overlap
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=self.chunk_size,
chunk_overlap=self.chunk_overlap,
separators=["nn", "n", " ", ""] # hierarchical splitting
)
# Cut up paperwork into smaller chunks
split_docs = text_splitter.split_documents(paperwork)
return split_docs
Step 3: Construct a Processing Pipeline (pipeline.py)
The PDFProcessingPipeline class offers a higher-level interface for:
- Processing a single PDF
- Choosing output format (uncooked dict, LangChain paperwork, or plain textual content)
- Enabling or disabling chunking with configurable chunk sizes
- Dealing with errors and logging
This abstraction permits straightforward integration into bigger purposes or workflows. The logic behind that is:
from typing import Listing, Optionally available, Dict, Any
from langchain.schema import Doc
from parser import CustomPDFParser
from langchain_loader import LangChainPDFLoader
import logging
logger = logging.getLogger(__name__)
class PDFProcessingPipeline:
def __init__(self, parser_config: Optionally available[Dict[str, Any]] = None):
"""
Args:
parser_config: dictionary of choices handed to CustomPDFParser
"""
self.parser_config = parser_config or {}
def process_single_pdf(
self,pdf_path: str,output_format: str = "langchain",chunk_documents: bool = True,chunk_size: int = 500,chunk_overlap: int = 50
) -> Any:
"""
Args:
pdf_path: path to PDF file
output_format: "uncooked" (dict), "langchain" (Paperwork), or "textual content" (string)
chunk_documents: whether or not to separate LangChain paperwork into chunks
chunk_size: chunk dimension for splitting
chunk_overlap: chunk overlap for splitting
Returns:
Parsed content material within the requested format
"""
if output_format == "uncooked":
# Use uncooked CustomPDFParser output
parser = CustomPDFParser(**self.parser_config)
return parser.parse_pdf(pdf_path)
elif output_format == "langchain":
# Use LangChain loader, optionally chunked
loader = LangChainPDFLoader(pdf_path, self.parser_config, chunk_size, chunk_overlap)
if chunk_documents:
return loader.load_and_split()
else:
return loader.load()
elif output_format == "textual content":
# Return mixed plain textual content solely
parser = CustomPDFParser(**self.parser_config)
parsed_data = parser.parse_pdf(pdf_path)
return parsed_data.get("full_text", "")
else:
increase ValueError(f"Unknown output_format: {output_format}")
Step 4: Check the Parser (instance.py)
Let’s take a look at the parser as follows:
import os
from pathlib import Path
def important():
print("👋 Welcome to the Customized PDF Parser!")
print("What would you love to do?")
print("1. View full parsed uncooked information")
print("2. Extract full plain textual content")
print("3. Get LangChain paperwork (no chunking)")
print("4. Get LangChain paperwork (with chunking)")
print("5. Present doc metadata")
print("6. Present per-page metadata")
print("7. Present cleaned web page textual content (header/footer eliminated)")
print("8. Present extracted picture metadata")
alternative = enter("Enter the variety of your alternative: ").strip()
if alternative not in {'1', '2', '3', '4', '5', '6', '7', '8'}:
print("❌ Invalid choice.")
return
file_path = enter("Enter the trail to your PDF file: ").strip()
if not Path(file_path).exists():
print("❌ File not discovered.")
return
# Initialize pipeline
pipeline = PDFProcessingPipeline({
"preserve_layout": False,
"remove_headers_footers": True,
"extract_images": True,
"min_text_length": 20
})
# Uncooked information is required for many choices
parsed = pipeline.process_single_pdf(file_path, output_format="uncooked")
if alternative == '1':
print("nFull Uncooked Parsed Output:")
for okay, v in parsed.gadgets():
print(f"{okay}: {str(v)[:300]}...")
elif alternative == '2':
print("nFull Cleaned Textual content (truncated preview):")
print("Previewing the primary 1000 characters:n"+parsed["full_text"][:1000], "...")
elif alternative == '3':
docs = pipeline.process_single_pdf(file_path, output_format="langchain", chunk_documents=False)
print(f"nLangChain Paperwork: {len(docs)}")
print("Previewing the primary 500 characters:n", docs[0].page_content[:500], "...")
elif alternative == '4':
docs = pipeline.process_single_pdf(file_path, output_format="langchain", chunk_documents=True)
print(f"nLangChain Chunks: {len(docs)}")
print("Pattern chunk content material (first 500 chars):")
print(docs[0].page_content[:500], "...")
elif alternative == '5':
print("nDocument Metadata:")
for key, worth in parsed["document_metadata"].gadgets():
print(f"{key}: {worth}")
elif alternative == '6':
print("nPer-page Metadata:")
for i, web page in enumerate(parsed["pages"]):
print(f"Web page {i+1}: {web page['metadata']}")
elif alternative == '7':
print("nCleaned Textual content After Header/Footer Removing.")
print("Displaying the primary 3 pages and first 500 characters of the textual content from every web page.")
for i, web page in enumerate(parsed["pages"][:3]): # First 3 pages
print(f"n--- Web page {i+1} ---")
print(web page["text"][:500], "...")
elif alternative == '8':
print("nExtracted Picture Metadata (if obtainable):")
discovered = False
for i, web page in enumerate(parsed["pages"]):
photos = web page["metadata"].get("photos", [])
if photos:
discovered = True
print(f"n--- Web page {i+1} ---")
for img in photos:
print(img)
if not discovered:
print("No picture metadata discovered.")
if __name__ == "__main__":
important()
Run this and you may be directed to enter the selection no and path to the PDF. Enter that. The PDF I’m utilizing is publicly accessible, and you may obtain it utilizing the hyperlink.
👋 Welcome to the Customized PDF Parser!
What would you love to do?
1. View full parsed uncooked information
2. Extract full plain textual content
3. Get LangChain paperwork (no chunking)
4. Get LangChain paperwork (with chunking)
5. Present doc metadata
6. Present per-page metadata
7. Present cleaned web page textual content (header/footer eliminated)
8. Present extracted picture metadata.
Enter the variety of your alternative: 5
Enter the trail to your PDF file: /content material/articles.pdf
Output:
LangChain Chunks: 16
First chunk preview:
San José State College Writing Middle
www.sjsu.edu/writingcenter
Written by Ben Aldridge
Articles (a/an/the), Spring 2014. 1 of 4
Articles (a/an/the)
There are three articles within the English language: a, an, and the. They're positioned earlier than nouns
and present whether or not a given noun is normal or particular.
Examples of Articles
Conclusion
On this information, you’ve realized how one can construct a versatile and highly effective PDF processing pipeline utilizing solely open-source instruments. As a result of it’s modular, you’ll be able to simply prolong it, possibly add a search bar utilizing Streamlit, retailer chunks in a vector database like FAISS for smarter lookups, and even plug this right into a chatbot. You don’t need to rebuild something, you simply join the following piece.PDFs don’t need to really feel like locked packing containers anymore. With this strategy, you’ll be able to flip any doc into one thing you’ll be able to learn, search, and perceive in your phrases.
Kanwal Mehreen Kanwal is a machine studying engineer and a technical author with a profound ardour for information science and the intersection of AI with medication. She co-authored the e-book “Maximizing Productiveness with ChatGPT”. As a Google Era Scholar 2022 for APAC, she champions range and tutorial excellence. She’s additionally acknowledged as a Teradata Range in Tech Scholar, Mitacs Globalink Analysis Scholar, and Harvard WeCode Scholar. Kanwal is an ardent advocate for change, having based FEMCodes to empower girls in STEM fields.