gpt4 book ai didi

python - 从 Outlook 电子邮件中提取嵌入图像

转载 作者:太空狗 更新时间:2023-10-30 02:50:43 25 4
gpt4 key购买 nike

我正在使用 Microsoft 的 CDO(协作数据对象)以编程方式从 Outlook 邮箱中读取邮件并保存嵌入式图像附件。我正在尝试使用 Win32 扩展从 Python 执行此操作,但任何使用 CDO 的语言的示例都会有所帮助。

到目前为止,我在这里...

以下 Python 代码将读取我邮箱中的最后一封电子邮件,打印附件名称,并打印邮件正文:

from win32com.client import Dispatch

session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\nbar');
inbox = session.Inbox
message = inbox.Messages.Item(inbox.Messages.Count)

for attachment in message.Attachments:
print attachment

print message.Text

session.Logoff()

但是,附件名称类似于:“zesjvqeqcb_chart_0”。在电子邮件源中,我看到这样的图像源链接:

那么,是否可以使用这个 CID URL(或其他任何东西)来提取实际图像并保存在本地?

最佳答案

OS/Outlook/CDO 版本的差异可能是混淆的根源,因此以下是使其在 WinXP/Outlook 2007/CDO 1.21 上运行的步骤:

  • 安装CDO 1.21
  • 安装win32com.client
  • 转到 C:\Python25\Lib\site-packages\win32com\client\目录运行以下命令:
python makepy.py
  • from the list select "Microsoft CDO 1.21 Library (1.21)", click ok
C:\Python25\Lib\site-packages\win32com\client>python makepy.pyGenerating to C:\Python25\lib\site-packages\win32com\gen_py\3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.pyBuilding definitions from type library...Generating...Importing module
  • Examining file 3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py that's just been generated, will give you an idea of what classes, methods, properties and constants are available.

Now that we are done with the boring steps, here is the fun part:

import win32com.client
from win32com.client import Dispatch

session = Dispatch('MAPI.session')
session.Logon ('Outlook') # this is profile name
inbox = session.Inbox
messages = session.Inbox.Messages
message = inbox.Messages.GetFirst()

if(message):
attachments = message.Attachments
for i in range(attachments.Count):
attachment = attachments.Item(i + 1) # yep, indexes are 1 based

filename = "c:\\tmpfile" + str(i)
attachment.WriteToFile(FileName=filename)
session.Logoff()

如果您有旧版本的 CDO(CDO for win2k),同样的通用方法也适用

关于python - 从 Outlook 电子邮件中提取嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/440356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com