简体   繁体   中英

Iterating through a list within dictionary in a for loop

I am trying to store all mails from outlook in a database. for that i need to iterate through every folder i have in outlook. I am using win32com.client for that. I created a dictionary with every name of the postbox as a key and all of the folders as a list with values.

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)

The output looks like this:

{'@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.

Now is my goal to go through each postbox and their respective folders to store the body of the messages in a database.

I know I have to use mapi.Folders but am not able to make it work with this dictionary. How do I iterate through every folder with this dictionary?

I just have to put the dictionary in this function and I feel like I'm pretty close to it.

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)

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:

private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// Uses recursion to enumerate Outlook subfolders.
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders =
        folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // Write the folder path.
            Debug.WriteLine(childFolder.FolderPath);
            // Call EnumerateFolders using childFolder.
            EnumerateFolders(childFolder);
        }
    }
}             

You may find a similar Problem with loop all the emails inside the outlook folder and sub-folder thread helpful.

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 using Application.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,

for key, value in postbox_and_folders.items():
    for i in value:    
        messages = mapi.Folders(str(key)).Folders(value[i]).Items
        for message in list(messages)[:10]:
            print(message.Body)

Hope this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM