Claude Code for pdfplumber: Python PDF Table and Text Extraction — Claude Skills 360 Blog
Blog / AI / Claude Code for pdfplumber: Python PDF Table and Text Extraction
AI

Claude Code for pdfplumber: Python PDF Table and Text Extraction

Published: February 23, 2028
Read time: 5 min read
By: Claude Skills 360

pdfplumber extracts tables and text with precise layout information from PDFs. pip install pdfplumber. Open: import pdfplumber; with pdfplumber.open("file.pdf") as pdf:. pdf.pages — list of pages. pdf.pages[0] — first page. Text: page.extract_text() — layout-aware text. page.extract_text(x_tolerance=3, y_tolerance=3). Words: page.extract_words() — list of word dicts with x0 y0 x1 y1. Lines: page.extract_text_lines() — words grouped by line. Tables: page.extract_table() — first table as list of rows. page.extract_tables() — all tables. Table settings: page.extract_table(table_settings={"vertical_strategy":"lines","horizontal_strategy":"lines"}). Crop: page.crop((x0, y0, x1, y1)) — extract a bounding box region. page.within_bbox((x0,y0,x1,y1)). Chars: page.chars — list of char dicts with x0 y0 fontname size. Rects: page.rects — horizontal/vertical lines. Images: page.images — embedded image metadata. Size: page.width, page.height. PDF properties: pdf.metadata. Debug: page.to_image().debug_tablefinder() — visualize detected table cells. page.to_image().save("debug.png"). Pandas: import pandas as pd; pd.DataFrame(table[1:], columns=table[0]). Batch: for path in paths: with pdfplumber.open(path) as pdf:. Claude Code generates pdfplumber extractors, table parsers, and region croppers.

CLAUDE.md for pdfplumber

## pdfplumber Stack
- Version: pdfplumber >= 0.11 | pip install pdfplumber
- Open: with pdfplumber.open("file.pdf") as pdf: for page in pdf.pages:
- Text: page.extract_text() | extract_text_lines() | extract_words()
- Tables: page.extract_table() → list of rows | extract_tables() → all tables
- Table tuning: table_settings={"vertical_strategy":"lines","horizontal_strategy":"text"}
- Crop: page.crop((x0,y0,x1,y1)).extract_text() — extract region
- Pandas: pd.DataFrame(table[1:], columns=table[0]) from extract_table()

pdfplumber Extraction Pipeline

# app/pdf_extract.py — pdfplumber text, table, and region extraction
from __future__ import annotations

import io
from pathlib import Path
from typing import Any

import pdfplumber


# ─────────────────────────────────────────────────────────────────────────────
# 1. Text extraction
# ─────────────────────────────────────────────────────────────────────────────

def extract_all_text(source: str | Path | bytes) -> str:
    """
    Extract text from all pages.
    pdfplumber uses pdfminer under the hood for layout-aware positioning.
    extract_text() respects column and line breaks better than pypdf.
    """
    with _open(source) as pdf:
        return "\n\n".join(
            page.extract_text() or ""
            for page in pdf.pages
        )


def extract_page_text(source: str | Path | bytes) -> list[str]:
    """Return text per page as a list."""
    with _open(source) as pdf:
        return [(page.extract_text() or "") for page in pdf.pages]


def extract_words(source: str | Path | bytes, page_num: int = 0) -> list[dict]:
    """
    Return word-level bounding boxes.
    Each word: {"text", "x0", "y0", "x1", "y1", "doctop", "bottom"}.
    Useful for understanding layout and position of text elements.
    """
    with _open(source) as pdf:
        return pdf.pages[page_num].extract_words(
            x_tolerance=3,
            y_tolerance=3,
            keep_blank_chars=False,
        )


def extract_chars(source: str | Path | bytes, page_num: int = 0) -> list[dict]:
    """
    Return character-level detail: x0 y0 x1 y1 text fontname size color.
    The most granular extraction — useful for font analysis or OCR post-processing.
    """
    with _open(source) as pdf:
        return pdf.pages[page_num].chars


# ─────────────────────────────────────────────────────────────────────────────
# 2. Table extraction
# ─────────────────────────────────────────────────────────────────────────────

TABLE_SETTINGS_DEFAULT = {
    "vertical_strategy":   "lines",   # use visible lines to detect columns
    "horizontal_strategy": "lines",   # use visible lines to detect rows
    "snap_tolerance":      3,
    "join_tolerance":      3,
    "edge_min_length":     3,
    "min_words_vertical":  3,
    "min_words_horizontal": 1,
}

