How to get code-completion for COM programming in PyCharm?(如何在 PyCharm 中完成 COM 编程的代码?)
问题描述
当使用 app = win32com.client.Dispatch('Some.Application')
时,有没有可行的方法在 PyCharm 中完成代码?必须从 API 文档重新键入(或复制粘贴)所有内容相当繁琐,因此创建
现在,除了感觉很脏之外,这种方法依赖于已经生成的相关类型,并且代码完成仅限于对象发布的方法,所以我想它在实践中的用处可能会有所限制;特别是,任何处理代码的人都必须生成代码,否则注释将导致 NameError
s.就个人而言,我可能更喜欢将 Jupyter 用于实施过程的探索性部分,并且在上述答案,Jupyter 可以通过 win32com
进行扩展以实现完整的代码补全.
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?
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.
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.
- 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:Usersusernameappdatalocal empgen_py3.6 0020905-0000-0000-c000-000000000046x0x8x7
. - Add this folder to the path of your PyCharm Python interpreter; see e.g. this answer.
- Import the modules for which you want code completion.
In the screenshots below, we use this approach with Word's Find
:
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 NameError
s. 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 编程的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PyCharm 中完成 COM 编程的代码?


基础教程推荐
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01