我目前正在使用Python开发自动化解决方案,以读取传入的Outlook电子邮件并下载任何附件。我正在使用exchangelib库来实现这一点。我能够阅读电子邮件正文并能够保存文件附件(.pdf、.jpg、.xlsx),但面临如何下载Outlook项目(.msg附件)的问题。例:有人提供了电子邮件批准或附加的电子邮件有附加信息等…)。我通读了exchangelib文档,但没有任何关于如何保存outlook附件的信息。在
我尝试过各种方式,通过改变各种模式“w”,“wb”…但没有运气。在
我的代码
# Read & Save Attachments code
for item in emails.all().order_by('-datetime_received')[:1]:
for attachment in item.attachments:
print('Number of Attachment found:', len(item.attachments))
if isinstance(attachment, FileAttachment):
local_path=os.path.join('C:/Attachments', attachment.name)
with open(local_path, 'wb') as f, attachment.fp as fp:
buffer = fp.read(1024)
while buffer:
f.write(buffer)
buffer = fp.read(1024)
print('Saved attachment to', local_path)
elif isinstance(attachment, ItemAttachment):
name = str(attachment.name)+'.msg'
local_path=os.path.join('C:/Attachments/', name)
with open(local_path, 'w') as f:
f.write(attachment.item.body)
print('Saved attachment to', local_path)
Exchangelib示例
^{pr2}$在上面的代码中,我看到的是一个用主题名创建的文件,但是0 kb(没有内容)并且没有扩展名(.msg),这就是我在上面连接“name”的原因。在
目前没有回答
相关问题 更多 >
编程相关推荐