Skip to main content

PDFTextExtractor

A Python utility for extracting text and images from PDF files. The extracted text includes content from PDF pages and OCR-processed text from images embedded in the PDF. Results are returned as a combined list of dictionaries, preserving the order of appearance.


Features

  • Extract text directly from PDF pages.
  • Extract and OCR-process images embedded in PDFs.
  • Return results in a combined, ordered list of text and image content.
  • Preprocess images to improve OCR accuracy.

Requirements

Python

  • Python Version: 3.12 or higher
pip install pdf-text-extractor

Tesseract OCR

  • Tesseract Installation:
    Install Tesseract OCR and ensure it is accessible via the system’s PATH. Follow the Tesseract Installation Guide for details.

Usage

Import and Initialize:

from pdf_text_extractor import PDFTextExtractor

# Provide the PDF file path and image directory
pdf_path = "example.pdf"
image_dir = "output_images"

# Initialize the extractor
extractor = PDFTextExtractor(pdf_path, image_dir)

Process PDF and Extract Content

# Extract text and image content
results = extractor.process_and_extract_text()

# Display extracted content
for item in results:
    if "text" in item:
        print("PDF Text:", item["text"])
    elif "image_text" in item:
        print("Image Text:", item["image_text"])

Text and Image Extraction with LLM:

The latest version adds a feature to refine OCR-processed text using a language model (LLM), such as Ollama. This enhances the accuracy and readability of text extracted from images embedded within the PDF.

# Extract text and image content with LLM refinement for image-based text
results = extractor.process_and_extract_text(use_llm_for_image_text=True)

Output Format

The method process_and_extract_text() returns a list of dictionaries. Each dictionary contains either text or image_text, corresponding to content from the PDF or OCR-processed images.

Example Output

[
  {
    "text": "This is text from the first page of the PDF."
  },
  {
    "image_text": "Text extracted from an image on the first page."
  },
  {
    "text": "Another page of the PDF with textual content."
  },
  {
    "image_text": "Additional image-based text extracted."
  }
]

How It Works

Text Extraction

  • Text from PDF pages is extracted using PyMuPDF.

Image Extraction

  • Embedded images are extracted and saved to the specified directory.
  • Images are preprocessed before OCR.

Image Preprocessing

  • Convert to Grayscale: Converts the image to grayscale.
  • Enhance Contrast: Increases contrast to make text stand out.
  • Binarization: Uses Otsu’s thresholding to create a binary image.
  • Denoising: Applies Gaussian blur to reduce noise.

OCR

  • Preprocessed images are processed with Tesseract OCR to extract text.

Error Handling

  • If an image fails to process, an empty image_text value is added to the results.
  • Example:
{
  "image_text": ""
}

Methods

__init__(pdf_path, image_dir)

Parameters:

  • pdf_path (str): Path to the input PDF file.
  • image_dir (str): Directory to save extracted images.

process_and_extract_text()

Description: Processes the PDF to extract text and images.

Returns:

  • A list of dictionaries containing extracted text or image_text.

Contribution

Contributions are welcome! If you have suggestions or improvements, please open an issue or submit a pull request.

Metadata

Release files for pdf-text-extractor 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdf-text-extractor 0.2.4
File Size Uploaded Provenance Actions
pdf_text_extractor-0.2.4.tar.gz 5.4 kB Without provenance Details for pdf_text_extractor-0.2.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pdf-text-extractor 0.2.4
File Interpreter ABI Platform Actions
pdf_text_extractor-0.2.4-py3-none-any.whl Python 3 none any Details for pdf_text_extractor-0.2.4-py3-none-any.whl

Total release size: 10.6 kB

Release files / pdf_text_extractor-0.2.4.tar.gz

Download URL pdf_text_extractor-0.2.4.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
78dee78cee7e6786ac47a3fced5d29dcf15e97b383f4888c7687fce038076252
BLAKE2b-256 checksum
How to use checksums
76015638aef0a3427eda604cf059d4db42670ca49d380f3893437a2b29d51769
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / pdf_text_extractor-0.2.4-py3-none-any.whl

Download URL pdf_text_extractor-0.2.4-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce00bb0b2c2359303adee45e02d88642df3cb6380b8e4b3373220e7aabeb383f
BLAKE2b-256 checksum
How to use checksums
b18dfa1d79cb6ed4350f5d6e82306698ece731946bc33eddf0c9e688f3ef79f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Security for pdf-text-extractor 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page