通过for循环中的字典中的列表迭代
我正在尝试将Outlook中的所有邮件存储在数据库中。为此,我需要在Outlook中迭代每个文件夹。我正在使用win32com.client。我创建了一个词典,将邮箱的每个名称作为键,所有文件夹都作为带有值的列表。
postbox_and_folders = {}
folder_of_postbox = []
for postbox in postboxes:
for idx, folder in enumerate(mapi.Folders(postbox).Folders):
folder_of_postbox.append(str(folder))
postbox_and_folders[postbox] = folder_of_postbox
if str(folder) == 'Archive':
folder_of_postbox = []
print(postbox_and_folders)
输出看起来像这样:
{'@VPC': ['Calendar', 'Contacts', 'Conversation Action Settings', 'Conversation History', 'Deleted Items', 'Drafts', 'Einstellungen für QuickSteps', 'ExternalContacts', 'Files', 'Inbox', 'Journal', 'Junk Email', 'Notes', 'Outbox', 'PersonMetadata', 'Sent Items', 'Social Activity Notifications', 'Sync Issues', 'Tasks', 'Yammer Root', 'Archive'], '@FCC': ['Calendar', 'Contacts', 'Conversation Action Settings', 'Conversation History', ...] which is exactly how it should look.
现在是我的目标,即通过每个邮箱及其各自的文件夹来存储消息正文中的数据库中。
我知道我必须使用mapi.folders,但无法使其与此词典一起使用。 我如何使用此词典迭代每个文件夹?
我只需要将词典放在此功能中,我觉得我非常接近它。
for key, value in postbox_and_folders.items():
messages = mapi.Folders(str(key)).Folders(value[i]).Items
for message in list(messages)[:10]:
print(message.Body)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要递归迭代所有子文件夹。例如,C#中的原始草图是所有类型的编程语言中常见的Outlook对象模型:
您可能会找到类似的线程有用。
You need to iterate over all subfolders recursively. For example, a raw sketch in C# as the Outlook object model is common for all kind of programming languages:
You may find a similar Problem with loop all the emails inside the outlook folder and sub-folder thread helpful.
如果要存储文件夹名称,则(或者)也可以存储文件夹条目ID(
mapifolder.entryid
)。如果您知道文件夹条目ID,则可以随时使用application.session.getFolderFromid
打开它。但是请记住,您只需处理一个级别的文件夹,您确实需要递归处理它们 - 创建一个将
mapifolder
作为参数的函数。然后,该函数可以通过子文件夹(mapifolder.folders
Collection)枚举,并为每个子文件夹呼叫。If you are storing the folder names, you might as well (or instead) store the folder entry ids (
MAPIFolder.EntryID
). If you know the folder entry id, you can open it at any moment usingApplication.Session.GetFolderFromID
.Keep in mind however that you only process one level of folders, and you really need to process them recursively - create a function that takes
MAPIFolder
as a parameter. The function can then enumerate through the child folders (MAPIFolder.Folders
collection) and call itself for each subfolder.您错过了一个循环,可以通过每个邮箱中的文件夹迭代,请尝试一下,
希望这有效!
You have missed a for loop to iterate through the folders in each postbox, try this,
Hope this works!