如何在 Python 中使用 win32com.client 保存 Outlook 中的附件? [英] How to save attachment from outlook using win32com.client in Python?

查看:35
本文介绍了如何在 Python 中使用 win32com.client 保存 Outlook 中的附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 中的 win32com 模块阅读电子邮件并将附件下载到我自己的文件夹中,但我停止获取附件对象:

from win32com.client import Dispatch导入日期时间作为日期Outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")收件箱 = Outlook.GetDefaultFolder("6")all_inbox = inbox.Itemsval_date = date.date.today()sub_today = '嗨'att_today = '附件.xlsx'对于 all_inbox 中的 msg:如果 msg.Subject == sub_today:休息对于 msg.Attachments 中的 att:如果 att.FileName == att_today:休息att.SaveAsFile('new.xlsx')att.ExtractFile('new.xlsx')打开(att)att.WriteToFile('x')

最后 4 行都不起作用...

<预><代码>>>>att.ExtractFile('new.xlsx')引发 AttributeError("%s.%s" % (self._username_, attr))属性错误:<unknown>.ExtractFile>>>打开(att)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到实例>>>att.WriteToFile('x')引发 AttributeError("%s.%s" % (self._username_, attr))属性错误:<unknown>.WriteToFile

att.SaveAsFile('new.xlsx') 没有错误,但是工作目录中没有这样的文件.似乎这条线被忽略了......

有人可以帮忙吗?提前致谢!

解决方案

只是为了更新,我通过在 SaveAsFile 中声明 dir 和文件名本身解决了这个问题:

att.SaveAsFile(os.getcwd() + '\new.xlsx')

它不像我在这里看到的大多数线程说你只需要在其中放置路径.其实路径和文件名都需要.

另外,奇怪的是,您必须将 os.getcwd() 放在这里,因为 Python 无法识别当前正在运行的 dir - 在 R 中,我们设置了工作 dirgetwd(),我们可以写入这个位置的任何文件.

Im trying to read email and download the attachment to my own folder using win32com module in Python, I stopped at getting the attachment object:

from win32com.client import Dispatch
import datetime as date

outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()

sub_today = 'Hi'
att_today = 'Attachment.xlsx'
for msg in all_inbox:
    if msg.Subject == sub_today:
        break

for att in msg.Attachments:
    if att.FileName == att_today:
        break

att.SaveAsFile('new.xlsx')
att.ExtractFile('new.xlsx')
open(att)    
att.WriteToFile('x')

None of the last 4 lines work...

>>> att.ExtractFile('new.xlsx')
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.ExtractFile

>>> open(att) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, instance found

>>> att.WriteToFile('x')
 raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.WriteToFile

att.SaveAsFile('new.xlsx') has no error, but there is no such file in the working directory. Seems that the line was just ignored...

Could anyone help? Thanks in advance!

解决方案

Just to update, I have solved this issue by claiming both dir and the file name itself in SaveAsFile:

att.SaveAsFile(os.getcwd() + '\new.xlsx')

It is not like most threads I've seen here saying that you only need to put path in it. Actually both path and file name are needed.

Also, weirdly, you have to put os.getcwd() here since Python wouldn't recognize current running dir - in R, after we set working dir with getwd(), we are able to write to any file at this location.

这篇关于如何在 Python 中使用 win32com.client 保存 Outlook 中的附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