专注于自动化测试,性能测试.......

Python生成COM组件(原创)

上一篇 / 下一篇  2009-09-13 22:46:57 / 个人分类:Python

   经过一段对Python的使用,发现它确实是一门比较优秀的语言,语法简练,类库丰富且调用简单,在数据库,文本处理,网络编程方面都很棒。所以就一直想在实际工作中,确切地来说是现在正在进行的自动化测试使用Python,但是由于测试工具不支持python语言,所以没法直接在工具中使用。就考虑怎么在C#或者VBS中引用Python,一直没有太好的思路。偶然间阅读了《Python programming on win32,里面提到了把Python的类注册成为Com组件供VBDelphi实用的例子,顿时豁然开朗。

3[zU;FEF6Uv4Ru t0

 下面就该书中Implementing COM Objects with Python章节进行分析,首先给出原文:

lsIC5F ~E.A.J-i0

Implementing COM Objects with Python

_$f DA&A|5t0

In this section, we discuss how to implement COM objects using Python and a small sample of such an object. We also present some Visual Basic code that uses our Python implemented object.

+t6_|GkL ] m0

For this demonstration, you'll write a simple COM object that supports a number of string operations. As Visual Basic is somewhat lacking in the string-processing department where Python excels, it's a good candidate. The sample provides a COM object with a single method, SplitString(). This method has semantics identical to the standard Python function string.split(); the first argument is a string to split, and the second optional argument is a string holding the character to use to make the split. As you have no doubt guessed, the method won't do much more than call the Python string.split() function.

a;atXrz_0

There are two steps to implement COM objects in Python:

OX"DR~#n3T0
  • Define a Python class with the methods and properties you wish to expose.
  • Annotate the Python class with special attributes required by the PythonCOM framework to expose the Python class as a COM object. These annotations include information such as the objects ProgID, CLSID, and so forth.

Python中实现COM对象有两个步骤:51Testing软件测试网 D6MO*T+p.s A!?1C

1.           定义你希望暴露的类的方法和属性

C!EW.iY0

2.           注解PythonCOM框架为了暴露python类所需的专用属性,其中包括对象ProgID,CLSID等等

i?p J6|L9RT0

The following code shows a smallCOM server written in Python:51Testing软件测试网+q*n_YJ4E5fG

# SimpleCOMServer.py - A sample COM server - almost as small as they come!51Testing软件测试网xqE6? z

#

q kSNx0UD/q0

# We expose a single method in a Python COM object.51Testing软件测试网 G4T;G7E+zkE

class PythonUtilities:51Testing软件测试网2O;eRbSi

   _public_methods_ = [ 'SplitString' ]51Testing软件测试网CU+d Z X:MK A

   _reg_progid_ = "PythonDemos.Utilities"51Testing软件测试网`%T ir_

   # NEVER copy the following ID51Testing软件测试网9A"bg7|^#N$n#O

   # Use "print pythoncom.CreateGuid()" to make a new one.51Testing软件测试网p(TDHM#OeI'?

   _reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"51Testing软件测试网K;E [V`p#Y R

   51Testing软件测试网9iZjPdZS4b

   def SplitString(self, val, item=None):51Testing软件测试网;B Y,]p ?Np Q&k

       import string51Testing软件测试网6j,y0B o| g8G

       if item != None: item = str(item)51Testing软件测试网%mb;|C:aI'DFF

       return string.split(str(val), item)51Testing软件测试网\&ypG$]#@2|c:{

 

3or?t*Wtv,S+C$E0

# Add code so that when this script. is run by

&p!mmH%C%lv0

# Python.exe, it self-registers.51Testing软件测试网 c+M%Tv,^:[7iE7w

if __name__=='__main__':

,bpD} e3N`$U([+h0

   print "Registering COM server..."

%H0Y'OT0B+{ @G`0

   import win32com.server.register51Testing软件测试网]/b#Vj*uA0wc

   win32com.server.register.UseCommandLine(PythonUtilities)

WOw.`zC0

The bulk of the class definition is taken up by the specialattributes:51Testing软件测试网%yJ M0G3k%O!H TQ

_public_methods_51Testing软件测试网 f@0~3\mf

A list of all methods in the object that are to be exposed via COM; the sample exposes only one method, SplitString.51Testing软件测试网X6d f |n

一列包含所有需要暴露的类方法的列表

m X|5U ^F0

_reg_progid_

h&c&X f$K0

TheProgID for the new object, that is, the name that the users of this object must use to create the object.51Testing软件测试网3ty4DE'I y:I{

新对象的ProgID,该名称是所有调用Com,创建对象时必须用到的。51Testing软件测试网)Z-h0Z1CB-b3Q![

_reg_clsid_51Testing软件测试网 |c1`D S&q

The uniqueCLSID for the object. As noted in the source code, you must never copy these IDs, but create new ones using pythoncom.CreateGuid().51Testing软件测试网\!Ryd!`-S^UU XO:c

对象的CLSID(独一无二的),你可以通过Pythoncom.CreateGuid()创建。51Testing软件测试网9VD c,\2KAK `

Full details of these and other possible attributes can be found inChapter 12.

l^FF+@S0

The SplitString() method is quite simple: it mirrors the behavior. of the Python string.split() function. A complication is that COM passes all strings asUnicode characters, so you must convert them to Python strings using the str() function. Note that in Python 1.6, it's expected that the string and Unicode types will be unified allowing the explicit conversions to be removed.51Testing软件测试网9jT}d }

The only thing remaining is to register theobject with COM. As the comments in the code imply, you can do this by executing the code as a normal Python script. The easiest way to do this is to open the source file in PythonWin and use the Run command from the File menu. After running the script, the PythonWin interactive window should display:51Testing软件测试网/|k7o-OW}7s

Registering COM server...

%P7Gc$|0wy0

Registered: PythonDemos.Utilities51Testing软件测试网+W V_4J4jkb

Finally, let's test theCOM object. UseVisual Basic for Applications, which ships with both Microsoft Office and Microsoft Excel, and perform. the following steps:51Testing软件测试网%m8g,kn+?$V

  1. Start Microsoft Word or Microsoft Excel.
  2. Press ALT-F8 to display the macros dialog.
  3. Enter a name for the macro (e.g., TestPython) and select Create.
  4. The Visual Basic editor is displayed. In the editor, enter the following code:

5.           Set PythonUtils = CreateObject("PythonDemos.Utilities")

3w _L8JKW/T W0

6.           response = PythonUtils.SplitString("Hello from VB")51Testing软件测试网:\\I&E t wCv

7.           for each Item in response51Testing软件测试网2RF+C-P;S I;?ej

8.             MsgBox Item

+x F&t:v[0

nex

3XGL1HD.{V0

Now run this code by pressing the F5 key. If all goes well, you should see three message boxes. The first one is shown inFigure 5.2.

h1Dfl_H(~U0

Just to be complete and help keep your registry clean, unregister your sample COM server. You do this by following the same process that registered the server, except specify --unregister as an argument to your script. A message is printed saying the object is unregistered.

Ed }'p%mWt0

自己练习实现的例子:

M2EJ%u/i B5B0

import win32com.server.register

;i@vx-to+u%Z0

import pythoncom

'v1Oe,j{!v0

class MyFirstCom(object):

j}*i}`Vn0

 

A@#G-rSW0

   _public_methods_ = ['Instring']51Testing软件测试网J FU6cg{ s

 

6fSPfAr0Z0

   _reg_progid_ ="Pythoncom.FirstCom"

%@L(tz/Hp0

 

Z `2n2HU@G P4u0

   _reg_clsid_ = pythoncom.CreateGuid()

-\|P-a9zcJ0

 51Testing软件测试网"FE-Z`6a/R*{

   def Instring(self,val,vals):51Testing软件测试网@D |"C lG `

       if val in vals:

!A2h*L;glM^a-T0

           return True

` Wg{ QXQ9e h*K0

       else:

!gJIpb A3lc0

           return False

;@~,@ Cv8I M0

 

2K Z nT.o5d%L'W0

if __name__ == "__main__":51Testing软件测试网%V&dQd;[

   win32com.server.register.UseCommandLine(MyFirstCom)

TAG: Python python

 

评分:0

我来说两句

wxf_xsfy

wxf_xsfy

自动化测试的拥簇者,善于自动化测试的框架和工具开发,TIB工作室核心成员

日历

« 2024-04-14  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 381249
  • 日志数: 79
  • 图片数: 1
  • 文件数: 1
  • 书签数: 3
  • 建立时间: 2007-09-19
  • 更新时间: 2018-01-30

RSS订阅

Open Toolbar