A professional Python application that automatically monitors Gmail for invoices, extracts data using LLM and vendor-specific parsers, and writes to Google Sheets.
- Gmail API + OAuth 2.0 authentication
- Automatic invoice email detection (Invoice, Receipt, Bill keywords)
- PDF attachment and email text extraction
- Dual extraction engines:
- Vendor-specific regex parsers (Home Depot, McMaster-Carr)
- LLM-based extraction via Ollama for unknown vendors
- Google Sheets integration for data storage
- 24/7 monitoring with configurable check intervals
- Scheduled processing (midnight & 7 AM)
- Python 3.10+
- Gmail account (Rutgers Gmail supported)
- Google Cloud project with Gmail API and Sheets API enabled
- Ollama (for LLM extraction)
-
Install dependencies:
pip install -r requirements.txt
-
Set up Google Cloud credentials:
- Download
credentials.jsonfrom Google Cloud Console - Place in
credentials/folder
- Download
-
Configure settings:
- Edit
src/config/settings.py - Set your
SPREADSHEET_ID
- Edit
-
Run the application:
python main.py
Invoice-Tracker/
├── main.py # Main entry point
├── requirements.txt # Python dependencies
├── README.md # Documentation
│
├── src/
│ ├── auth/
│ │ └── gmail_auth.py # Gmail & Sheets authentication
│ ├── config/
│ │ └── settings.py # Centralized configuration
│ ├── downloaders/
│ │ ├── bulk_downloader.py # Historical email downloader
│ │ └── monitor_downloader.py # Real-time email monitor
│ ├── processors/
│ │ ├── invoice_processor.py # Orchestrates parsing
│ │ ├── file_handler.py # PDF/TXT file operations
│ │ ├── llm_extractor.py # LLM-based extraction
│ │ └── vendor_parser.py # Vendor-specific parsers
│ ├── writers/
│ │ └── sheets_writer.py # Google Sheets writer
│ └── utils/
│ ├── file_utils.py # File utilities
│ └── date_utils.py # Date utilities
│
├── credentials/
│ ├── credentials.json # OAuth credentials (you provide)
│ └── token.json # Auth token (auto-generated)
│
└── data/
├── invoices/ # Current invoices
├── old_invoices/ # Historical invoices
└── processed_ids.json # Tracking file
- Go to Google Cloud Console
- Create new project → name it
invoice-tracker
- Enable Gmail API
- Enable Google Sheets API
- APIs & Services → OAuth consent screen
- User Type: External
- App name:
Invoice Tracker - Add your email as developer contact
- APIs & Services → Credentials
- Create Credentials → OAuth Client ID
- Application type: Desktop App
- Download JSON → rename to
credentials.json - Place in
credentials/folder
Edit src/config/settings.py:
# Your Google Sheets ID (from the URL)
SPREADSHEET_ID = 'your-spreadsheet-id-here'
# Check interval (seconds)
CHECK_INTERVAL_SECONDS = 60
# Ollama configuration
OLLAMA_MODEL = "gemma2:2b"
OLLAMA_URL = "http://localhost:11434/api/chat"Run the main application:
python main.py- Test Gmail Connection - Verify authentication
- Download Old Emails - Bulk download historical invoices
- Start Invoice Monitor - Download-only mode (no processing)
- Process Existing Invoices - Process downloaded invoices
- FULL AUTO (24/7) - Complete automation (backfill + monitor)
- Scheduled Check - Run at midnight & 7 AM daily
- Send yourself a test email with subject containing "Invoice"
- Attach a PDF or include invoice details in email body
- Run option 3 (Monitor) or 5 (Full Auto)
- Check your Google Sheet for extracted data
Each module has one clear purpose:
auth/- Authentication onlyconfig/- Configuration managementdownloaders/- Email downloadingprocessors/- Data extraction and processingwriters/- Data output to Google Sheetsutils/- Shared utilities
Gmail → Downloader → File Handler → Invoice Processor
↓
Vendor Parser or LLM Extractor
↓
Sheets Writer
Edit src/processors/vendor_parser.py:
@register("new_vendor")
def parse_new_vendor(text: str) -> dict:
# Your parsing logic
return {
"organizations": ["Vendor Name"],
"dates": [...],
"total_amount": [...],
...
}Update src/config/settings.py:
KNOWN_VENDORS = {
"vendor.com": "new_vendor",
...
}- Credentials: Never commit
credentials.jsonortoken.json - Data Files: Stored in
data/for easy management - Logs: All operations print status messages for debugging
- Duplicate Prevention: Uses Gmail thread IDs to avoid duplicates
Authentication errors:
- Delete
credentials/token.jsonand re-authenticate
No emails found:
- Check
src/config/settings.py→GMAIL_SEARCH_QUERY - Verify email keywords match your inbox
LLM extraction fails:
- Ensure Ollama is running:
ollama serve - Check model is installed:
ollama list
Google Sheets errors:
- Verify
SPREADSHEET_IDis correct - Ensure Sheets API is enabled
- Check sheet permissions
Internal use for Rutgers Solar Car team.