SHARE
TWEET

Untitled

a guest Jan 21st, 2020 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import win32com.client  # You need to install the module: "pypiwin32"
  2.  
  3.  
  4. Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")  # Opens Microsoft Outlook
  5.  
  6.  
  7. def getFolderIndex(COMObjectFolders, folderName):
  8.     i = 1  # Folder index. Used to access the right main folder.
  9.     while True:
  10.         try:
  11.             if COMObjectFolders.Item(i).Name == folderName:
  12.                 break  # At this point we have the right index
  13.             else:
  14.                 i += 1  # Next Folder
  15.         except:
  16.             i = None  # We did not find out our folder index
  17.             break
  18.     return i
  19.  
  20.  
  21. def getFolder(COMObjectFolders, folderName):  # Wrapper
  22.     return COMObjectFolders.Item(getFolderIndex(COMObjectFolders, folderName))
  23.  
  24.  
  25. myMainFolder = getFolder(Outlook.Folders, "[[your email (imap) or main folder name]]")
  26. mySubFolder = getFolder(myMainFolder.Folders, "[[Subfolder such as Inbox or whatever]]")
  27.  
  28. myEmails = mySubFolder.Items
  29.  
  30. def extractAttachments(COMObjectMailItems, howMany = "all"):
  31.     # howMany parameter controls how many last received emails to go through
  32.     if howMany == "all":
  33.         numberEmails = COMObjectMailItems.Count  # Used to loop by the number of emails in the folder
  34.         lastEmail = 0  # The index-1 of the email to stop at. Used in range().
  35.     else:
  36.         numberEmails = COMObjectMailItems.Count
  37.         lastEmail = numberEmails - howMany
  38.     outputAttachments = dict()  # Used for output. Which is a dict {'filename-date' : attachment}
  39.     for i in range(numberEmails, lastEmail, -1):
  40.         message = COMObjectMailItems.Item(i)
  41.         attachmentsList = message.Attachments
  42.         if attachmentsList.Count > 0:
  43.             n = attachmentsList.Count
  44.             for j in range(1, n+1):
  45.                 outputAttachments[str(attachmentsList.Item(j)) + "_" + message.CreationTime.Format()] =\
  46.                     attachmentsList.Item(j)
  47.     return(outputAttachments)
  48.  
  49. savePath = 'C:\\Users\\[[user name here, or change path competely]]\\Desktop\\Inbox'  # Where to save attachments
  50.  
  51. myAttachments = extractAttachments(myEmails, howMany=1)  # howMany = 1 was used to check the most recent email received
  52.  
  53. def saveAttachments(attachments, path):
  54.     for key, attachment in attachments.items():
  55.         attachment.SaveAsFile(path + '\\' + str(attachment))
  56.  
  57. saveAttachments(myAttachments, savePath)  # Has an obvious side effect - Saves files to disk
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top