Skip to main content

LLM-powered codebook reader for exploratory data analysis

Project description

codebookpy

LLM-powered codebook reader for exploratory data analysis.

codebookpy lets you import a statistical codebook PDF into Python and look up variable descriptions, value codes, and data summaries alongside your DataFrame — all from a single function call.


Installation

pip install codebookpy

Install with your preferred LLM backend:

pip install "codebookpy[anthropic]"   # Claude (recommended)
pip install "codebookpy[openai]"      # OpenAI
pip install "codebookpy[all]"         # everything including OCR support

OCR support (for scanned/image-based PDFs)

If your codebook is a scanned PDF, install the OCR extras and Tesseract:

pip install "codebookpy[ocr]"

Then install the Tesseract engine:

  • Windows: download from UB-Mannheim
  • macOS: brew install tesseract
  • Linux: sudo apt install tesseract-ocr

Update TESSERACT_CMD at the top of core.py to match your install path if needed (Windows only).


Quick start

import pandas as pd
from codebookpy import Codebook, cb_lookup

# Load your data
df = pd.read_csv("my_data.csv")

# Parse the codebook — pass df so the LLM anchors to your actual column names
cb = Codebook(
    "my_codebook.pdf",
    anthropic_api_key="sk-ant-...",   # or openai_api_key="sk-..."
    df=df,
)

# Look up variables — codebook info only
cb_lookup(cb, ["AGE", "SEX", "RACE"])

# Look up variables — with DataFrame summary
cb_lookup(cb, ["AGE", "SEX", "RACE"], df=df, summarize_df=True)

API reference

Codebook(pdf_path, *, anthropic_api_key=None, openai_api_key=None, df=None, known_vars=None, verbose=True)

Parses a codebook PDF using an LLM backend. Pass either anthropic_api_key or openai_api_key (not both).

Parameter Type Description
pdf_path str | Path Path to the codebook PDF
anthropic_api_key str Anthropic API key
openai_api_key str OpenAI API key
df DataFrame If provided, all column names are used to anchor the LLM during parsing
known_vars list[str] Explicit variable list (overrides df if both provided)
verbose bool Print parsing progress (default True)

cb_lookup(codebook, var_list, df=None, summarize_df=False)

Look up variables and print formatted summaries to the console.

Parameter Type Description
codebook Codebook A parsed Codebook object
var_list list[str] Variable names to look up
df DataFrame Optional DataFrame for data summaries
summarize_df bool If True and df provided, prints dtype, missingness, and numeric/frequency/date summaries

How it works

  1. Text extractionpdfplumber extracts text from the PDF. If the PDF is malformed, pikepdf repairs it first. If it's a scanned image-based PDF, OCR kicks in automatically via pymupdf + pytesseract.
  2. Anchor-guided parsing — Your DataFrame column names are passed to the LLM so it searches for each variable by name rather than guessing. Variables are sent in batches of 10 with the most relevant codebook sections.
  3. Rate limiting — Requests are throttled automatically to stay within API rate limits.
  4. Lookupcb_lookup() queries the in-memory parsed result and renders a formatted panel per variable using rich.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codebookpy-0.1.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codebookpy-0.1.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file codebookpy-0.1.0.tar.gz.

File metadata

  • Download URL: codebookpy-0.1.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for codebookpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3725ba116cf8601a27f26d2e8cd1c676f38e3b4f33c3f9cc2ca409894d3d34fc
MD5 9d2dd2661029db4a0875dc36ab615574
BLAKE2b-256 50b83a592a5531006f75617a011d0fd7ceded6fd5decaa27068b9fcdfb4843e8

See more details on using hashes here.

File details

Details for the file codebookpy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codebookpy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for codebookpy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e78ea19d796c69645a4315054ad76d9ea78d628f5b331ee657be2f45df704057
MD5 c51bb43290401b9f2d4bbd5f72e0bd59
BLAKE2b-256 a59a34be8b9145585b9fb700ea1a1a0141e87270841699dd7e1db64bcd400e83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page