如何使用win32.client通过Python访问各种Outlook文件夹 [英] How do I access various Outlook folders through Python using win32.client
问题描述
我打算针对主题为生产"的那些电子邮件解析我的整个电子邮件收件箱,以便将电子邮件提取到.txt文件中以进行进一步解析.我已经成功地使Python能够找到收件箱中的每封电子邮件,因为我创建了显示邮件数和相关电子邮件数(总邮件数和主题为生产"的邮件数)的计数.我想知道是否可以通过Python在Outlook文件夹之间移动电子邮件,因此可以从收件箱移动到Outlook中名为生产"的文件夹.我当前的代码看起来像(包含一个GUI才能成功满足我的课程要求):
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()
推荐答案
文件夹在哪里?如果它是收件箱的子文件夹,请使用inbox.Folders.Item("production").
Where is the folder located? If it is subfolder of the Inbox, use inbox.Folders.Item("production").
这篇关于如何使用win32.client通过Python访问各种Outlook文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!