如何在PyCharm中获得用于COM编程的代码完成功能? [英] How to get code-completion for COM programming in PyCharm?

查看:210
本文介绍了如何在PyCharm中获得用于COM编程的代码完成功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用app = win32com.client.Dispatch('Some.Application')时,有没有可行的方法在PyCharm中获得代码完成?必须重新输入(或复制-粘贴)API文档中的所有内容,这很繁琐,因此创建骨架是.没有其他方法可以让PyCharm知道通过COM提供的接口,特别是如果我可以提供.tlb文件的话?还是至少有某种方法可以从TypeLib自动生成这样的框架(或包装模块?)?

When using app = win32com.client.Dispatch('Some.Application'), is there any feasible way get code-completion in PyCharm? It is rather tedious having to retype (or copy-paste) everything from an API documentation, so would creating skeletons be. Is there no other way to let PyCharm know about the Interface provided via COM, especially if I can provide a .tlb file? Or is there at least some way automatically generate such a skeleton (or a wrapping module?) from the TypeLib?

推荐答案

由于PyCharm无法知道app的运行时类型,因此您不应期望直接在app上获得代码完成;至少直到他们决定添加内置支持以从类型库生成代码为止.

Since there is no way for PyCharm to know the runtime type of app, you shouldn't expect to get code completion on app directly; at least not until they decide to add built-in support for generating code from type libraries.

但是,您可以利用以下事实:win32com根据类型库隐式生成代码,如第一部分中所述答案,以及PyCharm对类型提示的支持,以在COM方法上获得代码完成.

However, you can exploit the fact that win32com implicitly generates code based on the type library as described in the first part of this answer, together with PyCharm's support for type hinting, to get code completion on COM methods.

  1. 确保已生成Python类型.它们的位置由COM对象的GUID确定.例如,我的计算机上Microsoft Word 2016的类型可在 C:\Users\username\appdata\local\temp\gen_py\3.6\00020905-0000-0000-c000-000000000046x0x8x7\.
  2. 将此文件夹添加到PyCharm Python解释器的路径;参见例如此答案.
  3. 导入要为其完成代码的模块.
  1. Make sure that the Python types have been generated; their location is determined by the GUID of the COM object. For example, the types for Microsoft Word 2016 on my machine are available in C:\Users\username\appdata\local\temp\gen_py\3.6\00020905-0000-0000-c000-000000000046x0x8x7\.
  2. Add this folder to the path of your PyCharm Python interpreter; see e.g. this answer.
  3. Import the modules for which you want code completion.

在下面的屏幕截图中,我们将这种方法与Word的

In the screenshots below, we use this approach with Word's Find:

现在,这种方法除了感到肮脏之外,还依赖于已生成的相关类型,并且代码完成仅限于对象所发布的方法,因此我认为它在实践中的实用性可能会受到一定程度的限制;特别是,任何从事代码工作的人都必须生成代码,否则注释将导致NameError.就我个人而言,我可能更愿意在执行过程的探索部分使用Jupyter,并且在上面提到的答案中概述了一些最小的调整. >,Jupyter可以扩展为具有win32com的完整代码完成.

Now, besides feeling dirty, this approach relies on the relevant types having been generated and the code completion is limited to the methods published by the object, so I imagine its usefulness in practice might be somewhat limited; in particular, anybody working on the code will have to generate the code, or the annotations will cause NameErrors. Personally, I would probably prefer using Jupyter for the exploratory part of the implementation process, and with minimal tweaks outlined in the answer mentioned above, Jupyter can be extended to have full code completion with win32com.

这篇关于如何在PyCharm中获得用于COM编程的代码完成功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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