Prompt
How do I turn unstructured PDFs into rows and columns?
Latest observation
To turn unstructured PDFs into rows and columns, you typically need a PDF extraction pipeline rather than a single tool. The best approach depends on what kind of PDF you have:
1) First identify the PDF type
A. Text-based PDFs
The PDF contains selectable text. These are easier to extract.
- Use tools like:
- Tabula
- Camelot
- pdfplumber
- PyMuPDF / fitz
B. Scanned/image PDFs
The PDF is just images of pages. You need OCR first.
- Use:
- Tesseract
- AWS Textract
- Google Document AI
- Azure Form Recognizer
- Adobe PDF Extract API
2) Common extraction workflows
If tables are already visible in the PDF
Use a table extractor:
- Tabula: good for simple tables
- Camelot: good for structured tables, supports lattice/stream modes
- pdfplumber: more flexible for custom parsing
Example with Camelot:
import camelot
tables = camelot.read_pdf("file.pdf", pages="1-end", flavor="stream")
tables[0].df.to_csv("output.csv", index=False)
If the PDF is messy or unstructured
You usually need a pipeline like:
- Extract text
- Detect layout (headers, paragraphs, tables, columns)
- Use rules or ML to identify records
- Normalize into a schema
Example columns:date,vendor,amount,description
Tools:
- pdfplumber for coordinates and text blocks
- LayoutParser for document layout detection
- OCR + document AI for scans
3) If the PDF is invoices, statements, or forms
These are often best handled by document AI tools:
- AWS Textract
- Google Document AI
- Azure Form Recognizer
They can return:
- key-value pairs
- tables
- bounding boxes
- extracted text by page
This is often much easier than writing custom logic.
4) If the document is highly unstructured
For reports, research papers, legal docs, etc.:
- Extract text with positions
- Split by section headers
- Use regex or NLP to pull fields
- Manually validate and clean
You may need:
- LLMs to classify sections or map text into structured fields
- post-processing scripts to create rows
5) Example: extracting text and turning repeated patterns into rows
If your PDF has repeated records, you can often use regex.
Example:
import re
import pandas as pd
from pdfplumber import open as pdfopen
rows = []
with pdfopen("file.pdf") as pdf:
for page in pdf.pages:
text = page.extract_text()
matches = re.findall(r"(\d{2}/\d{2}/\d{4})\s+(.+?)\s+\$?([\d,]+\.\d{2})", text)
for date, desc, amount in matches:
rows.append([date, desc, amount])
df = pd.DataFrame(rows, columns=["date", "description", "amount"])
df.to_csv("output.csv", index=False)
6) Best practice
A practical approach is:
- Text PDF + simple table → Camelot or Tabula
- Text PDF + complicated layout → pdfplumber + custom parsing
- Scanned PDF → OCR or Document AI
- Large-scale production → cloud document extraction service
7) Important note
“Unstructured” usually means there’s no perfect automatic solution. You often need:
- a bit of manual schema design
- cleanup rules
- validation of the final rows/columns
If you want, I can help you choose the best method for your specific PDF if you describe:
- whether it’s scanned or selectable text
- whether it contains tables, invoices, or free-form text
- a sample page or screenshot structure