Python 中的 win32.Dispatch 与 win32.gencache.优缺点都有什么? [英] win32.Dispatch vs win32.gencache in Python. What are the pros and cons?

查看:234
本文介绍了Python 中的 win32.Dispatch 与 win32.gencache.优缺点都有什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用 python 中的 win32com.client 作为 Windows 应用程序的 API,但我很难理解一些基本的东西.

I have been recently using win32com.client from python as an API for windows applications but am struggling to understand some basic things.

我一直在一个名为 WEAP 的程序中使用它,方法如下

I had been using it with a program called WEAP, in the following way

import win32com.client
win32com.client.Dispatch("WEAP.WEAPApplication")

现在,我想将它与 Excel 一起使用,并找到了前几行的替代方法,其中之一如下(取自 Python:使用Win32 COM Api打开Excel工作簿)

Now, I want to use it with Excel and have found alternatives to the previous lines, one of them as follows (taken from Python: Open Excel Workbook using Win32 COM Api)

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')

有谁知道使用

win32.Dispatch 

win32.gencache.EnsureDispatch

和其他替代品?有谁知道每一种的优缺点?或者关于何时应该使用一个或另一个的一些建议?

and other alternatives? Does anyone know pros and cons of each one? or some advice regarding when one or another should be used?

我寻求建议并找到了一些有用的答案,例如:

I have looked for advice and i have found some useful answers, eg:

Python:使用 Win32 COM Api 打开 Excel 工作簿

win32com.client.Dispatch 有效但 win32com 无效.client.gencache.EnsureDispatch

http://pythonexcels.com/python-excel-mini-cookbook/

https://mail.python.org/pipermail/python-win32/2011-August/011738.html

然而,他们通常专注于回答特定问题,而不是描述 Dispatch、gencache.EnsureDispatch 和其他替代方案之间差异的更大图景,这正是我想要的.

However, they are usually focused on answering specific issues, and not describing the bigger picture of the differences between Dispatch, gencache.EnsureDispatch, and perhaps further alternatives, which is what i want.

任何建议将不胜感激.

推荐答案

关于它你需要阅读的一件事是 此链接.

One thing about it you need to read is this link.

我会尽快回答你的问题(最后不会那么短......),但我不是专家.

I will try to answer shortly (finally not so short by the end...) your question, but I'm not a expert.

当你用python创建一个COM对象时,python如何知道这个对象有哪些方法和参数可用?这与早期晚期绑定的概念有关.

When you create a COM object with python, how python knows what methods and parameters are available for this object? This is related to the notion of early and late binding.

如果您尝试使用 Dispatch 创建一个您以前从未使用过的 COM 对象,您将不知道您的对象可以使用什么.如果我使用 Jupyter QtConsole:

If you try to create a COM object you never used before with Dispatch, you won't know what is available with your object. If I do in a Jupyter QtConsole:

import win32com.client as win32
xl_dis = win32.Dispatch("Excel.Application")
xl_dis
Out[3]: <COMObject Excel.Application>

然后尝试 xl_dis. 看看我之后能做什么,我别无选择.我在后期绑定的情况下,python不知道对象可以做什么".

Then trying xl_dis. to see what I can do after, I won't get any choice. I'm in the case of a late binding, "python does not know what the object can do".

如果我用 EnsureDispatch 做同样的事情:

If I do the same thing with EnsureDispatch:

import win32com.client as win32
xl_ens = win32.gencache.EnsureDispatch("Excel.Application")
xl_ens
Out[3]: <win32com.gen_py.Microsoft Excel 14.0 Object Library._Application instance at 0x35671240>

首先,您可以看到输出的差异,然后如果我执行 xl_ens.,我将获得一些可用的方法和参数.我现在处于早期绑定并且python知道对象可以做什么".

First, you can see the difference on the output and then if I do xl_ens. I will get some methods and parameters available. I'm now in early binding and "python knows some of what the object can do".

发生的情况是 EnsureDispatch 首先强制运行 makepy.py(查看您的文件夹 Lib\site-packages\win32com\client>) 在 Lib\site-packages\win32com\gen_py 中创建一个文件夹,其中包含 Python 脚本以及与此 COM 对象相关的一些方法和参数.

What happens is that EnsureDispatch forces to run makepy.py at first (look in your folder Lib\site-packages\win32com\client) to create a folder in Lib\site-packages\win32com\gen_py containing python scripts with some methods and parameters related to this COM object.

现在,如果您使用 Dispatch 在新控制台中再次尝试,您将获得完全相同的结果.确实,在使用EnsureDispatch之后,之前在win32com\gen_py中创建的文件夹仍然存在,并且python仍然知道该对象可以做什么".要自己试验,请转到您的文件夹 \win32com\gen_py 并删除包含 excel 信息的文件夹(对我而言,名称是 00020813-0000-0000-C000-000000000046x0x1x7,不确定它对你来说是一样的).

Now, if you try again in a new console using Dispatch, you will get the exact same result. Indeed, after using EnsureDispatch, the folder created before in win32com\gen_py still exists and "python still knows what the object can do". To experiment it yourself, go to your folder \win32com\gen_py and delete the folder with excel information (for me, the name is 00020813-0000-0000-C000-000000000046x0x1x7, not sure it is the same for you).

最后,两者的区别主要是第一次创建COM对象时是否强制提前绑定,但是如果文件夹与你的COM对象相关\win32com\gen_py 中已经存在,则没有太大区别.

Finally, one difference between both is mainly to force or not the early binding the first time you create a COM object, but if the folder related to your COM object already exist in \win32com\gen_py, then not much difference.

我给的链接的这两句话:

These two sentences of the link I gave:

要强制使用早期绑定来访问 COM 对象,您必须在代码中强制使用 MakePy 进程.确保存在 MakePy 支持后,照常使用 win32com.client.Dispatch().它始终为您的 COM 对象返回 MakePy 支持的包装器.

To force the use of early binding to access COM objects, you must force the MakePy process in your code. Once you have ensured the MakePy support exists, use win32com.client.Dispatch() as usual. It always returns the MakePy-supported wrappers for your COM object.

为了强制 MakePy 进程,使用了 win32com.client.gencache 模块.该模块包含管理 MakePy 生成的源文件目录的代码:生成的缓存或 gencache.本模块中有许多有用的功能,如果您需要对这些生成的文件进行高级管理,建议您浏览源文件.

To force the MakePy process, the win32com.client.gencache module is used. This module contains the code that manages the directory of MakePy-generated source files: the generated cache, or gencache. There are a number of useful functions in this module, and you are encouraged to browse the source file if you need to perform advanced management of these generated files.

总结一下.

另一种选择是使用 dynamic,例如 win32.dynamic.Dispatch("Excel.Application"),您将始终获得 COM 后期绑定中的对象.

The other alternative is to use dynamic such as win32.dynamic.Dispatch("Excel.Application") and you will always get a COM object in late binding.

这篇关于Python 中的 win32.Dispatch 与 win32.gencache.优缺点都有什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