【问题标题】:Write to a csv using Python from Microsoft Outlook 2010 [closed]从 Microsoft Outlook 2010 使用 Python 写入 csv [关闭]
【发布时间】:2016-04-05 12:54:15
【问题描述】:

我每周收到大约 60 封电子邮件,我必须手动将数据输入 csv 文件。我使用 Microsoft Outlook 2010,并在我的收件箱中有一个名为“市场电子邮件”的子文件夹。在这个子文件夹中,我有电子邮件,其中包括所有者姓名、公司名称和我需要写入 csv 的电子邮件。

我正在尝试在 Python 中进行此操作,但我不确定我是否能够做到。感谢您提供的任何帮助!

【问题讨论】:

  • 你到底想要什么,再解释一下
  • 这非常广泛;您是否只想知道这是否可能使用 Python?
  • 我想我想要他尝试过什么

标签: python email csv export-to-csv outlook-2010


【解决方案1】:

这是一个相当广泛的问题,但这是可能的,使用 .NET COM 互操作

StackOverflow 帖子 here 介绍了类似的内容,您可以使用从收到的电子邮件中获取详细信息。

来自上述帖子的代码:

import win32com.client
import pythoncom
import re

class Handler_Class(object):
    def OnNewMailEx(self, receivedItemsIDs):
        # RecrivedItemIDs is a collection of mail IDs separated by a ",".
        # You know, sometimes more than 1 mail is received at the same moment.
        for ID in receivedItemsIDs.split(","):
            mail = outlook.Session.GetItemFromID(ID)
            subject = mail.Subject
            try:
                # Taking all the "BLAHBLAH" which is enclosed by two "%". 
                command = re.search(r"%(.*?)%", subject).group(1)

                print command # Or whatever code you wish to execute.
            except:
                pass


outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)

#and then an infinit loop that waits from events.
pythoncom.PumpMessages() 

Excel

然后,您将继续使用 interop for excel 以及相当广泛的教程 here.

【讨论】:

  • 谢谢 haddow64,我确实被卡住了,想弄清楚这是否可能 - 谢谢!
  • @haddow64 我目前正在尝试实施您的解决方案,但似乎无法找到有关 DispatchWithEvents 的文档。我可以和你核实一下它的工作原理吗?我必须将 Outlook 和一个类传递给 DispatchWithEvents - 如果我在类中有多个函数,它会调用哪个函数?另外,我怎么知道 DispatchWithEvents 会将 receivedItemsIDs 传递给 OneNewMailEx 函数?谢谢!
猜你喜欢
  • 2017-04-18
  • 1970-01-01
  • 2018-08-20
  • 2016-07-26
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
相关资源
最近更新 更多