'Run Executable File to Get Outlook Attachments
I've written Python code that downloads Outlook attachments from [email protected] and saves them in particular_folder. It works great when I run it in Spyder, but when I convert it to an executable and run it, the attachments aren't downloaded, though no errors are thrown. I'm very confused about what the problem could be.
import win32com.client as win
import datetime as dt
# Set up connection to outlook
outlook = win.Dispatch("Outlook.Application").GetNamespace("MAPI")
# Go to the inbox folder
inbox = outlook.GetDefaultFolder(6)
# Record the messages
messages = inbox.Items
# Get the first message
message = messages.GetLast()
# Record today's date
today = dt.date.today()
# Initialize email date to today
email_date = today
# Get yesterday's date
yesterday = today - dt.timedelta(days = 1)
# Initialize count at 0
count = 0
# Record the number of emails read
emails_read = 0
# Record the number of tries
tries = 0
# Record the number of excepts
excepts = 0
# Was the break reached?
break_reached = 0
# While the count is less than three
while count < 4:
emails_read += 1
try:
# Record the email address
email_address = message.SenderEmailAddress
# Record the date when the message was sent
email_date = message.senton
# Convert the date to a dt.date object
email_date = dt.date(year = email_date.year, month = email_date.month, day = email_date.day)
# If the email is from [email protected] and the email was send yesterday...
if (email_address == '[email protected]') & ((email_date == yesterday)|(email_date == today)):
# Record the attachment
attachments = message.Attachments
# Get the first attachment
attachment = attachments.Item(1)
# Get the name of the attachment
attachment_name = str(attachment)
# Save the attachment in my ICE folder
attachment.SaveASFile(particular_location + attachment_name)
# Add 1 to the count
count += 1
# Move on to the next message
message = messages.GetPrevious()
tries += 1
# ... when not possible...
except:
# ... move on to the next message
message = messages.GetPrevious()
excepts += 1
if (emails_read > 1e4)|(email_date == dt.date.today() - dt.timedelta(days = 2)):
break_reached += 1
break
# Create a log dictionary
log_dict = {'count':count, 'emails_read':emails_read, 'tries':tries, 'excepts':excepts,
'break_reached':break_reached, 'email_date':email_date.strftime("%m-%d-%y")}
# Record the hyperparameters from the cross-validation
with open(other_location + 'log.txt', 'w') as f:
# Look over the dictionary of best hyperparameters
for key, value in log_dict.items():
# Print key: value (space)
f.write('%s:%s\n' % (key, value))
I've added a log to the code which is shown above. The log reads:
count:0 emails_read:10001 tries:0 excepts:10001 break_reached:1 email_date:03-30-22
So, it turns out I'm always getting an error in the try case and it goes to the except.
Solution 1:[1]
There are plenty reasons why your code could fail to save attached files on the disk. I'll name a few:
Outlook is a singleton. You can't run two instances of the Outlook application at the same time. So, if the loaded instance is run under a different security content your code may fail.
The target folder is not reachable. It can be insufficient privileges for writing, absence and etc.
Outlook items that correspond to the defined conditions in the code are not found.
We can continue listing assumptions there. But only a log file can shed some light on the problem you faced with.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugene Astafiev |
