title: getting specific xlsx attachment from outlook and saving it on my windows computer
body: <p>I hope you are well.</p>
  <p>I have been doing some python practice with web scraping and have come across the win32com library and have been really struggling to get the attachment from my outlook email ( it is just the one)! I have managed to view all of the emails by the following code. I was wondering if you could help me with getting the attachment file called lets say &quot;data_bay1.xlsx&quot; as i am stumped on the errors. I can so far get the data from the email but i cannot get the attachment xlsx file and the attachment part of the code when i try to run it gives me errors.  Please see what I have done so far and I hope any of you can help. I am using anaconda and windows. Thank you in advance! Kind regards, Lily</p>
  <pre><code>   import win32com.client
     
  #connecting python to outlook
  
  outlook = win32com.client.Dispatch(&quot;Outlook.Application&quot;).GetNamespace(&quot;MAPI&quot;)
   
  #connecting to our inbox
  
  inbox = outlook.GetDefaultFolder(6)
  (inbox)
  
  # here we are ensuring the indexes exist so we can grab data
  
  outlook=win32com.client.Dispatch(&quot;Outlook.Application&quot;).GetNamespace(&quot;MAPI&quot;)
  for i in range(50):
   try:
     box = outlook.GetDefaultFolder(i)
     name = box.Name
     print(i, name)
   except:
     pass
  
  messages = inbox.items
  
  messages = messages.GetFirst() earlier
  # get the last email
  #message = messages.GetLast()
  (message)
  
  #to loop through the email in the inbox 
  
  while True:
   try:
     print(message.subject) # get the subject of the email
     # if you use messages.GetFirst() earlier
     message = messages.GetNext() 
     # if you use messages.GetPrevious() earlier 
     #message = messages.GetPrevious()
   except:
     # if you use messages.GetFirst() earlier
     message = messages.GetNext() 
     # if you use messages.GetPrevious() earlier 
     #message = messages.GetPrevious()
  
  
  
  # get the attachment
  
   attachments = message.Attachments# return the first item in attachments
   attachment = attachments.Item(1)
   # the name of attachment file      
   attachment_name = str(attachment).lower()
   attachment.SaveASFile(path+ 'C:\Users\lily\OneDrive - Dataenv\Documents + attachment_data_bay1)
  
  
   
   
  </code></pre>