TABLE_SETTINGS_TEXT = {
    "vertical_strategy":   "text",    # use whitespace gaps between text columns
    "horizontal_strategy": "text",    # use vertical gaps between text rows
    "snap_tolerance":      3,
    "join_tolerance":      3,
    "edge_min_length":     3,
}


def extract_first_table(
    source: str | Path | bytes,
    page_num: int = 0,
    use_text_strategy: bool = False,
) -> list[list[str | None]]:
    """
    Extract the first table from a page.
    Returns list of rows, each row is list of cell strings.
    use_text_strategy=True for tables without visible borders (whitespace-delimited).
    """
    settings = TABLE_SETTINGS_TEXT if use_text_strategy else TABLE_SETTINGS_DEFAULT
    with _open(source) as pdf:
        return pdf.pages[page_num].extract_table(table_settings=settings) or []


def extract_all_tables(
    source: str | Path | bytes,
    page_num: int = 0,
) -> list[list[list[str | None]]]:
    """Extract all tables from the given page — returns list of tables."""
    with _open(source) as pdf:
        return pdf.pages[page_num].extract_tables(TABLE_SETTINGS_DEFAULT)


def extract_tables_all_pages(
    source: str | Path | bytes,
) -> dict[int, list[list[list[str | None]]]]:
    """Extract all tables on every page. Returns {page_num: [table, ...]}."""
    result = {}
    with _open(source) as pdf:
        for i, page in enumerate(pdf.pages):
            tables = page.extract_tables(TABLE_SETTINGS_DEFAULT)
            if tables:
                result[i] = tables
    return result


def table_to_dicts(table: list[list[str | None]]) -> list[dict[str, str]]:
    """
    Convert a table (list of rows, first row = headers) to list of dicts.
    Pairs with pandas: pd.DataFrame(table_to_dicts(table)).
    """
    if not table or len(table) < 2:
        return []
    headers = [str(h or "").strip() for h in table[0]]
    return [
        {headers[j]: str(row[j] or "").strip() for j in range(len(headers))}
        for row in table[1:]
    ]


def table_to_dataframe(table: list[list[str | None]]):
    """Convert extracted table to a pandas DataFrame."""
    import pandas as pd
    if not table:
        return pd.DataFrame()
    headers = [str(h or "").strip() or f"col_{i}" for i, h in enumerate(table[0])]
    data    = [[str(cell or "").strip() for cell in row] for row in table[1:]]
    return pd.DataFrame(data, columns=headers)


# ─────────────────────────────────────────────────────────────────────────────
# 3. Region / bounding box extraction
# ─────────────────────────────────────────────────────────────────────────────

def extract_region(
    source: str | Path | bytes,
    bbox: tuple[float, float, float, float],
    page_num: int = 0,
) -> str:
    """
    Extract text from a specific rectangular region.
    bbox = (x0, y0, x1, y1) in PDF coordinate system (origin = bottom-left).
    crop() returns a child page object — all extraction methods still work on it.
    """
    with _open(source) as pdf:
        region = pdf.pages[page_num].crop(bbox)
        return region.extract_text() or ""


def extract_header_footer(
    source: str | Path | bytes,
    header_height: float = 50,
    footer_height: float = 50,
) -> list[dict[str, str]]:
    """
    Extract header and footer text from each page by cropping top and bottom strips.
    Useful for identifying page numbers, document titles, or section headers.
    """
    results = []
    with _open(source) as pdf:
        for i, page in enumerate(pdf.pages):
            h       = page.height
            w       = page.width
            header  = page.crop((0, 0, w, header_height)).extract_text() or ""
            footer  = page.crop((0, h - footer_height, w, h)).extract_text() or ""
            results.append({"page": i, "header": header.strip(), "footer": footer.strip()})
    return results


def find_text_position(
    source: str | Path | bytes,
    search_text: str,
    page_num: int = 0,
) -> list[dict[str, Any]]:
    """
    Find all occurrences of a string and return their bounding boxes.
    Uses extract_words() to locate the search term's pixel coordinates.
    """
    with _open(source) as pdf:
        words = pdf.pages[page_num].extract_words()
    matches = [
        {"text": w["text"], "x0": w["x0"], "y0": w["top"], "x1": w["x1"], "y1": w["bottom"]}
        for w in words
        if search_text.lower() in w["text"].lower()
    ]
    return matches


# ─────────────────────────────────────────────────────────────────────────────
# 4. Metadata and structure
# ─────────────────────────────────────────────────────────────────────────────

def get_pdf_info(source: str | Path | bytes) -> dict[str, Any]:
    """Return page count, dimensions, and metadata."""
    with _open(source) as pdf:
        meta = pdf.metadata or {}
        pages_info = [
            {"page": i, "width": p.width, "height": p.height}
            for i, p in enumerate(pdf.pages)
        ]
        return {
            "pages":    len(pdf.pages),
            "metadata": {k: str(v) for k, v in meta.items()},
            "sizes":    pages_info,
        }


