Extract structured data from PDF documents using multiple OCR and AI methods. Available as both a Streamlit web application and command-line scripts.
The easiest way to use this tool is through the Streamlit web interface.
- Python 3.12+
- All dependencies from
requirements.txt
-
Clone or navigate to the project directory:
cd extract-data-from-pdf -
Set up virtual environment (using uv - recommended):
# Install uv if not already installed curl -LsSf https://astral.sh/uv/install.sh | sh export PATH="$HOME/.local/bin:$PATH" # Create virtual environment uv venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies uv pip install -r requirements.txt
Or using pip:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt
-
Run the Streamlit app:
streamlit run streamlit_app.py
-
Access the app:
- The app will automatically open in your default browser
- Default URL:
http://localhost:8501 - If it doesn't open automatically, copy the URL shown in the terminal
β¨ User-Friendly Interface:
- Clean, modern UI with intuitive controls
- Real-time progress tracking during extraction
- Interactive results display
π Multiple Extraction Methods:
- EasyOCR - Fast CPU-based OCR, lightweight (~100MB models), quick setup, no API key needed
- PyTesseract - Google's Tesseract OCR engine, fast and lightweight for text-based PDFs
- Local Model - Run Chandra or other Hugging Face OCR models locally with automatic CPU/GPU capability detection
- HuggingFace - Cloud-based OCR via Inference API, requires API key
- Datalab API - High-accuracy OCR via API, best for structured documents (recommended)
- Gemini AI - Google's AI-powered extraction with context understanding and custom fields
- Deepseek AI - AI-powered extraction with vision capabilities and custom fields
π API Key Management:
- Test API keys before use
- Secure local storage in
.envfile - Support for multiple providers (HuggingFace, Datalab, Gemini, Deepseek)
π Flexible Field Selection:
- Pre-defined field templates
- Custom field extraction (define your own fields)
- Selective field export
πΎ Multiple Output Formats:
- CSV
- Excel (XLSX)
- JSON
- Markdown (MD)
- Select multiple formats at once
π€ Batch Processing:
- Upload single or multiple PDF files
- Process files sequentially with progress tracking
- Results aggregated across all files
-
Configure Extraction Method:
- Select from: EasyOCR, Local Model, HuggingFace, Datalab API, Gemini AI, or Deepseek AI
- For Local Model: Pick a local OCR model (Chandra, TrOCR, etc.) and toggle
Force CPU/Formatted Outputbased on the detected capabilities - For HuggingFace: Enter model name (default:
datalab-to/chandra) - For API methods: Enter and test your API keys
-
Set Up API Keys (if using API methods):
- Enter API keys in the sidebar
- Click "Test" buttons to verify keys work
- Click "πΎ Save All Keys Locally" to persist keys
β οΈ Important: Make a backup copy of the.envfile
-
Select Fields to Extract:
- Choose from default fields, or
- Click "β Add Custom Fields" to define your own fields
- Custom fields work best with Gemini AI and Deepseek AI
-
Choose Output Format(s):
- Select one or more formats: CSV, Excel, JSON, Markdown
β οΈ Required: At least one format must be selected
-
Upload PDF Files:
- Drag and drop or click to upload
- Supports single or multiple files
- File validation ensures PDFs are valid
-
Extract Data:
- Click "π Extract Data" button
- Watch real-time progress with detailed status updates
- View metrics: Files Processed, Rows Extracted, Success Rate
-
Download Results:
- View extracted data in interactive table
- Download in your selected format(s)
- Each format has its own download button
Datalab API (Recommended for OCR):
- Sign up at https://datalab.to
- Get your API key from the dashboard
- Free tier available
HuggingFace:
- Sign up at https://huggingface.co
- Go to Settings β Access Tokens
- Create a new token with read permissions
Gemini AI:
- Get API key from https://aistudio.google.com/app/apikey
- Create a new API key
Deepseek AI:
- Sign up at https://platform.deepseek.com
- Get your API key from the dashboard
For script-based automation, you can use the CLI scripts directly.
Make sure your virtual environment is activated:
source .venv/bin/activate # On Windows: .venv\Scripts\activate
python examples/extract_ec_data_cpu.py # CPU-only version
# or
python examples/extract_ec_data.py # Auto-detects GPU if available
# Use custom file:
python examples/extract_ec_data.py --file path/to/your/file.pdfCPU/GPU Selection:
- Local Model supports GPU acceleration (10-20x faster than CPU)
- GPU is automatically detected and used if available
- In Streamlit UI: Select "Local Model" and toggle "Force CPU Mode" if needed
- CPU mode is slower but more stable and doesn't require GPU drivers
Uses Datalab Marker API - no need to download models locally!
- Get your free API key from Datalab
- Create a
.envfile:echo "DATALAB_API_KEY=your_key_here" > .env
- Run the API script:
python examples/extract_ec_data_api.py # Or with custom file: python examples/extract_ec_data_api.py --file path/to/your/file.pdf
Benefits of Datalab API:
- No local model download (~18GB saved)
- Faster setup
- Works on any machine
- Free tier available
Documentation: https://documentation.datalab.to/docs/recipes/marker/conversion-api-overview
Uses the new HuggingFace Inference Providers API endpoint.
- Get your free API token from HuggingFace
- Create/update
.envfile:echo "HF_API_KEY=your_token_here" >> .env
- Run the HF API script:
python examples/extract_ec_data_hf_api.py # Or with custom file: python examples/extract_ec_data_hf_api.py --file path/to/your/file.pdf
Documentation: https://huggingface.co/docs/inference-providers
- Get your API key from Google AI Studio
- Create/update
.envfile:echo "GEMINI_API_KEY=your_key_here" >> .env
- Run the Gemini script:
python examples/extract_ec_data_gemini.py
- Process PDF file(s) from specified directories or files
- Extract text using OCR (local model or API)
- Parse table rows that contain relevant information
- Export results to CSV and Excel files
The system now supports field presets for common document types. By default, the Encumbrance Certificate preset is used, which extracts:
filename- Source PDF filenameSr.No- Serial numberDocument No.& Year- Document number and yearName of Executant(s)- Name(s) of executant(s)Name of Claimant(s)- Name(s) of claimant(s)Survey No.- Survey numberPlot No.- Plot number (required field)
Available Presets:
- Encumbrance Certificate (default) - For Tamil Nadu Encumbrance Certificates
- Invoice Document - For invoice extraction (Invoice Number, Date, Vendor, Amount, Tax, Total, etc.)
- Receipt Document - For receipt extraction (Receipt Number, Date, Merchant, Items, Total Amount, etc.)
In Streamlit UI:
- Select a preset from the "Field Preset" dropdown in the sidebar
- Each preset includes predefined fields optimized for that document type
- You can also create custom fields for any document type
Programmatically:
from field_presets import get_preset_fields, get_field_preset, register_field_preset
from utils import get_default_fields
# Get fields from a preset
fields = get_preset_fields("encumbrance") # Returns list of field names
# Get full preset configuration
preset = get_field_preset("invoice") # Returns dict with fields, required_fields, etc.
# Use preset-aware default fields
fields = get_default_fields("encumbrance") # Includes 'filename' automatically
# Register your own custom preset
register_field_preset(
preset_name="my_document",
fields=["Field1", "Field2", "Field3"],
required_fields=["Field1"],
name="My Custom Document",
description="Custom fields for my document type"
)You can define custom fields in the Streamlit app or via the API for any document type. Custom fields work best with AI-based extraction methods (Gemini, Deepseek). See the API documentation for details on registering your own presets.
CLI Scripts:
*_ocr_text.txt: Raw OCR text for debugging*_extracted.csv: Extracted data in CSV format*_extracted.xlsx: Extracted data in Excel format
Streamlit App:
- Download in selected format(s): CSV, Excel, JSON, or Markdown
- Results shown in interactive table before download
We welcome contributions! Please see CONTRIBUTING.md for guidelines on how to contribute to this project.
See requirements.txt for complete dependency list. Key dependencies:
streamlit>=1.28.0- Web application frameworkpandas>=2.0.0- Data manipulationopenpyxl>=3.1.0- Excel file supporteasyocr>=1.7.0- EasyOCR librarypytesseract>=0.3.10- PyTesseract wrapperpdf2image>=1.16.3- PDF to image conversiongoogle-generativeai>=0.3.0- Gemini AI supportrequests>=2.31.0- API requestspython-dotenv>=1.0.0- Environment variable management
For PyTesseract: You need to install Tesseract OCR engine separately:
-
Linux (Debian/Ubuntu):
sudo apt-get install tesseract-ocr
-
macOS:
brew install tesseract
-
Windows: Download installer from: https://github.com/UB-Mannheim/tesseract/wiki
For pdf2image (converting PDFs to images):
- Linux: Requires
poppler-utilssudo apt-get install poppler-utils
- macOS:
brew install poppler
- Windows: Download poppler from: https://github.com/oschwartz10612/poppler-windows/releases
App won't start:
# Make sure dependencies are installed
pip install -r requirements.txt
# Check if port 8501 is in use
# Change port if needed:
streamlit run streamlit_app.py --server.port 8502API key not working:
- Use the "Test" buttons in the sidebar to verify keys
- Check that keys are saved in
.envfile - Ensure
.envfile is in the project root directory
No results extracted:
- Try a different extraction method
- For AI methods (Gemini/Deepseek), ensure API keys are valid
- Check PDF file quality - some PDFs may require better OCR
See TROUBLESHOOTING.md and MODEL_LOADING_ISSUES.md for detailed troubleshooting guides.
See LICENSE file for details.
Contributions are welcome! Please see CONTRIBUTING.md for detailed guidelines on how to contribute to this project.
API.md- Complete API documentation for all core modulesDEVELOPMENT.md- Development setup, code quality tools, and workflowsRELEASE.md- Release process and version management guideCHANGELOG.md- Release history and changesGPU_SETUP.md- GPU configuration guideMODEL_LOADING_ISSUES.md- Troubleshooting model loadingTROUBLESHOOTING.md- General troubleshooting guideCONTRIBUTING.md- Contribution guidelines and coding standards
For detailed API documentation, see API.md. The API documentation includes:
- Function signatures and parameters
- Return value descriptions
- Error handling information
- Usage examples
- Best practices
Quick API Example:
from extraction_service import extract_data
api_keys = {'gemini': 'your-api-key'}
rows = extract_data(
'document.pdf',
method='gemini',
api_keys=api_keys,
custom_fields=['Village Name', 'Plot No.']
)