Learn how to find an Outlook folder by name with Python.
Automating business tasks in Microsoft Outlook can save you tons of time. Python is a great tool that can help you automate Outlook tasks, such as finding a monthly email report a coworker regularly sends.
Many users find it helpful to file Outlook emails in folders outside of their inbox. The code below will help you access these custom folders by name.
You can easily do this with the win32com.client package. You will need to iterate through Outlook Folders using a for loop in Python. The function below allows you to input a specific folder name you want to use further in your script. It then iterates through each Outlook folder and checks if the folder name matches the folder name you want to find. If there is a match, it will return the folder as an object.
First, import the win32com.client and os packages.
Python Code
import win32com.client
import os
#having issues installing win32com? try the below (without the hashtag)
#pip install pypiwin32
#pip install pywin32
#python -m pip install pypiwin32
Full code below:
Python Code
import win32com.client
import os
#uses win32com to access Outlook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#loops through all Outlook folders to find given folder_name
def get_folder_by_name(folder_name):
for folder in outlook.Folders.Item(1).Folders:
if folder.Name == folder_name:
print ('Awesome, ' + folder_name + ' was found!')
found_folder = folder
return found_folder
#run functions
folder_name = 'Monthly Reports' #name of the Outlook folder you need
folder_needed = find_folder(folder_name)
print(folder_needed.Name)
Leave a Reply