我想使用Python读取PST文件。我找到了两个库win32和pypff
使用win32,我们可以使用以下方法启动outlook对象:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)GetDefaultFolder(6)获取收件箱文件夹。然后我可以使用这个文件夹函数和属性来处理。
但是我想要的是给我自己的pst文件,pywin32(或任何其他库)都可以读取这些文件。在这里,它只与我的Outlook应用程序连接
使用pypff,我可以使用下面的代码来处理pst文件:
import pypff
pst_file = pypff.file()
pst_file.open('test.pst')
root = pst_file.get_root_folder()
for folder in root.sub_folders:
for sub in folder.sub_folders:
for message in sub.sub_messages:
print(message.get_plain_text_body()但是,我需要像消息大小这样的属性,也希望访问pypff中不可用的pst文件中的日历(据我所知)。
问题
win32、pypff或其他库中有工作吗?发布于 2019-12-13 20:21:58
这是我想为我自己的应用程序做的事情。我能够从这些来源拼凑出一个解决方案:
上面的第三个链接应该提供关于可用属性和各种项目类型的更多详细信息。我的解决方案仍然需要连接到Outlook应用程序,但是它对用户应该是透明的,因为pst存储是使用try/catch/finally块自动删除的。我希望这能帮助你走上正确的轨道!
import win32com.client
def find_pst_folder(OutlookObj, pst_filepath) :
for Store in OutlookObj.Stores :
if Store.IsDataFileStore and Store.FilePath == pst_filepath :
return Store.GetRootFolder()
return None
def enumerate_folders(FolderObj) :
for ChildFolder in FolderObj.Folders :
enumerate_folders(ChildFolder)
iterate_messages(FolderObj)
def iterate_messages(FolderObj) :
for item in FolderObj.Items :
print("***************************************")
print(item.SenderName)
print(item.SenderEmailAddress)
print(item.SentOn)
print(item.To)
print(item.CC)
print(item.BCC)
print(item.Subject)
count_attachments = item.Attachments.Count
if count_attachments > 0 :
for att in range(count_attachments) :
print(item.Attachments.Item(att + 1).Filename)
Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
pst = r"C:\Users\Joe\Your\PST\Path\example.pst"
Outlook.AddStore(pst)
PSTFolderObj = find_pst_folder(Outlook,pst)
try :
enumerate_folders(PSTFolderObj)
except Exception as exc :
print(exc)
finally :
Outlook.RemoveStore(PSTFolderObj)https://stackoverflow.com/questions/54217082
复制相似问题