Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取所有Outlook电子邮件';日期、发件人、收件人,以CSV为准_Python_Email_Outlook_Win32com_Archiving - Fatal编程技术网

Python 提取所有Outlook电子邮件';日期、发件人、收件人,以CSV为准

Python 提取所有Outlook电子邮件';日期、发件人、收件人,以CSV为准,python,email,outlook,win32com,archiving,Python,Email,Outlook,Win32com,Archiving,简介 我想创建某种存档脚本,它将收集所有Outlook(unicode)电子邮件的日期、发件人(姓名+地址)、收件人(姓名+地址)、主题,并将它们放入CSV文件中 (额外的超级解决方案是,如果它能够提取包含文件夹的名称和可能的类别,尽管这不是必须的。 作为最后一步,我想让它具有可移植性,这样其他人就可以在没有Python的情况下使用它。) (我正在使用Python 2.7和Outlook 2013) 代码 以下是我目前掌握的情况: import win32com.client import sy

简介

我想创建某种存档脚本,它将收集所有Outlook(unicode)电子邮件的日期、发件人(姓名+地址)、收件人(姓名+地址)、主题,并将它们放入CSV文件中

(额外的超级解决方案是,如果它能够提取包含文件夹的名称和可能的类别,尽管这不是必须的。 作为最后一步,我想让它具有可移植性,这样其他人就可以在没有Python的情况下使用它。)

(我正在使用Python 2.7和Outlook 2013)

代码

以下是我目前掌握的情况:

import win32com.client
import sys
import unicodecsv as csv

output_file = open('./outlook_farming_001.csv','wb')    
output_writer = csv.writer(output_file, delimiter = ";", encoding='latin2')

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. 
messages = inbox.Items

for i, message in enumerate(messages):              # enumerated the items
    try:

        sender = message.SenderName                 
        sender_address = message.sender.address         
        sent_to = message.To                    
        date = message.LastModificationTime         
        subject = message.subject                   

        output_writer.writerow([
            date, 
            sender, 
            sender_address,             
            sent_to,
            subject]) 

    except Exception as e:
        ()

output_file.close()
问题:

  • 如何确保它提取所有电子邮件?(当我运行这个脚本时,它工作了,但它只提取了1555封电子邮件,尽管我的Outlook收件箱显示,它包含4785封。)
  • 如何使其在所有Outlook文件夹上工作?(它只处理收件箱,但我需要所有其他文件夹(已发送和其他已创建的文件夹))
  • 如何获取收件人的电子邮件地址?(我只能提取筛选的名称)
  • 如果您对任何问题有任何建议,我们将不胜感激。 提前谢谢

    对于问题2:

    inbox = outlook.GetDefaultFolder(6)
    
    代码中的“6”表示收件箱

    for folder in outlook.Folders: 
        print(folder.Name)
    
    使用上面的for循环查找邮箱中的所有文件夹

    关于问题3:

    要获取发件人电子邮件ID,您可以使用以下代码:

    messages = inbox.Items
    message = messages.GetFirst()
    sender_emailid =message.SenderEmailAddress
    
    关于问题1: 我没有答案。对不起,问题2:

    inbox = outlook.GetDefaultFolder(6)
    
    代码中的“6”表示收件箱

    for folder in outlook.Folders: 
        print(folder.Name)
    
    使用上面的for循环查找邮箱中的所有文件夹

    关于问题3:

    要获取发件人电子邮件ID,您可以使用以下代码:

    messages = inbox.Items
    message = messages.GetFirst()
    sender_emailid =message.SenderEmailAddress
    
    关于问题1: 我没有答案。对不起