简体   繁体   English

Python 中的 win32.Dispatch 与 win32.gencache。 优缺点都有什么?

[英]win32.Dispatch vs win32.gencache in Python. What are the pros and cons?

I have been recently using win32com.client from python as an API for windows applications but am struggling to understand some basic things.我最近一直在使用 python 中的 win32com.client 作为 Windows 应用程序的 API,但我很难理解一些基本的东西。

I had been using it with a program called WEAP, in the following way我一直将它与一个名为 WEAP 的程序一起使用,如下所示

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

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

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

Does anyone know the difference between using有谁知道使用之间的区别

win32.Dispatch 

and

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: Open Excel Workbook using Win32 COM Api Python:使用 Win32 COM Api 打开 Excel 工作簿

win32com.client.Dispatch works but not win32com.client.gencache.EnsureDispatch win32com.client.Dispatch 工作但不 win32com.client.gencache.EnsureDispatch

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

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

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

Any advice would be greatly appreciated.任何建议将不胜感激。

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.我会尽快回答(最后不会那么短......)你的问题,但我不是专家。

When you create a COM object with python, how python knows what methods and parameters are available for this object?用python创建COM对象时,python怎么知道这个对象有哪些方法和参数? This is related to the notion of early and late binding.这与早期晚期绑定的概念有关。

If you try to create a COM object you never used before with Dispatch , you won't know what is available with your object.如果您尝试使用Dispatch创建一个您以前从未使用过的COM对象,您将不知道您的对象可以使用什么。 If I do in a Jupyter QtConsole:如果我在 Jupyter QtConsole 中这样做:

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

Then trying xl_dis.然后尝试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".我在后期绑定的情况下,“python 不知道对象可以做什么”。

If I do the same thing with EnsureDispatch :如果我对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>

First, you can see the difference on the output and then if I do xl_ens.首先,您可以看到输出的差异,然后如果我执行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".我现在处于早期绑定状态,并且“python 知道对象可以做什么”。

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

Now, if you try again in a new console using Dispatch , you will get the exact same result.现在,如果您使用Dispatch在新控制台中再次尝试,您将获得完全相同的结果。 Indeed, after using EnsureDispatch , the folder created before in win32com\\gen_py still exists and "python still knows what the object can do".确实,在使用了EnsureDispatch ,之前在win32com\\gen_py创建的文件夹仍然存在,并且“python 仍然知道该对象可以做什么”。 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).要自己进行实验,请转到您的文件夹\\win32com\\gen_py并删除包含 excel 信息的文件夹(对我而言,名称是00020813-0000-0000-C000-000000000046x0x1x7 ,不确定它是否与您相同)。

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.最后,两者之间的一个区别主要是在您第一次创建COM对象时是否强制进行早期绑定,但如果与您的COM对象相关的文件夹已经存在于\\win32com\\gen_py ,则没有太大区别。

These two sentences of the link I gave:我给的链​​接的这两句话:

To force the use of early binding to access COM objects, you must force the MakePy process in your code.要强制使用早期绑定来访问 COM 对象,您必须在代码中强制使用 MakePy 过程。 Once you have ensured the MakePy support exists, use win32com.client.Dispatch() as usual.确保存在 MakePy 支持后,照常使用 win32com.client.Dispatch()。 It always returns the MakePy-supported wrappers for your COM object.它始终为您的 COM 对象返回 MakePy 支持的包装器。

To force the MakePy process, the win32com.client.gencache module is used.为了强制 MakePy 进程,使用了 win32com.client.gencache 模块。 This module contains the code that manages the directory of MakePy-generated source files: the generated cache, or gencache.该模块包含管理 MakePy 生成的源文件目录的代码:生成的缓存或 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.该模块中有许多有用的功能,如果您需要对这些生成的文件进行高级管理,建议您浏览源文件。

kind of summary this.有点总结这个。

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.另一种选择是使用dynamic例如win32.dynamic.Dispatch("Excel.Application")并且您将始终在后期绑定中获得COM对象。

Location of the generated cache may be in USER_PROFILE\\AppData\\Local\\Temp\\gen_py\\PYTHON_VERSION\\ .生成的缓存的位置可能在 USER_PROFILE\\AppData\\Local\\Temp\\gen_py\\PYTHON_VERSION\\ 中。 This can be useful if one wants to clear the cache.如果想要清除缓存,这会很有用。

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

相关问题 是什么阻止 win32.Dispatch() 打开 Microsoft Office 程序? - What is stopping win32.Dispatch() from opening Microsoft Office programs? 监控通过 Python win32.Dispatch('Outlook.Application') 发送的电子邮件? - Monitor E-mail sent via Python win32.Dispatch('Outlook.Application')? win32com.client.Dispatch有效,但不是win32com.client.gencache.EnsureDispatch - win32com.client.Dispatch works but not win32com.client.gencache.EnsureDispatch Word = win32.Dispatch(&quot;Word.Application&quot;) 尽管有 Word.Visible = False 命令,但每隔一次显示打开的文件 - Word = win32.Dispatch("Word.Application") Displays the opened file every other time despite Word.Visible = False command win32api与Python - win32api vs Python Python win32com.client调度和创建Shorcut方法 - Python win32com.client Dispatch and Create Shorcut Methods Python-Win32Com客户端调度按计划任务挂起 - Python - Win32Com Client Dispatch Hanging as Scheduled Tasks win32com.client.gencache.EnsureDispatch(“ Outlook.Application”)不起作用 - win32com.client.gencache.EnsureDispatch(“Outlook.Application”) not working win32client分发在python中失败,而win32 :: ole new在perl中成功为com dll运行 - win32client dispatch fails in python while win32::ole new runs successfully in perl for a com dll Python Win32服务 - Python win32 service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM