Download Gmail purchase emails and PDFs, extract structured invoice fields with an LLM
Project description
gmail-invoice
Download purchase invoice emails and PDF attachments from Gmail (read-only).
For a given date, the tool searches Gmail's Purchases category, saves each email as JSON, downloads any PDF attachments, and uses an LLM to extract structured invoice fields into the same JSON file.
Install
pip install gmail-invoice
Setup
-
Create a Google Cloud OAuth desktop client with the Gmail API enabled and download
credentials.json. -
Create a config folder outside this project (so credentials are not in your repo or workspace):
mkdir -p ~/.config/gmail-invoice mv ~/Downloads/credentials.json ~/.config/gmail-invoice/
-
Copy the sample env file, paste your config folder path, and save as
.env:cp .env.example .env
# .env — do not commit; do not share with AI assistants CONFIG_DIR=/Users/you/.config/gmail-invoice # Optional: where to write invoice folders (default: ./invoices_YYYY_MM_DD) # GMAIL_INVOICE_OUTPUT_DIR=./invoices_2026_05_24
Replace
/Users/youwith your username (or use the full expanded path to~/.config/gmail-invoice). On first run, OAuth will createtoken.jsonin that config folder. -
Add LLM settings in your config folder for invoice parsing:
Anthropic (default) — sends PDFs natively and uses structured outputs:
cat > ~/.config/gmail-invoice/llm.env <<'EOF' LLM_PROVIDER=anthropic LLM_API_KEY=sk-ant-api03-your-key-here LLM_MODEL=claude-sonnet-4-6 EOF
OpenAI-compatible (OpenAI, Groq, Together, Ollama shim, etc.):
cat > ~/.config/gmail-invoice/llm.env <<'EOF' LLM_PROVIDER=openai LLM_API_KEY=sk-your-key-here LLM_BASE_URL=https://api.openai.com/v1 LLM_MODEL=gpt-4o-mini EOF
Usage
# Today's purchase emails → ./invoices_YYYY_MM_DD/
gmail-invoice
# Specific date
gmail-invoice --date 2026-05-04
# Download only — skip LLM parsing
gmail-invoice --date 2026-05-04 --skip-parse
Equivalent module invocation:
python -m gmail_invoice --date 2026-05-04
Output
Each run creates a folder (default: invoices_YYYY_MM_DD/ in the current directory) containing:
email_YYYY_MM_DD_HH_MM_SS.json— sender, subject, body text, and parsedinvoiceobject*.pdf— invoice attachments with a timestamp suffix
Example invoice fields in the JSON:
{
"is_invoice": true,
"vendor_name": "Apple",
"invoice_number": "INV-2026-9912",
"invoice_date": "2026-05-22",
"currency": "USD",
"subtotal": 1199.0,
"tax": 100.0,
"total": 1299.0,
"line_items": [{"description": "MacBook", "quantity": 1, "unit_price": 1199.0, "amount": 1199.0}]
}
Development
git clone https://github.com/nish/gmail-invoice.git
cd gmail-invoice
pip install -e ".[dev]"
pytest
ruff check .
gmail-invoice --date 2026-05-04
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gmail_invoice-0.2.0.tar.gz.
File metadata
- Download URL: gmail_invoice-0.2.0.tar.gz
- Upload date:
- Size: 12.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
442280b2f688640d4a1f2e7d0050b7f46a688081acd85e7c7b764ce99a6f3c43
|
|
| MD5 |
1c74a8e7b4d2d072bbc509af634f1a66
|
|
| BLAKE2b-256 |
9da50d6f5c1b66f149cd906fb40c4034bfd65c2cc5a86556138aa49afc5a2849
|
File details
Details for the file gmail_invoice-0.2.0-py3-none-any.whl.
File metadata
- Download URL: gmail_invoice-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e44ed594780f92254057f31b54c79afb87ec9362caa444595ba22477ae2dd53c
|
|
| MD5 |
d9db7be681f71037ce4bd09812b70613
|
|
| BLAKE2b-256 |
5c9e5033d33bfaa63d4b4e123c7f58bad20fcc0309ec98a41a15c040d44f2fac
|