Prompt
How do I use OCR to read scanned documents and save the fields?
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
To use OCR for scanned documents and save fields, the typical workflow is:
1) Scan or collect the document
- Input can be PDF, TIFF, JPG, PNG, etc.
- If possible, scan at 300 DPI or higher for better OCR accuracy.
2) Preprocess the image
Improve OCR results by:
- Deskewing tilted pages
- Removing noise
- Increasing contrast
- Cropping unnecessary borders
- Converting to grayscale
3) Run OCR
Use an OCR engine to extract text from the image/PDF. Popular options:
- Tesseract (open source)
- Google Cloud Vision
- AWS Textract
- Azure Document Intelligence
- EasyOCR / PaddleOCR
4) Extract the fields you care about
Once you have text, map it to structured fields such as:
- Name
- Date
- Invoice number
- Total amount
- Address
Ways to do this:
- Template-based extraction: if all forms look the same
- Regex/parsing: for fields with predictable patterns
- ML/document AI: for variable layouts
5) Save the fields
Store the extracted data in:
- JSON
- CSV
- SQL database
- NoSQL database
- CRM/ERP system
Example with Python and Tesseract
Install
pip install pytesseract pillow pandas
You also need to install the Tesseract OCR engine on your system.
OCR a scanned image
from PIL import Image
import pytesseract
image = Image.open("scanned_doc.png")
text = pytesseract.image_to_string(image)
print(text)
Extract fields from the OCR text
Example for a simple invoice:
import re
fields = {}
match = re.search(r"Invoice No[:\s]*([A-Z0-9-]+)", text, re.IGNORECASE)
if match:
fields["invoice_number"] = match.group(1)
match = re.search(r"Total[:\s]*\$?([\d,]+\.\d{2})", text, re.IGNORECASE)
if match:
fields["total"] = match.group(1)
print(fields)
Save to JSON
import json
with open("output.json", "w") as f:
json.dump(fields, f, indent=2)
If your documents have a fixed layout
Use coordinates to read fields from specific regions:
- OCR each region separately
- Or use a document AI tool that returns text + bounding boxes
This works well for:
- Standard forms
- IDs
- Invoices with consistent templates
If your documents vary a lot
Use a document understanding service:
- AWS Textract for forms/tables
- Azure Document Intelligence
- Google Document AI
These can directly detect key-value pairs and tables.
Best practice
- Start with OCR + regex for a simple prototype
- Use document AI if accuracy/layout complexity is high
- Validate extracted fields before saving
- Keep the original document and OCR output for audit/debugging
If you want, I can give you:
- a Python script that processes PDFs and saves fields to CSV/JSON, or
- an example using AWS Textract / Azure / Google Document AI.