Export Data analyst emails to Excel
Four steps, one command, and a completeness check at the end — for mailbox metadata for analysis.
Step 1 — connect read-only
Use an app password, never the main password. The script opens the folder in read-only mode, so nothing is marked as read or deleted.
python mail_to_excel.py --host imap.example.com --user you@example.com --days 31 --fields date,sender,subject,thread,attachments --out data-analyst.xlsx
Step 2 — filter to one period
A month or a quarter, not «everything». Email metadata is an underused dataset: response times, senders, topics. A clean export is the first step.
Step 3 — keep only the columns that matter
| Column | Why it matters |
|---|---|
date | Message date in ISO 8601; sortable and unambiguous across time zones. |
sender | Who wrote in; used for triage and routing. |
subject | Message subject, MIME-decoded. |
thread | Thread id; the only reliable dedupe key for conversations. |
attachments | Number of attachments; a cheap filter for «has documents». |
Step 4 — verify completeness
- Run the same search in your mail client and note the message count.
- Compare it with the row count in the spreadsheet.
- If the numbers differ, the filter or the folder is wrong — fix it before using the file.
Example output
| date | sender | subject | thread | attachments |
|---|---|---|---|---|
| 2026-10-14 | ops@data.io | Weekly export | 23 | 2 |
Pitfalls
- Thread ids are missing — Export Message-ID and In-Reply-To headers; threading cannot be reconstructed reliably afterwards.
- Character encoding breaks analysis — Decode MIME headers and keep the file in UTF-8 from the start.
Provider notes
Gmail shows label copies; export one label to avoid counting the same message twice.
FAQ
What format should I use?
CSV with ISO dates and lowercase emails; Excel is for reading, CSV for pipelines.
Can I get message bodies?
Yes, but bodies are heavy and noisy; start with metadata and add bodies only for the analysis you need.
How many messages can it handle?
Thousands per run with a date filter; very large mailboxes are better exported month by month.
Will it change anything in the mailbox?
No. Read-only access is the default and should stay enabled.
Related guides
- Data analyst: columns and pitfallsThe compact overview for this role.
- Universal IMAP guideApp passwords, decoding and Excel output.