I'm newbie in python and trying to use a watchdog module to control if any new files are created in some directory. Everytime a new file is there I want watchdog to create an email via outlook and set the new created file as attachment.
Here is my code:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import win32com.client as win32
class MyHandler(FileSystemEventHandler):
def on_created(self, event):
created_file = event.src_path.strip()
print(type(created_file), created_file)
# After that will be some checking of the created file
# then creating an email item, which makes an exception below
outlook = win32.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)
mail.To = ' '
mail.cc = ' '
mail.Subject = ' '
mail.Body = ' '
mail.Display()
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path=f'some_path', recursive=False)
observer.start()
while True:
try:
pass
except KeyboardInterrupt:
observer.stop()
#end of the code
But I get the following (sorry for long-read). Importing of pythoncom and adding argument to outlook = win32.Dispatch("Outlook.Application, pythoncom.CoInitialize()) did not work in my case or I did something wrong.
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\dynamic.py", line 86, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\site-packages\watchdog\observers\api.py", line 204, in run
self.dispatch_events(self.event_queue)
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\site-packages\watchdog\observers\api.py", line 380, in dispatch_events
handler.dispatch(event)
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\site-packages\watchdog\events.py", line 287, in dispatch
{
File "C:\Users\Username\PycharmProjects\pythonProject1\TEST WATCHDOG.py", line 25, in on_created
outlook = win32.Dispatch("Outlook.Application")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\__init__.py", line 117, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Username\AppData\Local\Programs\Python\Python311\Lib\site-packages\win32com\client\dynamic.py", line 88, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)
The How to use python and windows com in pyramid (threads)? post suggests a solution that could work for you:
Also I see the same error messages when Outlook is automated from a task scheduler. Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
In that case you may consider using any components that doesn't require Outlook to be automated or
EWS(Graph API in case of Office365) if Exchange is used as an email server.