我正在执行以下操作的脚本
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中找到了此代码示例
https://github.com/python-excel/tutorial/raw/master/python-excel.pdf
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)