Python 导入错误:没有名为 win32com.client 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23864234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:34:02  来源:igfitidea点击:

ImportError: No module named win32com.client

pythonexcelwin32com

提问by user3194189

I am currently using python 2.7 and trying to open an Excel sheet. When using the code below:

我目前正在使用 python 2.7 并尝试打开 Excel 工作表。使用以下代码时:

import os
from win32com.client import Dispatch

xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Visible = True
# Open the file we want in Excel
workbook = xlApp.Workbooks.Open('example.xls')

I get this error:

我收到此错误:

ImportError: No module named win32com.client

导入错误:没有名为 win32com.client 的模块

Is there any possibility of getting the error since I am using 64-bit Windows machine?

由于我使用的是 64 位 Windows 机器,是否有可能出现错误?

回答by Nishant Thapliyal

win32com.clientis a part of pywin32

win32com.clientpywin32的一部分

So, download pywin32 from here

所以,从这里下载 pywin32

回答by Mitch44

pip install pywin32didn't work for me but pypiwin32did.

pip install pywin32没有为我工作,但pypiwin32做了。

回答by janani selvan

Try both pip install pywin32and pip install pypiwin32.

pip install pywin32和都试试pip install pypiwin32

It works.

有用。

回答by Huston94

I don't have any experience with this particular library (I'm just getting into it myself), but I did notice that you only imported the Dispatch class from the module. So you either need to just change your import as such:

我对这个特定的库没有任何经验(我只是自己动手),但我确实注意到您只从模块中导入了 Dispatch 类。所以你要么只需要改变你的导入:

import win32com.client

ORchange your instance call like so:

或者像这样更改您的实例调用:

xlApp = Dispatch("Excel.Application")

回答by Sponge Bob

Try to install the "pywin32" file, you can find in https://github.com/mhammond/pywin32/releases

尝试安装“pywin32”文件,你可以在https://github.com/mhammond/pywin32/releases 中找到

Install the version that you use in your IDLE, and try to install, after you can open your project and compile another turn!

安装你在IDLE中使用的版本,然后尝试安装,打开你的项目再编译一遍即可!

thanks !

谢谢 !

回答by Anil M S

Try this command:

试试这个命令:

pip install pywin32

Note

笔记

If it gives the following error:

如果它给出以下错误:

Could not find a version that satisfies the requirement pywin32>=223 (from pypiwin32) (from versions:)
No matching distribution found for pywin32>=223 (from pypiwin32)

找不到满足要求的版本 pywin32>=223(来自 pypiwin32)(来自版本:)
找不到 pywin32>=223(来自 pypiwin32)的匹配发行版

upgrade 'pip', using:

升级“pip”,使用:

pip install --upgrade pip

回答by Olivier

Had the exact same problem and none of the answers here helped me. Till I find this thread and post

有完全相同的问题,这里的答案都没有帮助我。直到我找到这个帖子并发布

Short: win32 modules are not guaranted to install correctly with pip. Install them directly from packages provided by developpers on github. It works like a charm.

简而言之:不能保证使用 pip 正确安装 win32 模块。 直接从开发人员在 github 上提供的包中安装它们。它就像一个魅力。

回答by Mike - SMT

I realize this post is old but I wanted to add that I had to take an extra step to get this to work.

我意识到这篇文章很旧,但我想补充一点,我必须采取额外的步骤才能让它发挥作用。

Instead of just doing:

而不是仅仅做:

pip install pywin32

I had use use the -mflag to get this to work properly. Without it I was running into an issue where I was still getting the error ImportError: No module named win32com.

我曾使用该-m标志来使其正常工作。没有它,我遇到了一个问题,我仍然收到错误ImportError: No module named win32com

So to fix this you can give this a try:

因此,要解决此问题,您可以尝试一下:

python -m pip install pywin32

This worked for me and has worked on several version of python where just doing pip install pywin32did not work.

这对我有用,并且已经在几个版本的 python 上pip install pywin32工作,只是这样做不起作用。

Versions tested on:

测试版本:

3.6.2, 3.7.6, 3.8.0, 3.9.0a1.

3.6.2、3.7.6、3.8.0、3.9.0a1。

回答by oetzi

in some cases where pywin32 is not the direct reference and other libraries require pywin32-ctypes to be installed; causes the "ImportError: No module named win32com" when application bundled with pyinstaller.

在某些情况下,pywin32 不是直接引用,其他库需要安装 pywin32-ctypes;当应用程序与 pyinstaller 捆绑在一起时,会导致“ImportError: No module named win32com”。

running following command solves on python 3.7 - pyinstaller 3.6

在 python 3.7 上运行以下命令可以解决 - pyinstaller 3.6

pip install pywin32==227