Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将exchange文件附件传递到pd.read\u excel?_Python_Pandas_Exchangelib - Fatal编程技术网

Python 如何将exchange文件附件传递到pd.read\u excel?

Python 如何将exchange文件附件传递到pd.read\u excel?,python,pandas,exchangelib,Python,Pandas,Exchangelib,我使用exchangelib创建了一个过滤器,以获取包含.xlsx文件的多封电子邮件。下一步应该放在一个pd.DataFrame中 当我在迭代过滤器时尝试pd.read\u excel()时,我无法将attachment.content传递到pd.read\u excel 我尝试过几种组合,如pd.read\u excel(attachment.content),pd.read\u excel(open(attachment.content,'rb'))。我最后一次尝试io.BytesIO时,请

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

当我在迭代过滤器时尝试
pd.read\u excel()
时,我无法将attachment.content传递到pd.read\u excel

我尝试过几种组合,如
pd.read\u excel(attachment.content)
pd.read\u 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.
read\u excel()
需要类似文件的对象或文件路径,但
attachment.content
bytes
对象。您可以将内容写入文件并指向
read\u excel()
,也可以将内容转换为
字节IO
。类似的方法应该可以工作(未经测试):

read\u excel()
需要类似文件的对象或文件路径,但
attachment.content
bytes
对象。您可以将内容写入文件并指向
read\u excel()
,也可以将内容转换为
字节IO
。类似的方法应该可以工作(未经测试):


很好,这很有效。顺便说一下,我已经在各种脚本中使用了exchanglib模块,非常好的工作和强大的支持!谢谢你的好话:-)很好,这很有效。顺便说一下,我已经在各种脚本中使用了exchanglib模块,非常好的工作和强大的支持!谢谢你的好话:-)
from io import BytesIO
import pandas as pd

pd.read_excel(BytesIO(attachment.content))