I’m trying to get the body of an outlook email from a report I get daily (sometimes twice a day) and export it into an excel file. I know I can just copy it manually and paste it into a new workbook (since the content comes in csv format already in the body of the email), but I’m trying to do this to sharpen my Python skills.
So far what I have is it does grab the most recent report from my inbox and can print the content of it into the Python terminal. However, when the part of the script runs where it’s supposed to take the output from the print function and put it into an excel file, it spits out a blank csv file. So it does create the excel file, it’s just blank. I tried changing the extension to txt and it outputs a blank txt file.
Can someone please tell me where I went wrong?
# Importing modules/libraries
import pandas as pd
import numpy as np
import win32com.client
import xlsxwriter
from xlsxwriter.utility import xl_rowcol_to_cell
# Variables
outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox = outlook.GetDefaultFolder(6) # 6 corresponds to the inbox folder
# For loop to loop through Outlook inbox to find the report. It will print the most recent email in your inbox that matches the criteria.
for message in inbox.Items:
if 'LYTX Sync errors' in message.Subject and 'The following vehicle adds/edits Failed in LYTX:' in message.body:
latest = (f"Date: {message.senton.date()}tSender: {message.Sender}tTrade Idea: {message.body}")
with open("test.csv", "w") as new_file:
new_file.write(latest[-1])
print (latest)
Here is what the print function outputs (from the body of the email)
1116496 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
3311958 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
2217477 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
2218862 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9922174 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9922196 UPDATE ErrorCode:3-ER already has a vehicle attached.
1113527 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1116598 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1116969 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
3316277 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
3319472 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
5520193 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1117436 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1118130 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
77350 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
77363 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1120031 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1120038 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1120505 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
1120512 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9910615 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9912998 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9917774 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
2210953 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
2211476 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
2213601 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
2213633 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9919287 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9919917 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9920159 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9920302 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9920339 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9921180 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9921276 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9921308 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
9921338 UPDATE ErrorCode:3-SerialNumber is invalid or ER group does not match with the Vehicle Group.
Thanks for any help in advance. I’m pretty new at Python and have tried to use it to automate different things in my day to day. I’m hoping I can learn something new from this.
I tried outputting to xlsx using xlsxwriter and also tried outputting to a text file. This resulted in a blank text file being created and a blank csv file created.
I also tried somehow converting the output to a pandas dataframe to use the export to excel feature of pandas but had no luck.