agentleFS
THEROCKSSSTHEROCKSSS/hermes-skills-portfolio/skills/pdf-extract/SKILL.md

pdf-extract

Skill1 starsChanged 4 months agoMITView on GitHub

Use when the user wants to extract text, tables, or images from a PDF file — including scanned/image-only PDFs needing OCR — or asks to "read this PDF", "extract text from PDF", "what's in this PDF", or to pull tables/images out of one.

  • Installs packages

3 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If get_text() returns empty or near-empty, the PDF is likely scanned images.

    line 107
  2. Distinctive:

    If text is sparse (< 50 chars/page average), fall back to OCR

    line 144
  3. Distinctive:

    If the user needs tables, use pdfplumber

    line 145
  4. Distinctive:

    If the user needs images, extract with pymupdf's image API

    line 146

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

pdf-extract

Overview

Extract text, images, and tables from PDF files using open-source Python libraries. The agent handles PDF parsing, OCR fallback for scanned documents, and structured output.

When to Use

  • The user wants to extract text from a PDF.
  • The user has a scanned PDF that needs OCR.
  • The user wants to pull tables or images out of a PDF.
  • The user says "read this PDF", "extract text from PDF", or "what's in this PDF".

Prerequisites

pip install pymupdf pdfplumber pillow
# For OCR fallback:
pip install pytesseract
# Also install tesseract-ocr system package:
# Linux: apt install tesseract-ocr
# macOS: brew install tesseract
# Windows: download from https://github.com/UB-Mannheim/tesseract/wiki

Text Extraction

Basic text extraction (pymupdf)

import fitz  # pymupdf

def extract_text(pdf_path: str) -> str:
    doc = fitz.open(pdf_path)
    text = []
    for page in doc:
        text.append(page.get_text())
    return "\n".join(text)

With page numbers

def extract_text_with_pages(pdf_path: str) -> list[dict]:
    doc = fitz.open(pdf_path)
    pages = []
    for i, page in enumerate(doc):
        pages.append({
            "page": i + 1,
            "text": page.get_text()
        })
    return pages

Table Extraction

import pdfplumber

def extract_tables(pdf_path: str) -> list:
    tables = []
    with pdfplumber.open(pdf_path) as pdf:
        for i, page in enumerate(pdf.pages):
            page_tables = page.extract_tables()
            for table in page_tables:
                tables.append({"page": i + 1, "rows": table})
    return tables

Image Extraction

import fitz
import os

def extract_images(pdf_path: str, output_dir: str = "./extracted_images"):
    os.makedirs(output_dir, exist_ok=True)
    doc = fitz.open(pdf_path)
    images = []
    for page_num, page in enumerate(doc):
        for img_index, img in enumerate(page.get_images(full=True)):
            xref = img[0]
            base_image = doc.extract_image(xref)
            image_bytes = base_image["image"]
            ext = base_image["ext"]
            filename = f"{output_dir}/page{page_num+1}_img{img_index+1}.{ext}"
            with open(filename, "wb") as f:
                f.write(image_bytes)
            images.append(filename)
    return images

OCR Fallback (for scanned PDFs)

If get_text() returns empty or near-empty, the PDF is likely scanned images. Use OCR:

import fitz
import pytesseract
from PIL import Image
import io

def extract_with_ocr(pdf_path: str) -> str:
    doc = fitz.open(pdf_path)
    text = []
    for page in doc:
        # Render page to image at 300 DPI
        pix = page.get_pixmap(dpi=300)
        img = Image.open(io.BytesIO(pix.tobytes("png")))
        page_text = pytesseract.image_to_string(img)
        text.append(page_text)
    return "\n".join(text)

Auto-detect: text vs scanned

def extract_pdf(pdf_path: str) -> str:
    doc = fitz.open(pdf_path)
    # Try direct text extraction
    total_text = "".join(page.get_text() for page in doc)
    # If less than 50 chars per page on average, use OCR
    if len(total_text) / len(doc) < 50:
        return extract_with_ocr(pdf_path)
    return total_text

Workflow

  1. Identify the PDF file path
  2. Try direct text extraction with pymupdf
  3. If text is sparse (< 50 chars/page average), fall back to OCR
  4. If the user needs tables, use pdfplumber
  5. If the user needs images, extract with pymupdf's image API
  6. Return structured output (text, tables, or image paths)

Common Pitfalls

  1. Scanned PDFs return empty text. get_text() returns "" for image-only PDFs — always check text length and fall back to OCR.
  2. OCR is slow. Rendering at 300 DPI and running tesseract takes 2-5 seconds per page — warn the user before running it on large PDFs.
  3. Encrypted PDFs fail to open. fitz.open() raises on password-protected PDFs — call doc.authenticate("password") first if the password is known.
  4. Table extraction quality varies. pdfplumber handles bordered tables well but struggles with borderless ones — check the output before trusting it.
  5. Large PDFs exhaust memory. A 500-page PDF loaded whole with pymupdf can use significant RAM — process pages one at a time if memory is constrained.
  6. Missing Tesseract language packs. Non-English PDFs need the matching pack (e.g. tesseract-ocr-fra) installed and lang='fra' passed to image_to_string, or OCR silently produces garbage text.

Verification Checklist

  • Checked average chars/page before deciding text-extraction vs. OCR
  • Extracted text/table/image count is consistent with the source PDF's page count
  • OCR output spot-checked for garbled text when a scanned PDF was processed
  • Correct Tesseract language pack used for non-English documents
  • Password-protected PDFs authenticated successfully before extraction was attempted
  • Output files (images, extracted text/tables) saved where the user expects them

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.