Allen
Allen

Reputation: 1

word = comtypes.client.CreateObject('Word.Application') generates error

Windows 10, Anaconda Spyder, Python Trying to convert word '.doc' to PDF The first step fails

import comtypes.client

word = comtypes.client.CreateObject('Word.Application')


Get Error:

Traceback (most recent call last):

File "", line 1, in word = comtypes.client.CreateObject('Word.Application')

File "C:\ProgramData\Anaconda3\lib\site-packages\comtypes\client_init_.py", line 250, in CreateObject return _manage(obj, clsid, interface=interface)

File "C:\ProgramData\Anaconda3\lib\site-packages\comtypes\client_init_.py", line 188, in _manage obj = GetBestInterface(obj)

File "C:\ProgramData\Anaconda3\lib\site-packages\comtypes\client_init_.py", line 112, in GetBestInterface interface = getattr(mod, itf_name)

AttributeError: module 'comtypes.gen.Word' has no attribute '_Application'


Most websites seem to state that this should not happen???

Upvotes: 0

Views: 2671

Answers (2)

박해동
박해동

Reputation: 11

Thank you very much.

word = comtypes.client.CreateObject("Word.Application") ->run ok
excel = comtypes.client.CreateObject("Excel.Application") ->run ok
powerpoint = comtypes.client.CreateObject("Powerpoint.Application") ->run class err

I thought it was a python setting error, but it was resolved by changing the ms office installation.

Upvotes: 1

D4LI3N
D4LI3N

Reputation: 394

Problem:

This problem is caused by incorrect COM Interop settings in the Windows registry.
(not python or its libraries)

I've tested this using "comtypes" and "win32api", and multiple MS Office versions.

There seem to be issues with calls to COM objects,
regarding some MS Office versions.


Solution 1:

  1. Click on your Start menu and open the Control Panel

  2. Click on Uninstall a Program (or Add/Remove programs in Windows XP)

  3. Locate the entry for Microsoft Office and click on it. After you click on it, you should see a button labelled Change appear either next to it, or at the top of the list (depending on what version of Windows you have). Click this Change button.

  4. Once the Microsoft Office setup appears, choose the Repair option and click Next to have Microsoft Office repair itself. You may need to reboot your computer once this process is complete; Microsoft Office setup will tell you if you need to do this once it is done.


Solution 2:

Install MS Office versions that are tested and functional with COM calls.
Here are the results of the MS Office versions that I have tested:

Working MS Office versions: 2010, 2019, 365.
Non-working MS Office versions: 2007, 2013.


Useful COM registry paths to check:

MS Word x64:
"HKEY_CLASSES_ROOT\WOW6432Node\Interface{00020970-0000-0000-C000-000000000046}\TypeLib" MS Word x32:
"HKEY_CLASSES_ROOT \Interface{00020970-0000-0000-C000-000000000046}\TypeLib"
Both: "HKEY_CLASSES_ROOT\ WOW6432Node \TypeLib{00020970-0000-0000-C000-000000000046}"

  • 8.5 is for Office 2010
  • 8.6 is for Office 2013
  • 8.7 is for Office 2016

COM Interfaces:
"HKEY_CLASSES_ROOT\WOW6432Node\Interface{000C033A-0000-0000-C000-000000000046}\TypeLib\Version"

  • 2.5 if for Office 2010
  • 2.7 is for Office 2013
  • 2.8 is for Office 2016

"HKEY_CLASSES_ROOT\WOW6432Node\Interface{000C0339-0000-0000-C000-000000000046}\TypeLib\Version"

  • 2.5 if for Office 2010
  • 2.7 is for Office 2013
  • 2.8 is for Office 2016

Upvotes: 0

Related Questions