win32com.client is a module in the win32com package you need to import the actual module.
import win32com.client
w = win32com.client.Dispatch('Word.Application')
Answer from joojaa on Stack Overflowpython 3.x - Why am I suddenly getting a no attribute 'CLSIDToPackageMap' error with win32com.client? - Stack Overflow
AttributeError: module 'win32com.gen_py.00020905-0000-4B30-A977-D214852036FFx0x3x0' has no attribute 'CLSIDToClassMap'
win32com.client issue.
Win32Com.Client error with Python 3.4 - Stack Overflow
After deleting C:\Temp\gen_py, the code above works again. Hope it can save trouble!
PS. Or, you can try searching gen_py in your %TEMP% folder;
I found a more elegant solution on a Github discussion and incorporated it into a function. Worked for me.
def dispatch(app_name:str):
try:
from win32com import client
app = client.gencache.EnsureDispatch(app_name)
except AttributeError:
# Corner case dependencies.
import os
import re
import sys
import shutil
# Remove cache and try again.
MODULE_LIST = [m.__name__ for m in sys.modules.values()]
for module in MODULE_LIST:
if re.match(r'win32com\.gen_py\..+', module):
del sys.modules[module]
shutil.rmtree(os.path.join(os.environ.get('LOCALAPPDATA'), 'Temp', 'gen_py'))
from win32com import client
app = client.gencache.EnsureDispatch(app_name)
return app
SOLVED! Solution Below (Thanks, u/tankorsmash)
I'm trying to do something pretty basic, just use the COM api for OneNote to export the structure in xml.
import win32com.client
onenote = win32com.client.Dispatch("Onenote.Application")
one = ""
onenote.GetHierarchy(None, onenote.HierarchyScope.hsPages, one)
print(one)As per https://msdn.microsoft.com/en-us/library/office/jj680120.aspx, this should print in XML format. I think I'm fubaring the hsScope attribute though, because I get
Traceback (most recent call last):
File "C:/Users/johnsnow/PycharmProjects/untitled/Testing/Tests.py", line 5, in <module>
onenote.GetHierarchy(None, onenote.HierarchyScope.hsPages, one)
File "C:\Users\johnsnow\AppData\Local\Programs\Python\Python35-32\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Onenote.Application.HierarchyScope
Process finished with exit code 1You may have noticed that onenote.HierarchyScope.hsPages isn't written exactly like it is in the API documentation. Well, naturally python expects it to be a variable so it throws an error. However, in the doc, there is no variable defined as just OneNote. (Unless I missed something. I don't know C#) I even tried using a string to "trick" it into sending the request exactly as it is written in the C# code:
import win32com.client
onenote = win32com.client.Dispatch("Onenote.Application")
one = ""
three = "OneNote"
onenote.GetHierarchy(None, three.HierarchyScope.hsPages, one)
print(one)But I guess it passes the actual variable, because:
Traceback (most recent call last):
File "C:/Users/johnsnow/PycharmProjects/untitled/Testing/Tests.py", line 6, in <module>
onenote.GetHierarchy(None, three.HierarchyScope.hsPages, one)
AttributeError: 'str' object has no attribute 'HierarchyScope'
Process finished with exit code 1The library and COM object seem fine, as this:
import win32com.client
onenote = win32com.client.Dispatch("Onenote.Application")
one = ""
#onenote.GetHierarchy(None, onenote.HierarchyScope.hsPages, one)
print(onenote)Produces this:
<COMObject Onenote.Application> Process finished with exit code 0
And I've just run out of ideas. This is my first time coding, and I'm afraid I've bit off more than I can chew. Once I get this one error sorted, I'm pretty confident I can finish the project, but win32com has so little documentation, I just can't figure it on my own.
==========Solution============
So it turns out, the first node attribute needs to be a null string. Passing null as advised in the documentation doesn't work. The output to variable apparently doesn't work, either. Also, scope attribute needs to be a numerical value. hsPages just doesn't translate. See Enumeration section of API Documentation. Working code:
import win32com.client
onenote = win32com.client.Dispatch("Onenote.Application")
start = ""
test = onenote.GetHierarchy(start, 4)
print(test)Far simpler than I had ever hoped or realized. That being said, based on the documentation, I would never have found the solution.
I have written a program to read my outlook calendar (and write it to my google calendar), mostly just to practice some python/learn from it/for fun. Now this was all fun and games and it worked, although it needed some improvements.
However it stopped beging fun and games when it decided to break itself. Literally. I didn't chage a letter of code and suddenly it returns an errormessage.
The faulty bit of code seems to be:
import win32com.client
def read_outlook_calendar(some arguments):
Outlook = win32com.client.Dispatch("Outlook.Application")
more of this functionwhere this worked before, it now returns the following error:
File "syncal2.py", line 17, in <module>
appointments = fun.read_outlook_calendar(begin = begin, end = end)
File "C:\Users\Gebruiker\Documents\Agendaproject\tidy\fun.py", line 8, in read_outlook_calendar
Outlook = win32com.client.gencache.EnsureDispatch("Outlook.Application")
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 527, in EnsureDispatch
disp = win32com.client.Dispatch(prog_id)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\__init__.py", line 96, in Dispatch
return __WrapDispatch(dispatch, userName, resultCLSID, typeinfo, clsctx=clsctx)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\__init__.py", line 37, in __WrapDispatch
klass = gencache.GetClassForCLSID(resultCLSID)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 183, in GetClassForCLSID
mod = GetModuleForCLSID(clsid)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 226, in GetModuleForCLSID
mod = GetModuleForTypelib(typelibCLSID, lcid, major, minor)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 266, in GetModuleForTypelib
AddModuleToCache(typelibCLSID, lcid, major, minor)
File "C:\Users\Gebruiker\anaconda3\lib\site-packages\win32com\client\gencache.py", line 552, in AddModuleToCache
dict = mod.CLSIDToClassMap
AttributeError: module 'win32com.gen_py.00062FFF-0000-0000-C000-000000000046x0x9x6' has no attribute 'CLSIDToClassMap'
So yeah, how do I unfuck this?
I had the same issue and I resolved it by following the instructions here: https://mail.python.org/pipermail/python-win32/2007-August/006147.html
Deleting the gen_py output directory and re-running makepy SUCCEEDS and subsequently the test application runs OK again.
So the symptom is resolved, but any clues as to how this could have happened. This is a VERY long running application (think 24x7 for years) and I'm concerned that whatever caused this might occur again.
To find the output directory, run this in your python console / python session:
import win32com
print(win32com.__gen_path__)
or, even better, a one-liner in the command line:
python -c "import win32com; print(win32com.__gen_path__)"
Based on the exception message in your post, the directory you need to remove will be titled '00020813-0000-0000-C000-000000000046x0x1x9'. So delete this directory and re-run the code. And if you're nervous about deleting it (like I was) just cut the directory and paste it somewhere else.
💡Note that this directory is usually in your "TEMP" directory (copy-paste %TEMP%/gen_py in Windows File Explorer and you will arrive there directly).
I have no idea why this happens nor do I know how to prevent it from happening again, but the directions in the link I provided seemed to work for me.
A more straightforward solution was posted in a related question Issue in using win32com to access Excel file.
Basically, you just need to delete the folder C:\Users\<your username>\AppData\Local\Temp\gen_py and rerun your code.
💡TIP: You can also put in your Windows file explorer %TEMP%\gen_py to access it directly, and then delete its content.