# ─────────────────────────────────────────────────────────────────────────────
# 5. Batch processing
# ─────────────────────────────────────────────────────────────────────────────

def batch_extract_tables(
    paths: list[str | Path],
) -> list[dict[str, Any]]:
    """
    Extract tables from a list of PDF files.
    Returns [{"file", "page", "table_index", "data": list_of_dicts}].
    """
    results = []
    for path in paths:
        try:
            tables_by_page = extract_tables_all_pages(path)
            for page_num, tables in tables_by_page.items():
                for ti, table in enumerate(tables):
                    results.append({
                        "file":        Path(path).name,
                        "page":        page_num,
                        "table_index": ti,
                        "rows":        len(table) - 1,
                        "cols":        len(table[0]) if table else 0,
                        "data":        table_to_dicts(table),
                    })
        except Exception as e:
            results.append({"file": Path(path).name, "error": str(e)})
    return results


def _open(source):
    if isinstance(source, bytes):
        return pdfplumber.open(io.BytesIO(source))
    return pdfplumber.open(str(source))


# ─────────────────────────────────────────────────────────────────────────────
# Demo — creates a test PDF with fpdf2 then extracts from it
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    try:
        from fpdf import FPDF

        def _make_table_pdf() -> bytes:
            pdf = FPDF()
            pdf.add_page()
            pdf.set_font("Helvetica", "B", 14)
            pdf.cell(0, 10, "Sales Report", align="C")
            pdf.ln(12)

            cols = ["Product", "Units", "Revenue"]
            col_w = [80, 40, 60]
            rows_data = [
                ["Widget A",  "150", "$3,750"],
                ["Gadget B",  "42",  "$8,400"],
                ["Module C",  "300", "$15,000"],
            ]

            pdf.set_font("Helvetica", "B", 11)
            pdf.set_fill_color(50, 50, 50)
            pdf.set_text_color(255, 255, 255)
            for w, h in zip(col_w, cols):
                pdf.cell(w, 8, h, border=1, fill=True, align="C")
            pdf.ln()

            pdf.set_font("Helvetica", size=10)
            pdf.set_text_color(0, 0, 0)
            for r in rows_data:
                for w, c in zip(col_w, r):
                    pdf.cell(w, 7, c, border=1, align="C")
                pdf.ln()
            return pdf.output()

        pdf_bytes = _make_table_pdf()
        print(f"Created test PDF: {len(pdf_bytes):,} bytes")

        print("\n=== Text extraction ===")
        text = extract_all_text(pdf_bytes)
        print(text[:200])

        print("\n=== Table extraction ===")
        table = extract_first_table(pdf_bytes, use_text_strategy=False)
        if table:
            for row in table:
                print(f"  {row}")
            print(f"\n  As dicts: {table_to_dicts(table)[:2]}")
        else:
            # Text strategy fallback
            table = extract_first_table(pdf_bytes, use_text_strategy=True)
            for row in (table or []):
                print(f"  {row}")

        print("\n=== PDF info ===")
        info = get_pdf_info(pdf_bytes)
        print(f"  Pages: {info['pages']}")
        for size in info["sizes"]:
            print(f"  Page {size['page']}: {size['width']:.0f}×{size['height']:.0f}")

    except ImportError:
        print("fpdf2 not installed — install with: pip install fpdf2")

For the pypdf alternative — pypdf’s extract_text() is faster and sufficient for continuous body text, but pdfplumber is the correct tool when you need tables: extract_table() detects cell boundaries from visible lines or text alignment and returns a list[list[str]] ready to pass to pd.DataFrame(), while pypdf has no table detection at all. For the tabula-py / camelot alternative — tabula-py wraps the Java-based Tabula library (requires JVM) and camelot uses OpenCV for lattice/stream table detection; pdfplumber is a pure-Python solution that handles the majority of PDF table layouts without Java or OpenCV, making it easier to install in containers and serverless functions. The Claude Skills 360 bundle includes pdfplumber skill sets covering pdfplumber.open() context manager, page.extract_text() and extract_words() and extract_chars(), extract_table() with lines and text strategies, extract_tables() for all tables on a page, TABLE_SETTINGS_DEFAULT and TABLE_SETTINGS_TEXT configurations, table_to_dicts() and table_to_dataframe() converters, crop() bounding box region extraction, extract_header_footer() for page template regions, find_text_position() word search, batch_extract_tables() for multi-file processing, and get_pdf_info() metadata reader. Start with the free tier to try PDF table extraction pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free