How can I export the text from the result of a python for-loop into an excel file?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật