我有这个以前曾用于其他电子邮件的脚本来下载附件:
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)
然而,当我尝试使用xlrd阅读器(或使用pandas)打开文件时,我得到了这个:
raise XLRDError('Unsupported format, or corrupt file: ' + msg)
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\nVisit '
有谁能解释这里出了什么问题?
有没有办法可以打开附件而不保存,只需复制工作表并将该副本另存为.csv文件?
谢谢
答案 0 :(得分:0)
看看这个question。您尝试下载的文件可能不是真正的Excel文件,而是保存为.xls文件的csv。证据是错误消息Expected BOF record; found b'\r\nVisit '
。我认为excel文件将以<?xml
或类似的东西开头。你可以用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)