The following code works well, but it fails if executed from a CherryPy app method with the error message CoInitialize has not been called
:
import win32com.client
xl = win32com.client.Dispatch("Excel.Application")
xl.quit()
This post suggests a solution that works for me:
import pythoncom
pythoncom.CoInitialize()
The reason I'm asking about a problem for which I already have a solution, is that (1) I would like to know what I'm doing (rather than doing it only because I've seen it working once) and (2) I don't want to risk to miss something important (and reading this post makes me think that I am missing something.)
I couldn't find any documentation for pythoncom.CoInitialize()
, and the source of pythoncom is the following three lines that don't help me (nor Eclipse+pydev which says that the method does not exist):
# Magic utility that "redirects" to pythoncomxx.dll
import pywintypes
pywintypes.__import_pywin32_system_module__("pythoncom", globals())