Issue
I need to save the message subject from emails as a simple txt file on my desktop. In the below example subject1 defines the message subject. I have tried subject1.SaveAsFile(location) but that did not work. Since I am using Jupyter notebook, its saving locally but I need to save it on my desktop.
import win32com.client
import datetime
import os
import email
import pandas as pd
import os
import glob
import file_operations
outlook = win32com.client.dynamic.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
message = inbox.Items
for message in inbox.Items:
if message.UnRead == True:
subject1 = message.Subject
Solution
You need to open a file to do so. Make sure subject1
is a string, or cast it to a string with str()
.
with open('C:\\Users\\ShaneN\\Desktop\\testing\\filename.txt','w') as file:
file.write(subject1)
Answered By - Titouan L
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.