Download All Outlook Emails and Attachments With Python
Python reaches Outlook through its COM interface with pywin32, so any account already set up in classic Outlook for Windows works, Gmail included. The script opens a folder, walks every message and saves each body and its attachments into its own subfolder.
Requirements
You need Windows, classic Outlook for Windows with your accounts set up, and the pywin32 package. The new Outlook for Windows has no COM interface, so the script does not work with it. Outlook on a Mac or on the web is not supported either.
Install pywin32 from a terminal:
pip install pywin32
Connect to Outlook
The script imports Path from pathlib (part of Python) for the file paths and win32com.client for Outlook, creates an Output folder next to where it runs, and opens the Inbox through Outlook’s MAPI namespace.
from pathlib import Path #core python module
import win32com.client #pip install pywin32
parents=True creates missing parent folders, and exist_ok=True lets the script run again without an error when the folder already exists.
# Create output folder
output_dir = Path.cwd() / "Output"
output_dir.mkdir(parents=True, exist_ok=True)
Dispatch("Outlook.Application") connects to Outlook, and GetNamespace("MAPI") gives access to its mail folders.
# Connect to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# Connect to folder
inbox = outlook.GetDefaultFolder(6)
# https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders
# DeletedItems=3, Outbox=4, SentMail=5, Inbox=6, Drafts=16, FolderJunk=23
GetDefaultFolder takes a number for each built-in folder: 6 is the Inbox, 5 Sent Items, 3 Deleted Items. The comment in the code lists the common ones, and the Microsoft page in the comment has the full list. inbox.Items then returns every message in that folder.
Save every message
For each message the script creates a folder named after the subject, writes the body to EMAIL_BODY.txt and saves every attachment next to it with SaveAsFile. Here is the complete script.
from pathlib import Path #core python module
import win32com.client #pip install pywin32
# Create output folder
output_dir = Path.cwd() / "Output"
output_dir.mkdir(parents=True, exist_ok=True)
# Connect to outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# Connect to folder
inbox = outlook.GetDefaultFolder(6)
# https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders
# DeletedItems=3, Outbox=4, SentMail=5, Inbox=6, Drafts=16, FolderJunk=23
# Get messages
messages = inbox.Items
for message in messages:
subject = message.Subject
body = message.body
attachments = message.Attachments
# Create separate folder for each message
target_folder = output_dir / str(subject)
target_folder.mkdir(parents=True, exist_ok=True)
# Write body to text file
Path(target_folder / "EMAIL_BODY.txt").write_text(str(body), encoding="utf-8")
# Save attachments
for attachment in attachments:
attachment.SaveAsFile(str(target_folder / str(attachment)))
Two details keep it from failing. The code wraps the path in str() because COM expects a plain string; passing a pathlib path can fail with an error that a WindowsPath cannot be converted. encoding="utf-8" keeps emojis and other special characters in an email body from stopping the script. The property names are not consistent in case (Subject, body, Attachments), so copy them as written.
Safe folder names
Subjects often contain characters Windows does not allow in folder names, such as a colon or a question mark, and two emails can share a subject. The repository version strips special characters from subjects and file names before it uses them as folder and file names, and adds a timestamp folder, so two emails with the same subject do not overwrite each other. It adds import datetime and import re at the top and replaces the loop with this:
for message in messages:
subject = message.Subject
body = message.body
attachments = message.Attachments
# Create separate folder for each message, exclude special characters and add a timestamp
current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
target_folder = output_dir / re.sub("[^0-9a-zA-Z]+", "", subject) / current_time
target_folder.mkdir(parents=True, exist_ok=True)
# Write body to text file
Path(target_folder / "EMAIL_BODY.txt").write_text(str(body), encoding="utf-8")
# Save attachments and exclude special characters
for attachment in attachments:
filename = re.sub(r"[^0-9a-zA-Z\.]+", "", attachment.FileName)
attachment.SaveAsFile(str(target_folder / filename))
The result
After a run, the Output folder holds one subfolder per message, each with EMAIL_BODY.txt and the attachments under their original names.

To process those files afterwards, see a loop through folders and subfolders.
Other accounts and folders
GetDefaultFolder only reaches the default mailbox. For another account, address it by the name shown in the Outlook folder pane, usually its email address, and then the subfolder. In my test the second account was codingisfun.testuser@gmail.com. Replace the GetDefaultFolder(6) line with:
inbox = outlook.Folders("codingisfun.testuser@gmail.com").Folders("Inbox")

The same chain of .Folders(...) calls reaches any subfolder, for example a folder you file invoices into. The COM connection also works the other way round: creating and emailing Excel files with Python and Outlook shows how to send messages with attachments.
