Quickly extract text characters and character metadata from pdfs using pdfium.
Low maintenance activity. Last release 17 months ago. Weaker on recent releases and release cadence.
pip install unpdfREADME as published on PyPI with version 1.0.0.
PDF Extract
A lightweight Python library for extracting text characters and metadata from PDF files using PDFium. This library provides detailed character-level information including positions, fonts, colors, and transformation matrices without external dependencies.
Features
- Character-level extraction: Get individual characters with precise positioning
- Font metadata: Extract font information, sizes, and styling
- Color information: Access text colors (RGBA values)
- Transformation matrices: Retrieve text transformation data
- Page boundaries: Get page dimensions and bounding boxes
- Zero dependencies: Uses PDFium directly with no external requirements
- PyArrow integration: Optional PyArrow table output for data analysis
Installation
pip install pdf-extract
Basic Usage
Simple Character Extraction
from unpdf import extract
# Extract data from PDF
pages, chars, text_objs, fonts = extract("document.pdf")
# Access character data
print(f"Total characters: {len(chars.arrays['char'])}")
print(f"Total text objects: {len(text_objs.arrays['txt_obj_id'])}")
print(f"Total fonts: {len(fonts.arrays['font_obj_id'])}")
# Get page information
print(f"Page width: {pages.arrays['width'][0]}")
print(f"Page height: {pages.arrays['height'][0]}")
Reconstructing Text
from unpdf import extract
_, chars, _, _ = extract("document.pdf")
# Convert characters to string
text = ''.join(chr(c) for c in chars.arrays['char'])
print(text)
Character Positioning
from unpdf import extract
_, chars, _, _ = extract("document.pdf")
# Access character positions
for i in range(len(chars.arrays['char'])):
char = chr(chars.arrays['char'][i])
left = chars.arrays['left'][i]
top = chars.arrays['top'][i]
right = chars.arrays['right'][i]
bottom = chars.arrays['bottom'][i]
print(f"'{char}' at ({left:.1f}, {top:.1f}) - ({right:.1f}, {bottom:.1f})")
PyArrow / pandas / polars Integration
For data analysis and advanced processing, it is recommended to convert to first convert to PyArrow tables. Note that PyArrow must be installed separately. From PyArrow tables, you can easily convert to pandas or polars DataFrames.