Skip to content

Instantly share code, notes, and snippets.

@sergiolucero
Created June 6, 2019 20:23
Show Gist options
  • Save sergiolucero/16ef496f9ec645a5436d7693f3289be7 to your computer and use it in GitHub Desktop.
Save sergiolucero/16ef496f9ec645a5436d7693f3289be7 to your computer and use it in GitHub Desktop.
parsing outlook folders
import os, time
from pprint import pprint
from collections import defaultdict
from win32com.client import Dispatch
t0 = time.time()
outlook=Dispatch("Outlook.Application").GetNamespace("MAPI")
#inbox = outlook.GetDefaultFolder("6")
confi = outlook.Folders(2).Folders(2) # found by trial and error
counts = defaultdict(int)
for aca in confi.Items:
ax = aca.Attachments
if ax.Count>0:
print(ax.Count,aca.Subject)
for att in ax:
if '.pdf' in att.DisplayName:
sender = aca.Sender()
counter = counts[sender]+1
csender = sender.replace(' ','')
csdir = os.path.join(os.getcwd(),'PDF',csender)
if not os.path.exists(csdir):
os.mkdir(csdir)
filename = str(counter)+'_'+att.DisplayName
ofn = os.path.join(os.getcwd(),'PDF',csender,filename) # overwrite?
#print(sender)
att.SaveAsFile(ofn)
counts[sender]+=1
pprint(counts)
print(sum(counts.values()))
print(time.time()-t0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment