This error is caused by a problem with how the "pywin32" package is installed in your Python environment. This can happen after pywin32 has been upgraded, or if an old version is left over from another Python installation.
The problem occurs because the Python module win32api or win32com loads the wrong version of its DLL component, either "pythoncomXX.dll" or "pywintypesXX.dll".
To resolve this issue you should completely uninstall the pywin32 package and remove and old versions of the "pythoncomXX.dll" and "pywintypesXX.dll" DLLs. The exact name of these will depend on your version of Python.
To completely uninstall pywin32 perform the following steps:
NOTE: In these steps below I have used "pythoncomXX.dll" and "pywintypesXX.dll". You should replace the XX with your version of Python! For example, if you are using Python 3.7 these files will be named "pythoncom37.dll" and "pywintypes37.dll".
- If you are using Anaconda, uninstall pywin32 using "conda uninstall pywin32"
- Uninstall pywin32 using "pip uninstall pywin32". If you are using Anaconda you should still perform this step in case pywin32 has been installed using pip.
- Search for any files called "pythoncomXX.dll" or "pywintypesXX.dll" in your Python folder (you should be able to use the Search feature in Windows Explorer to find them). If you are not sure where your Python environment is located use "where python" to find it. There should be none left after uninstalling so if you find any then delete them.
- Search for "pythoncomXX.dll" and "pywintypesXX.dll" in "C:\Windows\System32". These dlls may have been installed here too, and if they are then that could be what's conflicting with the current version. If you find these files move them somewhere else and rename them (eg pywintypesXX.dll -> pywintypesXX.dll.old) so they will no longer conflict (you could also delete them, I only recommend moving and renaming them in case you later find some other application requires them, but usually that will not be a problem).
Once you are satisfied that the pywin32 package is completely uninstalled and you have removed or renamed all old versions of the DLLs then re-install pywin32 and you should now be able to import win32api and win32com successfully.
To install pywin32 use the following command:
pip install pywin32
Or if you are using Anaconda you can use:
conda install pywin32