我正在使用.msg文件导出Outlook消息。我不确定如何完成这项任务。我有一个程序,当前正在读取电子邮件并导出附件,一旦完成它将消息移动到已处理的文件夹,以便我可以跟踪已完成的内容。我需要添加一个函数,将整个电子邮件本身导出到本地计算机上的文件夹。有人用pywin32.client完成了这个吗?
这是现在的程序。 (原谅它还在进行中)
import os
import win32com.client
import csv
import datetime
from random import randint
ATTACHMENTS_FOLDER = "C:\\EMAILS"
LOG_PATH = 'C:\\EMAILS\\log.csv'
COUNTER = 1
SUBFOLDER_LIST = ["TEST"]
UPLOAD_LIST = 'C:\\EMAILS\\logupload%d.csv' % randint(2,150000)
def ExportMessages (Folder, item):
#No IDEA!
pass
def process_attachment(sender, subject, attachment, messagedate):
"""
:param sender:
:param subject:
:param attachment:
:param messagedate:
"""
global count
count = 0
try:
filepath = os.path.join(ATTACHMENTS_FOLDER, "RAN-%dSEN-%sSUB-%s%s" % (randint(2,100000),str(sender), str(subject), attachment))
count = count +1
print "Trying", filepath
open(filepath, "r")
os.remove(filepath)
except IOError:
pass
try:
attachment.SaveAsFile(filepath)
row = [messagedate, sender, subject, count]
row2 = [messagedate, sender, subject, filepath]
w = csv.writer(csv_file)
w2 = csv.writer(csv_file2)
w.writerow(row)
w2.writerow(row2)
except:
pass
def move_message_fail(message, folder):
"""
:param message:
:param folder:
"""
print "Moving:", message.Subject
proc_folder = folder.Folders("Failed")
message.Move(proc_folder)
def move_message(folder, message):
"""
:param folder:
:param message:
"""
print "Moving:", message.Subject
proc_folder = folder.Folders("Processed")
message.Move(proc_folder)
def process_message(message, folder):
"""
:param message:
:param folder:
"""
global vin_num
vin_num = ''
print "Message:", message.Subject
vin = message.Subject
sender = message.SenderName
if folder == SUBFOLDER_LIST[0]:
for i in vin.split(' '):
if '-' in i:
vin_num = i
if vin_num:
now = datetime.datetime.now()
messagedate = now.strftime("%m-%d-%Y")
attachments = message.Attachments
for n_attachment in range(attachments.Count):
attachment = attachments.Item(n_attachment + 1)
#if attachment.Type == win32com.client.constants.CdoFileData:
process_attachment(sender, vin_num, attachment, messagedate)
#message.Update()
move_message(folder, message, up_folder)
else:
move_message_fail(message, folder)
else:
vin_num = vin.split(' ')[1]
now = datetime.datetime.now()
messagedate = now.strftime("%m-%d-%Y")
attachments = message.Attachments
for n_attachment in range(attachments.Count):
attachment = attachments.Item(n_attachment + 1)
#if attachment.Type == win32com.client.constants.CdoFileData:
process_attachment(sender, vin_num, attachment, messagedate)
#message.Update()
move_message(folder, message, up_folder)
if __name__ == '__main__':
csv_file = open(LOG_PATH, "ab")
csv_file2 = open(UPLOAD_LIST, "wb")
for i in SUBFOLDER_LIST:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
one_folder = outlook.Folders(1)
two_folder = one_folder.Folders("Inbox")
three_folder = two_folder.Folders(i)
messages = three_folder.Items
message = messages.GetFirst()
while message:
process_message(message, i)
ExportMessages(three_folder, message)
message = messages.GetNext()
答案 0 :(得分:3)
调用MailItem.SaveAs并将olMsg或olMsgUnicode作为第二个(格式)参数传递。