0

I have this script, which has previously worked for other emails, to download attachments:

import win32com.client as win
import xlrd

outlook = win.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items

subject = 'Email w/Attachment'

attachment1 = 'Attachment - 20160715.xls'

for msg in all_inbox:
    if msg.subject == subject:
        break
for att in msg.Attachments:    
    if att.FileName == attachment1:
        break
att.SaveAsFile('L:\\My Documents\\Desktop\\' + attachment1)

workbook = xlrd.open_workbook('L:\\My Documents\\Desktop\\' + attachment1)

However, when I try and open the file using xlrd reader (or with pandas)I get this:

raise XLRDError('Unsupported format, or corrupt file: ' + msg)

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\nVisit '

Can anyone explain what's gone wrong here?

Is there a way I can open the attachment, without saving it, and just copy a worksheet and save that copy as a .csv file instead?

Thank you

Vladimir Nabokov
  • 265
  • 1
  • 2
  • 7
  • did you try changing the file extension from .xls (older, possibly unsupported) to .xlsx (new excel extension)? – Sam Jul 18 '16 at 15:13
  • Yes. Didn't work. Any other ideas? – Vladimir Nabokov Jul 18 '16 at 15:19
  • Does the physical file actually exist in specified path and can you open it directly? Your use of `break` might be a concern. Try printing, `print(att.FileName)`, just after or before `SaveAsFile()` to see if att is correct attachment. – Parfait Jul 18 '16 at 17:10

1 Answers1

0

take a look at this question. It's possible the file you are trying to download is not a true excel file, but a csv saved as an .xls file. The evidence is the error message Expected BOF record; found b'\r\nVisit '. I think an excel file would start with <?xml or something to that effect. You could get around it with a try/catch:

import pandas as pd
try: #try to read it as a .xls file
    workbook = xlrd.open_workbook(path)

except XLRDError: #if fails, read as csv
    workbook = pd.read_csv(path)
Community
  • 1
  • 1
Sam
  • 4,000
  • 20
  • 27