【问题标题】:pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)pywintypes.com_error:(-2147221008,'CoInitialize 尚未被调用。',无,无)
【发布时间】:2020-04-07 00:33:47
【问题描述】:

当我尝试按原样运行此代码时,我收到此错误“IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)" ,但是如果我单独运行 stp_tracker 它工作正常,如果我单独运行 notify stp 它工作正常。我感谢任何人的意见。谢谢

import time
import win32com.client
# import sys
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
# import watchdog


class MyHandler(PatternMatchingEventHandler):
    patterns = ["*.stp", "*.step", "*.txt"]

    def process(self, event):
        """
        event.event_type
            'modified' | 'created' | 'moved' | 'deleted'
        event.is_directory
            True | False
        event.src_path
            path/to/observed/file
        """
        # the file will be processed there
        print(event.src_path, event.event_type)

    def on_modified(self, event):
        self.process(event)
        notify_stps()

    def on_created(self, event):
        self.process(event)
        notify_stps()

    def on_deleted(self, event):
        self.process(event)
        notify_stps()


def stp_tracker():
    # /if __name__ == '__main__':
    path = r"W:\TestFolder"
    observer = Observer()
    observer.schedule(MyHandler(), path)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()


def notify_stps():
    const = win32com.client.constants
    olMailItem = 0x0
    obj = win32com.client.Dispatch("Outlook.Application")
    newMail = obj.CreateItem(olMailItem)
    newMail.Subject = "I AM SUBJECT!!"
    newMail.Body = "Step files in directory"
    # newMail.BodyFormat = 2 # olFormatHTML https://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx
    # newMail.HTMLBody = "<HTML><BODY>Enter the <span style='color:red'>message</span> text here.</BODY></HTML>"
    newMail.To = 'Acoker251@outlook.com'
    # attachment1 = r"C:\Temp\example.pdf"
    # newMail.Attachments.Add(Source=attachment1)

    newMail.Send()


stp_tracker()

【问题讨论】:

  • 为此道歉,但搜索互联网,我发现了一些有用的东西。我之前遇到过同一篇文章,并认为它是不推荐使用的信息,因为我在 pycharm 中的 AutoComplete 在键入 pythoncom.CoInitialize() 时没有拾取任何东西,所以它认为它是过时的信息。
  • 添加pythoncom.CoInitialize(),参考:CoInitialize has not been called

标签: python winapi events pywin32 watchdog


【解决方案1】:

为此道歉,但搜索互联网,我发现了一些有用的东西。我之前遇到过同一篇文章,并认为它是不推荐使用的信息,因为我在 pycharm 中的 AutoComplete 在键入 pythoncom.CoInitialize() 时没有拾取任何东西,所以它让我认为它是过时的信息。 Strive Sun 也解释了相同的信息

【讨论】:

    【解决方案2】:

    第一:

    import pythoncom
    

    然后设置:

    xl=win32com.client.Dispatch("Excel.Application",pythoncom.CoInitialize())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      相关资源
      最近更新 更多