18

I am using python 3.4 on windows 7. In order to open a doc file I am using this code:

import sys
import win32com.client as win32

word = win32.Dispatch("Word.Application")
word.Visible = 0
word.Documents.Open("MyDocument")
doc = word.ActiveDocument

I'M not sure why is this error popping up every time:

ImportError: no module named win32api

Although I have installed pywin32 from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32 and I have also checked the path from where I am importing. I have tried reinstalling pywin32 as well but that doesn't remove the error.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Maxxie
  • 397
  • 1
  • 4
  • 14

7 Answers7

19

Try to install pywin32 from here :

http://sourceforge.net/projects/pywin32/files/pywin32/

depends on you operation system and the python version that you are using. Normally 32bit version should works on both 32 and 64 bit OS.

EDIT: moved to https://github.com/mhammond/pywin32/releases

Rainald62
  • 706
  • 12
  • 19
Nima Soroush
  • 12,242
  • 4
  • 52
  • 53
17

This is a bug in the library itself, probably they used a different python implementation for creating this.

What they are trying to import is the site-packages\win32\win32api.pyd file, but the win32 folder is not in the path that python searches in, but site-packages is.

Try to replace the import win32api (inside win32com\__init__.py) to from win32 import win32api

TulkinRB
  • 590
  • 1
  • 6
  • 10
  • 1
    When neither (1) `pip install pypiwin32` nor (2) Nima's answer worked for me (Python 3.7 64 bit on Win 10), I successfully tried yours (3), but after a reboot (4), `import win32api` works too. Don't know whether (3) or (4) did the trick. May be (2) isn't needed either. – Rainald62 Dec 07 '19 at 12:24
13

I encountered the same error yestoday with Python 3.6.1 on Windows 7, and resolved it by "pip install pypiwin32".

flyisland
  • 397
  • 2
  • 8
2

Had the same error trying to import win32com.client (using Python 2.7, 64-bit). I agree with TulkinRB, there seem to be path issues, but the fix suggested did not work for me, since I also could not import win32.

Perhaps my fix will also work in Python 3.4.

Eventually, installing the .exe from SourceForge as an administrator (as suggested in Rina Rivera's answer here) allowed me to import win32com.client from IDLE, but not when I executed the script I was originally trying to run.

In the end, I discovered 3 differences in the sys.path that had been extended when I installed as admin and opened IDLE, but were not applied when executing a script. By extending the sys.path in my script, I was able to get rid of the import errors when I executed it:

import sys
sys.path.extend(('C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin'))

Finally, if you want more than a temporary fix, the sys.path could be permanently extended by establishing IDLESTARTUP or PYTHONSTARTUP variables (as described here and here).

Community
  • 1
  • 1
hanaQokus
  • 21
  • 1
1

You can create the __init.py file inside the win32 folder and then go inside the win32com folder and change its __init__.py file, where it is import win32api, change to from win32 import win32api

martineau
  • 119,623
  • 25
  • 170
  • 301
0

I ended up debugging and copying and pasting the necessary files into the appropriate folders. It's a work-around until the bug is fixed, but it works.

Bennybear
  • 335
  • 2
  • 13
0

from https://github.com/mhammond/pywin32/issues/1151#issuecomment-360669440

append the 'pypiwin32_system32' path to your system PATH,

in a script this can be done like:

import os
sitedir='C:/where_ever/'
os.environ["PATH"]+=(';'+os.path.join(sitedir,"pypiwin32_system32"))
...

from powershell

$env:PATH="$PATH;C:\where_ever\pywin32_system32";
python.exe ...

for help on site dir, see What is python's site-packages directory?

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147