gpt4 book ai didi

python - pywintypes.com_error : (-2147221008, 'CoInitialize has not been called.',无,无)

转载 作者:行者123 更新时间:2023-12-05 08:50:54 25 4
gpt4 key购买 nike

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

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()

最佳答案

首先:

import pythoncom

然后设置:

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

关于python - pywintypes.com_error : (-2147221008, 'CoInitialize has not been called.',无,无),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61071022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com