[文摘]Quick Start to Client side COM and Python
摘自:PyWin32.chm
Introduction
This documents how to quickly start using COM from Python. It is not a thorough discussion of the COM system, or of the concepts introduced by COM.
Other good information on COM can be found in various conference tutorials - please see the collection of Mark's conference tutorials
For information on implementing COM objects using Python, please see a Quick Start to Server side COM and Python
In this document we discuss the following topics:
- Using a COM object from Python
- How do I know which objects are available?
- Static Dispatch/Type Safe objects (using the new improved makepy.py)
- Using COM Constants with makepy.
Quick Start
To use a COM object from Python
import win32com.client
o =
win32com.client.Dispatch("Object.Name")
o.Method()
o.property = "New
Value"
print o.property
Example
o = win32com.client.Dispatch("Excel.Application")
o.Visible = 1
o.Workbooks.Add() #
for office 97 – 95 a bit different!
o.Cells(1,1).Value = "Hello"
And we will see the word "Hello" appear in the top cell.
How do I know which methods and properties are
available?
Good question. This is hard! You need to use the documentation with the
products, or possibly a COM browser. Note however that COM browsers typically
rely on these objects registering themselves in certain ways, and many objects
to not do this. You are just expected to know.
The Python COM browser
PythonCOM comes with a basic COM browser that may show you the information
you need. Note that this package requires Pythonwin (ie, the MFC GUI
environment) to be installed for this to work.
There are far better COM browsers available - I tend to use the one that
comes with MSVC, or this one!
To run the browser, simply select it from the Pythonwin Tools menu, or
double-click on the file win32com\client\combrowse.py
Static Dispatch (or Type Safe) objects
In the above examples, if we printed the 'repr(o)' object above,
it would have resulted in
<COMObject Excel.Application>
This reflects that the object is a generic COM object that Python has no
special knowledge of (other than the name you used to create it!). This is known
as a "dynamic dispatch" object, as all knowledge is built dynamically. The
win32com package also has the concept of static dispatch objects, which
gives Python up-front knowledge about the objects that it is working with
(including arguments, argument types, etc)
In a nutshell, Static Dispatch involves the generation of a .py file that
contains support for the specific object. For more overview information, please
see the documentation references above.
The generation and management of the .py files is somewhat automatic, and
involves one of 2 steps:
- Using makepy.py to select a COM library. This process is very similar
to Visual Basic, where you select from a list of all objects installed on your
system, and once selected the objects are magically useable.
or
- Use explicit code to check for, and possibly generate, support at run-time.
This is very powerful, as it allows the developer to avoid ensuring the user has
selected the appropriate type library. This option is extremely powerful for OCX
users, as it allows Python code to sub-class an OCX control, but the actual
sub-class can be generated at run-time. Use makepy.py with a -i
option to see how to include this support in your Python code.
The win32com.client.gencache module manages these generated files.
This module has some documentation of its
own, but you probably don't need to know the gory details!
How do I get at the generated module?
You will notice that the generated file name is long and cryptic - obviously
not designed for humans to work with! So how do you get at the module object for
the generated code?
Hopefully, the answer is you shouldn't need to. All generated file
support is generally available directly via win32com.client.Dispatch and
win32com.client.constants. But should you ever really need the Python
module object, the win32com.client.gencache module has functions specifically
for this. The functions GetModuleForCLSID and GetModuleForProgID both return
Python module objects that you can use in your code. See the docstrings in the
gencache code for more details.
To generate Python Sources supporting a COM object
Example using Microsoft Office 97.
Either:
- Run '
win32com\client\makepy.py' (eg, run it from the command
window, or double-click on it) and a list will be presented. Select the Type
Library 'Microsoft Word 8.0 Object Library' - From a command prompt, run the command '
makepy.py "Microsoft Word 8.0' (include the double quotes). This simply avoids the
Object Library"
selection process. - If you desire, you can also use explicit code to generate it just before you
need to use it at runtime. Run'makepy.py -i "Microsoft Word 8.0 Object' (include the double quotes) to see how to do this.
Library"
And that is it! Nothing more needed. No special import statements needed!
Now, you simply need say
>>> import win32com.client
>>> w=win32com.client.Dispatch("Word.Application")
>>> w.Visible=1
>>> w
<win32com.gen_py.Microsoft Word 8.0 Object
Library._Application>
Note that now Python knows the explicit type of the object.
Using COM Constants
Makepy automatically installs all generated constants from a type library in
an object called win32com.clients.constants. You do not need to do
anything special to make these constants work, other than create the object
itself (ie, in the example above, the constants relating to Word would
automatically be available after the
w=win32com.client.Dispatch("Word.Application")
statement.
For example, immediately after executing the code above, you could execute
the following:
>>> w.WindowState =
win32com.client.constants.wdWindowStateMinimize
and Word will Minimize.
[文摘]Quick Start to Client side COM and Python的更多相关文章
- Code a simple telnet client using sockets in python
测试端口是否开放的python脚本 原文: https://www.binarytides.com/code-telnet-client-sockets-python/ 配置: 120.79.14.8 ...
- Open Source VOIP applications, both clients and servers (开源sip server & sip client 和开发库)
SIP Proxies SBO SIP Proxy Bypass All types of Internet Firewall JAIN-SIP Proxy Mini-SIP-Proxy A very ...
- saltstack通过salt.client执行命令(转)
利用saltstack的salt.client模块可以在python的命令行下或者python脚本里执行相应的salt命令 master端想要执行类似 salt '*' cmd.run 'uptime ...
- 使用epoll实现一个udp server && client
udp server #!/usr/bin/env python #-*- coding:utf-8 -*- import socket import select import Queue #创建s ...
- Longhorn 云原生容器分布式存储 - Python Client
内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 云原生容器分布式存储 - 设计架构和概念 Longhorn 云原生容器分布式存储 - ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- Http Message Converters with the Spring Framework--转载
原文:http://www.baeldung.com/spring-httpmessageconverter-rest 1. Overview This article describes how t ...
- HTTP Components简介
基于版本4.5.x 简介 组件 HttpClient,核心组件 HC Fluent,提供了流式操作接口 HttpMime,提供文件上传时用到的一些工具类 HttpClient Cache,有待学习 H ...
- Python socket – network programming tutorial
原文:https://www.binarytides.com/python-socket-programming-tutorial/ --------------------------------- ...
随机推荐
- HTML5中meta属性大集合
1.声明文档的字符编码 <meta charset='utf-8'> 2.声明文档的兼容模式 <meta http-equiv="X-UA-Compatible" ...
- JavaScript数据结构与算法(四) 循环队列的实现
实现击鼓传花,需要用到上一章所述队列类Queue TypeScript方式实现源码 let hotPotato = (nameList, num) => { let queue = new Qu ...
- pat 1001 A+B Format
题目链接:传送门 题目简述: 1. 给定两个整数值a,b: 2.范围-1000000 <= a, b <= 1000000: 3.按指定格式输出结果 例:-100000 9 输出: -99 ...
- Docker(六):Docker 三剑客之 Docker Swarm
实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...
- [Codeforces 946G]Almost Increasing Array
Description 题库链接 给你一个长度为 \(n\) 的序列 \(A\) .现在准许你删除任意一个数,删除之后需要修改最小的次数使序列单调递增.问最小次数. \(1\leq n\leq 200 ...
- 【NOIP 2017】列队
Description Sylvia 是一个热爱学习的女♂孩子. 前段时间,Sylvia 参加了学校的军训.众所周知,军训的时候需要站方阵. Sylvia 所在的方阵中有n×m名学生,方阵的行数为 n ...
- 数据权限管理中心 - 基于mybatis拦截器实现
数据权限管理中心 由于公司大部分项目都是使用mybatis,也是使用mybatis的拦截器进行分页处理,所以技术上也直接选择从拦截器入手 需求场景 第一种场景:行级数据处理 原sql: select ...
- python2.7入门---运算符
已经分享过变量类型的基本概念了,接下来就研究了一下运算符的基础知识.接下来我们就来看一下内容.举个简单的例子 4 +5 = 9 .例子中,4 和 5 被称为操作数,"+" ...
- 配置文件错误导致jenkins无法启动 org.xmlpull.v1.XmlPullParserException: only 1.0 is supported as <?xml version not '1.1' (position: START_DOCUMENT seen <?xml version=\'1.1\'... @1:19)
org.xmlpull.v1.XmlPullParserException: only 1.0 is supported as <?xml version not '1.1' (position ...
- Spring--AOP 例子
先用代码讲一下什么是传统的AOP(面向切面编程)编程 需求:实现一个简单的计算器,在每一步的运算前添加日志.最传统的方式如下: Calculator.Java package cn.limbo.spr ...