我制作了一些 python 脚本来控制外部 CATIA 应用程序。我现在必须将这些脚本打包成可执行文件,但我无法做到。
问题 :
构建脚本后,如何强制 win32com 对特定模块使用早期绑定?
细节 :
我的脚本使用win32com.client模块控制 CATIA 应用程序。我使用后期绑定处理 CATIA,但模块CATIA V5 SpaceAnalysisInterfaces Object Library包含具有参考输入/输出参数的函数。对于这个我使用早期绑定,遗憾的是,简单地使用MakePy还不够,我不得不修改从win32com.gen_py包生成的源代码,以从输入/输出参数中获得正确的行为。现在当我执行 python 脚本时它工作正常。但是,如果我使用py2exe或cx_freeze构建它们,则可执行文件仅使用后期绑定,因此我得到了不好的结果。
这是我挂钩 CATIA 应用程序并使用其 API 的方式:
import win32com.client
buff = [0, 0, 0]
catApp = win32com.client.GetActiveObject("CATIA.Application") # Late bind needed
doc = catApp .Documents.Open(path)
part = doc.Part # This property fails if using early binding
spa = doc.GetWorkbench(u"SPAWorkbench")
I = spa.Inertias.Add(part) # Early bind needed
cogCoords = I.GetCOGPosition(buff) # The damn input/ouput argument function
这是我使用cx_freeze的构建脚本:
from cx_Freeze import setup, Executable
options = {
"includes": [],
"excludes": [],
"packages": ["win32com.gen_py"]
}
target = Executable(
script = "test.py",
base = "Console",
compress = True,
icon = None,
)
setup(
name = "Test",
version = "1.0",
description = "Early Binding Test Built",
author = "C.LECLERC",
options = {"build_exe": options},
executables = [target]
)
这个构建脚本会生成一堆文件,包括我实际的win32com.gen_py模块的内容,所以它应该可以工作。但是当我执行文件时,它只使用后期绑定。当我使用py2exe时,我无法检查文件是否正确添加,但行为完全相同:后期绑定!
我看了一下这篇文章,但我的问题有所不同。模块被正确复制并且脚本不会引发异常。输入/输出功能无法正常工作。
任何帮助,将不胜感激。