问题
I intend on parsing my entire email inbox for those emails with the subject 'production' in order to extract the email into a .txt file for further parsing. I have successfully got Python to locate every email in my inbox as I have created counts which show the number of emails and the related emails (total emails and emails with subject 'production'). I want to know if through Python, I can move emails between Outlook folders, so from Inbox to a folder named 'production' within Outlook. My current code looks like (contains a GUI in to successfully meet the requirements of my coursework):
from tkinter import *
import win32com.client
import sys
import os.path
save_path = 'D:/Test1/'
nGui=Tk()
title=nGui.title('Production Accolation')
geo=nGui.geometry
def homepage():
geo('250x150')
title
BtnSta=Button(text="Start", command=start,height=2,width=100,font = "Calibri 12 bold").pack()
BtnRep=Button(text="Report",command=report,height=2, width=100,font = "Calibri 12 bold").pack()
BtnExi=Button(text="Exit",fg="red",command=end, height=2,width=100,font = "Calibri 12 bold").pack()
nGui.mainloop()
def start():
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
subject=message.Subject
emailcount=0
corrcount=0
misccount=0
for m in messages:
emailcount=emailcount+1
if m.Subject=="production":
corrcount=corrcount+1
email="outlookparse"
compemail=os.path.join(save_path, email+".txt")
file1=open(compemail,"w")
file1.write(message.body)
file1.close()
else:
misccount=misccount+1
print("Total Emails ",emailcount)
print("Production Emails ",corrcount)
print("Miscellaneous Emails ",misccount)
def report():
print("ok")
def end():
exit()
homepage()
回答1:
Where is the folder located? If it is subfolder of the Inbox, use inbox.Folders.Item("production").
回答2:
Try writing the file with this snippet: file1.write(m.body)
来源:https://stackoverflow.com/questions/35891562/how-do-i-access-various-outlook-folders-through-python-using-win32-client