简体   繁体   English

py2exe转换的脚本无法正确运行win32com.client

[英]py2exe converted script does not run win32com.client correctly

I have seen a couple posts related to my issue on other sites, but nothing worked. 我在其他网站上看到了几则与我的问题有关的帖子,但没有任何效果。 To make a long story short, my program importa win32com.client to access Microsoft Word. 长话短说,我的程序importa win32com.client访问Microsoft Word。 I create a standalone executable using py2exe and every time the user selects the option to open MS Word I get a KeyError. 我使用py2exe创建一个独立的可执行文件,每当用户选择打开MS Word的选项时,我都会得到一个KeyError。 Below is the code which the compiler claims the error is: 下面是编译器声称错误的代码:

# Call the MS Word app
MS_Word = win32com.client.gencache.EnsureDispatch('Word.application')

And below is the result when the program run this particular line: 下面是程序运行此特定行时的结果:

Exception in Tkinter callback
Traceback (most recent call last):
File "Tkinter.pyc", line 1536, in __call__
File "PROTOTYPE_PCE.PY", line 46, in SCAN
File "win32com\client\gencache.pyc", line 544, in EnsureDispatch
File "win32com\client\CLSIDToClass.pyc", line 46, in GetClass
KeyError: '{00020970-0000-0000-C000-000000000046}'

I am using Tkinter as well, but it is NOT the source of the issue. 我也正在使用Tkinter,但这不是问题的根源。 Opening MS Word from the program is a new feature I have added and it only fails when I create the standalone application. 从程序中打开MS Word是我添加的一项新功能,只有在创建独立应用程序时它才会失败。 I have also tried Pyinstaller and I my line of errors only increased. 我也尝试了Pyinstaller,但我的错误仅在增加。 Thanks in advance! 提前致谢!

OKAY! 好的! So for some reason the library.zip file that py2exe creates after being run does not allow for modules like win32com.client to import into the program. 因此,由于某些原因,py2exe在运行后创建的library.zip文件不允许将win32com.client之类的模块导入程序。 Why? 为什么? I really do not know I am a noob at this stuff. 我真的不知道我是这个菜的菜鸟。 Anyway the following solution works VERY well, as if I initially had no problem at all. 无论如何,以下解决方案非常有效,好像我最初一点都没有问题。 This is what should be included in the setup.py script. 这就是应该包含在setup.py脚本中的内容。 Taken from another post. 取自另一篇文章。 I hope this helps someone :) 我希望这可以帮助别人 :)

setup(  
...

zipfile="foo/bar.zip", 
options={"py2exe": {"skip_archive": True}})

Ignoring library.zip in py2exe 忽略py2exe中的library.zip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM