Skip to main content

pdf-strings

Extract text from PDFs with position data.

Installation

pip install pdf-strings

Quick Start

from pdf_strings import from_path

# Extract text from a PDF
output = from_path("document.pdf")
print(output)  # Plain text

API Reference

Functions

from_path(path: str, *, password: str | None = None) -> TextOutput

Extract text from a PDF file.

Parameters:

  • path (str): Path to the PDF file
  • password (str, optional): Password for encrypted PDFs

Returns: TextOutput object containing structured lines and spans

Example:

from pdf_strings import from_path

# Basic usage
output = from_path("document.pdf")

# With password
output = from_path("encrypted.pdf", password="secret")

from_bytes(data: bytes, *, password: str | None = None) -> TextOutput

Extract text from PDF bytes.

Parameters:

  • data (bytes): PDF file contents as bytes
  • password (str, optional): Password for encrypted PDFs

Returns: TextOutput object containing structured lines and spans

Example:

from pdf_strings import from_bytes

with open("document.pdf", "rb") as f:
    data = f.read()

output = from_bytes(data)

Classes

TextOutput

Container for extracted text with structured data.

Attributes:

  • lines (List[List[TextSpan]]): Lines of text, each containing multiple spans

Methods:

to_string() -> str

Get plain text output (concatenates all text with spaces).

output = from_path("document.pdf")
plain_text = output.to_string()
# or simply:
plain_text = str(output)
to_string_pretty() -> str

Get formatted text that preserves spatial layout using a character grid.

output = from_path("document.pdf")
formatted_text = output.to_string_pretty()
# or using format spec:
formatted_text = f"{output:#}"

Magic Methods:

  • __str__(): Returns plain text (same as to_string())
  • __format__(format_spec): Use # for pretty formatting: f"{output:#}"

TextSpan

A span of text with position and metadata.

Attributes:

  • text (str): The text content
  • bbox (BoundingBox): Bounding box coordinates
  • font_size (float): Font size in points
  • page (int): Page number (0-indexed)

Example:

output = from_path("document.pdf")
for line in output.lines:
    for span in line:
        print(f"'{span.text}' at size {span.font_size}pt on page {span.page}")
        print(f"  Position: {span.bbox}")

BoundingBox

Bounding box coordinates for a text span.

Attributes:

  • top (float): Top coordinate
  • right (float): Right coordinate
  • bottom (float): Bottom coordinate
  • left (float): Left coordinate

String representation: (top, right, bottom, left) following HTML margin convention.

Example:

bbox = span.bbox
print(f"Top-left: ({bbox.left}, {bbox.top})")
print(f"Width: {bbox.right - bbox.left}")
print(f"Height: {bbox.top - bbox.bottom}")

Usage Examples

Extract all text

from pdf_strings import from_path

output = from_path("document.pdf")
print(output.to_string())

Preserve layout

from pdf_strings import from_path

output = from_path("invoice.pdf")
# Character grid rendering preserves columns and spacing
print(output.to_string_pretty())

Access structured data

from pdf_strings import from_path

output = from_path("document.pdf")

for line_idx, line in enumerate(output.lines):
    print(f"Line {line_idx}:")
    for span in line:
        print(f"  {span.text}")
        print(f"    Font size: {span.font_size}")
        print(f"    Position: ({span.bbox.left}, {span.bbox.top})")
        print(f"    Page: {span.page}")

Find text in specific regions

from pdf_strings import from_path

output = from_path("document.pdf")

# Find text in the top-right corner
for line in output.lines:
    for span in line:
        if span.bbox.top < 100 and span.bbox.left > 400:
            print(f"Top-right text: {span.text}")

Extract tables by position

from pdf_strings import from_path

output = from_path("table.pdf")

# Group spans by their vertical position (rows)
rows = {}
for line in output.lines:
    for span in line:
        row_key = round(span.bbox.top / 10) * 10  # Group by ~10pt vertical bands
        if row_key not in rows:
            rows[row_key] = []
        rows[row_key].append((span.bbox.left, span.text))

# Print rows sorted by vertical position
for y_pos in sorted(rows.keys(), reverse=True):
    # Sort spans in each row by horizontal position
    row_spans = sorted(rows[y_pos], key=lambda x: x[0])
    print(" | ".join(text for _, text in row_spans))

Features

  • Plain text extraction
  • Spatial layout preservation via character grid
  • Bounding box coordinates for every text span
  • Font size and page information
  • Password-protected PDF support
  • Handles complex fonts, rotated text, and multi-column layouts
  • Works with all Python 3.11+ versions

License

MIT

Metadata

Release files for pdf-strings 0.1.2

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

Built distributions (wheels)

Table of built distributions (wheels) for pdf-strings 0.1.2
File Interpreter ABI Platform Actions
pdf_strings-0.1.2-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for pdf_strings-0.1.2-py3-none-win_amd64.whl
pdf_strings-0.1.2-py3-none-manylinux_2_17_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for pdf_strings-0.1.2-py3-none-manylinux_2_17_x86_64.whl
pdf_strings-0.1.2-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for pdf_strings-0.1.2-py3-none-macosx_11_0_arm64.whl

Total release size: 11.0 MB

Release files / pdf_strings-0.1.2-py3-none-win_amd64.whl

Download URL pdf_strings-0.1.2-py3-none-win_amd64.whl
Size 944.1 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
4d74a7ad96760f4bca4b4e61fa2dfbb64269e1fa5f4c2e05d1239b7e90496796
BLAKE2b-256 checksum
How to use checksums
c967495028f54cb90bc363ce1ac2d8750935c70dac69fcbe508dd5cd6da7d961
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Release files / pdf_strings-0.1.2-py3-none-manylinux_2_17_x86_64.whl

Download URL pdf_strings-0.1.2-py3-none-manylinux_2_17_x86_64.whl
Size 9.0 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
7e024592240dbce9a8ca61cbdd0a565c2383030b583a830866f792e7b94f61cd
BLAKE2b-256 checksum
How to use checksums
48d184347688f07b766ba1eb00a7f4bac2b8b58cc1e4accfb24538bf4abf8e24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Release files / pdf_strings-0.1.2-py3-none-macosx_11_0_arm64.whl

Download URL pdf_strings-0.1.2-py3-none-macosx_11_0_arm64.whl
Size 1.0 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
943014022619f99df05a8cbaf6ebfb59b475b73f029ac578fc0753a2e22289ee
BLAKE2b-256 checksum
How to use checksums
1cc2d42c84748ac64d6dd5b4cc08478c0f70b507cdd9a2f440f557c5f0120366
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.5

Security for pdf-strings 0.1.2

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