title: How to download email attachments from outlook using Python
body: <p>I tried to use this script I found <a href="https://stackoverflow.com/questions/39656433/how-to-download-outlook-attachment-from-python-script">here</a>. What I want to do is given either some subject line or email I will download and save the attachment.</p>
  
  <p>This is the code I used:</p>
  
  <pre><code>import datetime
  import os
  import win32com.client
  
  
  path = os.path.expanduser("//cottonwood//users///MyDocuments//Projects//outlook crawler")
  
  today = datetime.date.today()
  
  outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
  inbox = outlook.GetDefaultFolder(6) 
  messages = inbox.Items
  
  def saveattachemnts(subject):
      for message in messages:
          if message.Subject == subject and message.Unread or message.Senton.date() == today:
              # body_content = message.body
              attachments = message.Attachments
              attachment = attachments.Item(1)
              for attachment in message.Attachments:
                  attachment.SaveAsFile(os.path.join(path, str(attachment)))
                  if message.Subject == subject and message.Unread:
                      message.Unread = False
                  break
  
  saveattachemnts("Snarf")
  </code></pre>
  
  <p>I am getting this error:</p>
  
  <pre><code>  File "&lt;COMObject &lt;unknown&gt;&gt;", line 2, in Item
  pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)
  </code></pre>
  
  <p>The Outlook email is a work email and it is Microsoft Outlook 2010. </p>
  
  <p>My question is how do I download and save attachments from Microsoft Outlook.</p>