如何将交换FileAttachments传递给pd.read_excel?

时间:2018-07-12 09:25:56

标签: python pandas exchangelib

我使用exchangelib创建了一个过滤器,以获取包含.xlsx文件的多封电子邮件。下一步应该放在一个pd.DataFrame中。

当我尝试遍历过滤器时尝试pd.read_excel()时,我无法将attachment.content传递到pd.read_excel中。

我尝试使用pd.read_excel(attachment.content)pd.read_excel(open(attachment.content,'rb'))之类的几种组合。参见下面有关io.BytesIO的最后尝试:

import pandas as pd
import exchangelib
from exchangelib import EWSTimeZone,EWSDateTime,FileAttachment,HTMLBody
import datetime
from dateutil.parser import parse
from ipywidgets import interact
from ipywidgets import interact_manual
import io

def get_outages(filterstart,filterende,location):

  credentials = exchangelib.Credentials('my.user@provider.com', 'passwd')
  account = exchangelib.Account('my.user@provider.com', credentials=credentials, autodiscover=True)
  tz = EWSTimeZone.localzone()
  myfolder_delay = account.inbox/'Delay'

  outages=pd.DataFrame

  filterstart=datetime.datetime.strptime(filterstart,"%d.%m.%Y %H:%M")
  filterende=datetime.datetime.strptime(filterende,"%d.%m.%Y %H:%M")

  #filterstart=filterstart+datetime.timedelta(hours=1)
  filterende=filterende+datetime.timedelta(hours=1)

  filter = myfolder_delay.filter(datetime_received__range=tz.localize(EWSDateTime(filterstart.year, filterstart.month, filterstart.day, filterstart.hour, filterstart.minute)), tz.localize(EWSDateTime(filterende.year, filterende.month, filterende.day, filterende.hour, filterende.minute))))

  for item in filter:
    print(item.subject)
    for attachment in item.attachments:
        stream_str = io.BytesIO(attachment.content)
        outages=pd.read_excel(stream_str.getvalue(),engine='xlrd')

interact_manual(get_outages, filterstart='11.07.2018 00:00', 

filterende='11.07.2018 23:59',location='Location')

**ValueError**
.
.
.
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\excel.py in __init__(self, io, **kwds)
    394             self.book = xlrd.open_workbook(self._io)
    395         else:
--> 396             raise ValueError('Must explicitly set engine if not passing in'
    397                              ' buffer or path for io.')
    398 

ValueError: Must explicitly set engine if not passing in buffer or path for io.

1 个答案:

答案 0 :(得分:0)

read_excel()需要一个类似文件的对象或文件的路径,但是Attactment.content是一个bytes对象。您可以将内容写入文件并指向read_excel(),也可以将内容转换为BytesIO。这样的事情应该起作用(未经测试):

from io import BytesIO
import pandas as pd

pd.read_excel(BytesIO(attachment.content))