繁体   English   中英

pywintypes.com_error:(-2147221008,'CoInitialize 尚未被调用。',无,无)

[英]pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

当我尝试按原样运行此代码时,我收到此错误“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 中的自动完成在键入 pythoncom.CoInitialize() 时没有收到任何内容,所以它让我认为它是过时的信息。 Strive Sun 也解释了相同的信息

第一的:

import pythoncom

然后设置:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM