Python 3.8:从内存处理从Outlook电子邮件中提取的Excel文件

Python 3.8:从内存处理从Outlook电子邮件中提取的Excel文件,excel,python-3.x,outlook-2013,Excel,Python 3.x,Outlook 2013,我正在编写一个脚本,它执行以下操作 1) 查看我的Outlook收件箱 2) 在符合我的条件的电子邮件处停止 3) 获取其中的.XLS Excel文件附件(仅1个文件) 4) 按摩 其中的数据 5) 将结果文件保存到文件夹中 问题是我试图让Python从内存中处理文件 我在读取文件时遇到了以下错误 AttributeError: Item.Read 谢谢你给我的建议 import win32com.client import os import xlrd from io impo

我正在编写一个脚本,它执行以下操作

1) 查看我的Outlook收件箱
2) 在符合我的条件的电子邮件处停止
3) 获取其中的.XLS Excel文件附件(仅1个文件)
4) 按摩 其中的数据
5) 将结果文件保存到文件夹中

问题是我试图让Python从内存中处理文件 我在读取文件时遇到了以下错误

AttributeError: Item.Read
谢谢你给我的建议

import win32com.client
import os
import xlrd

from io       import BytesIO 
from datetime import datetime, date
from openpyxl import load_workbook


# Retrieve the email attachment from Outlook.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")


# Access the Inbox.
inbox = outlook.GetDefaultFolder(6)

messages = inbox.Items
for message in messages:

    if message.Subject == "XYZ" and message.Senton.date() == date.today():

        attachments = message.Attachments
        a   = attachments.Item(1)


##### Most likely I am missing something between these 2 lines
workbook = xlrd.open_workbook(file_contents=BytesIO(a.read()))

我进行了一次愉快的网络追逐,在这个PDF中找到了这个代码示例


您在哪里定义变量“xlrd”?…错误发生在哪一行?@Nick.McDermaid
xlrd
在这里不是变量。这是一个库,我需要它来调用
open\u工作簿
函数。错误发生在最后一行运行之后。不过我不想保存和打开文件。我想在内存中处理该文件,然后将其另存为文件。要将该文件放入内存,必须打开该文件。我不是python高手,所以可能我遗漏了一些东西,但文件没有加载到内存的唯一时间是当您的计算机内存不足并且必须交换到磁盘时。
from mmap import mmap,ACCESS_READ
from xlrd import open_workbook
print open_workbook('simple.xls')
with open('simple.xls','rb') as f:
 print open_workbook(
 file_contents=mmap(f.fileno(),0,access=ACCESS_READ)
 )
aString = open('simple.xls','rb').read()
print open_workbook(file_contents=aString)