Python 3.4 :ImportError: no module named win32api

前端 未结 6 1409
萌比男神i
萌比男神i 2020-11-28 11:26

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.         


        
6条回答
  •  旧巷少年郎
    2020-11-28 11:59

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

提交回复
热议问题