我的代码可以与一个测试msg文件一起正常工作,比如说它是file_1。
但是它与我实际上打算在其上运行的目标msg无关,比如说它是file_2。
更奇怪的是,在抛出com_error后,它无法在file_1上运行。
请在下面粘贴的代码段中查看详细信息。
我已将其更改为完整路径,不起作用。 我已经关闭了Outlook客户端,无法正常工作。 我尝试使用其他软件包,无法安装msg_extract,并显示有关另一个名为OLE的软件包的错误消息。找到了一个名为outlook_msg的程序包,它可以解析我的msg文件,但是功能不多,我什至无法检索电子邮件接收者,因此我应该坚持使用win32com。
from win32com import client as win32
outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
msg = outlook.OpenSharedItem('file_1.msg')
print(msg.SenderEmailAddress)
somebody@email.com
outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
msg = outlook.OpenSharedItem('file_2.msg')
print(msg.SenderEmailAddress)
com_error:(-2147352567,“发生异常。”,(4096,“ Microsoft Outlook”,“我们无法打开'file_2.msg'。可能文件已经打开,或者您没有权限。打开它。\ n \ n要检查您的权限,请右键单击该文件夹,然后单击“属性”。“,None,0,-2147287038),None)
outlook = win32.Dispatch('Outlook.Application').GetNamespace('MAPI')
msg = outlook.OpenSharedItem('file_1.msg')
print(msg.SenderEmailAddress)
com_error:(-2147352567,“发生了异常。”,(4096,“ Microsoft Outlook”,“我们无法打开'file_1.msg'。可能文件已经打开,或者您没有权限。打开它。\ n \ n要检查您的权限,请右键单击该文件夹,然后单击“属性”。“,None,0,-2147287008),None)
我希望file_2可以输出像file_1一样的结果,但是它显示了一个错误,已粘贴到上面。
com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "file_2.msg'. It's possible the file is already open, or you don't have permission to open it.\n\nTo check your permissions, right-click the file folder, then click Properties.", None, 0, -2147287008), None)
答案 0 :(得分:2)
.msg文件似乎已打开。 尝试在脚本之前粘贴代码,以便如果打开任何.msg文件,它将首先将其杀死。
脚本:
import os
try:
os.system('taskkill /f /im OUTLOOK.exe')
except:
pass
... snippet ... your script goes here.
答案 1 :(得分:0)
错误是STG_E_FILENOTFOUND
,这很有意义-您仅指定文件名而没有路径。您必须提供完全限定的文件名。