简体   繁体   中英

Using win32com to download attachments through outlook with python

I've written a short code to download and rename files from a specific folder in my outlook account. The code works great, the only problem is that I typically need to run the code several times to actually download all of the messages. It seems the code is just failing to acknowledge some of the messages, there are no errors when I run through it. I've tried a few things like walking through each line step by step in the python window, running the code with outlook closed or opened, and trying to print the files after they're successfully saved to see if there are specific messages that are causing the problem. Here's my code

#! python3
# downloadAttachments.py - Downloads all of the weight tickets from Bucky
# Currently saves to desktop due to instability of I: drive connection
import win32com.client, os, re

#This line opens the outlook application
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

#Not exactly sure why the inbox is default folder 6 but it works
inbox = outlook.GetDefaultFolder(6)

#box where the messages are to save
TicketSave = inbox.Folders('WDE').Folders('SAVE').Folders('TicketSave')

#box where the messages are moved to 
done = inbox.Folders('WDE').Folders('CHES').Folders('Weight Tickets')
ticketMessages = TicketSave.Items

#Key is used to verify the subject line is correct. This script only works if the person sends
# their emails with a consistent subject line (can be altered for other cases)
key = re.compile(r'wde load \d{3}') #requires regulars expressions (i.e. 'import re')


for message in ticketMessages:

    #will skip any message that does not match the correct subject line format (non-case sensitive)
    check = str(message.Subject).lower()
    if key.search(check) == None:
        continue
    attachments = message.Attachments
    tic = attachments.item(1)
    ticnum = str(message.Subject).split()[2]
    name = str(tic).split()[0] + ' ticket ' + ticnum + '.pdf' #changes the filename
    tic.SaveAsFile('C:\\Users\\bhalvorson\\Desktop\\Attachments' + os.sep + str(name))
    if message.UnRead == True:
        message.UnRead = False
    message.Move(done)
    print('Ticket pdf: ' + name + ' save successfully')

Alright I found the answer to my own question. I'll post it here in case any other youngster runs into the same problem as me.

The main problem is the "message.Move(done)" second from the bottom. Apparently the move function alters the current folder thus altering the number of loops that the for loop will go through. So, the way it's written above, the code only ever processes half of the items in the folder.

An easy work around is to switch the main line of the for loop to "for message in list(ticketMessages):" the list is not affected by the Move function and therefore you'll be able to loop through every message.

Hope this helps someone.

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