【问题标题】:How do I access various Outlook folders through Python using win32.client如何使用 win32.client 通过 Python 访问各种 Outlook 文件夹
【发布时间】:2016-03-09 12:25:38
【问题描述】:

我打算解析我的整个电子邮件收件箱以查找主题为“production”的电子邮件,以便将电子邮件提取到 .txt 文件中以进行进一步解析。我已经成功地让 Python 在我的收件箱中找到每封电子邮件,因为我创建了显示电子邮件数量和相关电子邮件(主题为“生产”的电子邮件总数和电子邮件)的计数。我想知道是否通过 Python,我可以在 Outlook 文件夹之间移动电子邮件,因此从收件箱到 Outlook 中名为“生产”的文件夹。我当前的代码看起来像(包含一个 GUI 以成功满足我的课程要求):

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()

【问题讨论】:

    标签: python email winapi outlook directory


    【解决方案1】:

    文件夹在哪里?如果是收件箱的子文件夹,请使用inbox.Folders.Item("production").

    【讨论】:

    • 是的,就是我要找的文件夹,谢谢。但是,有没有办法将收件箱中的电子邮件完全移动到 Outlook 中的那个文件夹??
    • 当然,通过目标文件夹调用 MailItem.Move。
    【解决方案2】:

    尝试使用以下 sn-p 写入文件:file1.write(m.body)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 2019-08-26
      • 2019-06-17
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多