Python 3.4 :ImportError: no module named win32api

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 11:12:09

问题


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

M not sure why is this error popping up everytime

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 doesnt remove the error.....

any suggestions....please help

Thanks


回答1:


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.




回答2:


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




回答3:


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




回答4:


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).




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/25257274/python-3-4-importerror-no-module-named-win32api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!