首页 > 解决方案 > 无法使用 python 在 for 循环中解析 .msg 文件

问题描述

我是 Python 编程的新手。我需要根据每个“msg”文件中的内容自动将位于 Windows 目录的多个子目录中的大约 450 个“msg”文件复制到其他目录。我使用了以下代码:

from shutil import copy2
import win32com.client

# Function which returns dictionary with absolute filepaths, file names
def list_files(dir):
    r = {}
    for root, dirs, files in os.walk(dir):
        for name in files:
            r[os.path.join(root, name)]=name
    return r

allFiles = list_files(sourceDir)

# Parsing all emails
for filename in allFiles.items():
    if filename[1].endswith(".msg"):
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        try:
            email = outlook.OpenSharedItem(filename[0])
            emailContent = email.Subject + "\n" + email.Body
            print(emailContent)
        except FileNotFoundError as e:
            print("File Not Found Error: " + str(e))
        finally:
            del email, outlook

再往下,我将根据内容复制文件。但这些电子邮件的许多文件路径都超过 260 个字符。所以,我收到以下错误:

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The URL was bad/not found:\r\n  "C:\\Users\\HPHP\\Documents\\...\\Data\\...\\IHI ...\\......". Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)

如果我重命名文件名以使绝对路径长度减少到 260 个字符以下,则我不会遇到此错误。但是我需要文件名完好无损。我还尝试使用以下代码绕过 MAX_PATH 限制,在这种情况下不起作用:

email = outlook.OpenSharedItem("\\\\?\\"+filename[0])

我尝试了另一种方法,为每次迭代更改当前工作目录。以下是代码:

for filename in allFiles.items():
    if filename[1].endswith(".msg"):
        os.chdir(filename[0].replace(filename[1],""))
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        try:
            email = outlook.OpenSharedItem(filename[1])
            emailContent = email.Subject + "\n" + email.Body
            print(emailContent)
            ...

但是,我面临以下错误:

com_error                                 Traceback (most recent call last)
<ipython-input-64-c7dea54d9247> in <module>()
      5         try:
----> 6             email = outlook.OpenSharedItem(filename[1])
      7             emailContent = email.Subject + "\n" + email.Body

~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in OpenSharedItem(self, Path)

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open '2013 Self Cert.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, -2147287038), None)

我无法弄清楚这个错误背后的原因。有人可以帮帮我吗?

标签: pythonoutlook

解决方案


将 MSG 文件复制到一个完整路径短于 MAX_PATH 的临时文件夹/文件。


推荐阅读