用python编写com server程序 Com server using python preparation procedures

2007年04月19日• 追风逐月 • 分类: 技术开发 April 19, 2007 Herd monthly • • Category: Technology Development

因为同事工作上需要使用到htmltidy (一个开源的用于规正html代码的工具。用来对html代码进行格式化,效果非常好),但平时使用的是delphi,虽然其主页上提供了delphi的封装包,但是可能是版本比较旧的缘故,无法处理繁体中文和日文。 Work because my colleagues need to use to htmltidy (an open source code for the regulation is a tool for html. Used to html code format, the effect was very good), but in peacetime use the delphi, although its home page to provide a delphi The package, but may be the older version of the reason, not able to deal with Traditional Chinese and Japanese.
我看了一下,上面有phthon的封装包,于是试了一下,发现能很好的处理多国语言,于是就想起写一个com server让他调用. I read about the top of the package have phthon, then tried to found a better job handling multiple languages, therefore reminded to write on a com server let him call.
python实现com非常简单,具体实现可以参考《 Python Programming on Win32 》,这是一本介绍python window编程,非常经典的书籍。 Com using python achieve very simple, concrete realization of reference "Python Programming on Win32" This is a program introduced python window, very classic books.
这里我介绍一下实现步骤: Here, I explain to achieve steps:

  1. 下载并安装Python wrapper for TidyLib ,因为该库依赖于ctypes库,所以您可能还需要安装ctypes库 Download and install Python wrapper for TidyLib because it relies on the ctypes library, so you may also want to install the ctypes
  2. 下载并安装pywin32 Download and install pywin32
  3. 实现起来很简单,代码如下: PLAIN TEXT It's easy to achieve, the following code: PLAIN TEXT

    PYTHON: PYTHON:

    1. #–coding:utf8– #-Coding:-utf8

    2. # pytidycom.py # Pytidycom.py

    3. # #

    4. # A contrived sample Python server that demonstrates # A contrived sample Python server that demonstrates

    5. # wrapping, unwrapping, and passing IDispatch objects. # Wrapping and unwrapping, and passing IDispatch objects.

    6. # Import the utilities for wrapping and unwrapping. # Import the utilities for wrapping and unwrapping.

    7. from win32com. server . util import wrap, unwrap From win32com. Server. Util import wrap, unwrap

    8. import win32com. client Import win32com. Client

    9. import tidy Import tidy

    10. # Although we are able to register our Parent object for debugging, # Although we are able to register our Parent object for debugging,

    11. # our Child object is not registered, so this won’t work. To get # Our Child object is not registered, so this won't work. To get

    12. # the debugging behavior for our wrapped objects, we must do it ourself. # The debugging behavior for our wrapped objects, we must do it ourself.

    13. debugging = 1 Debugging = 1

    14. if debugging: If debugging:

    15. from win32com. server . dispatcher import DefaultDebugDispatcher From win32com. Server. Dispatcher import DefaultDebugDispatcher

    16. useDispatcher = DefaultDebugDispatcher UseDispatcher = DefaultDebugDispatcher

    17. else : Else:

    18. useDispatcher = None UseDispatcher = None

    19. # Our Parent object. # Our Parent object.

    20. # This is registered, and therefore creatable # This is registered, and therefore creatable

    21. # using CreateObject etc from VB. # Using CreateObject etc from VB.

    22. class Parse: Class Parse:

    23. _public_methods_ = [ ‘parseString’ ] _public_methods_ = [ 'ParseString']

    24. # Use “print pythoncom.CreateGuid()” to make a new one. # Use the "print pythoncom.CreateGuid ()" to make a new one.

    25. _reg_clsid_ = “{7AB98634-4E1E-40D0-8970-27CC9B1A1C40}” _reg_clsid_ = "(7AB98634-4E1E-40D0-8970-27CC9B1A1C40)"

    26. _reg_progid_ = “pytidycom.Parse” _reg_progid_ = "Pytidycom.Parse"

    27. def parseString ( self ,msg ) : Def parseString (self, msg):

    28. msg= unicode ( msg ) Msg = unicode (msg)

    29. Options= dict ( char_encoding= ‘utf8′ ,show_body_only= True ,output_html= True ,input_encoding= ‘utf8′ ,output_encoding= ‘utf8′ ) Options = dict (char_encoding = 'utf8' show_body_only = True, output_html = True, input_encoding = 'utf8' output_encoding = 'utf8')

    30. msg=tidy. parseString ( msg. encode ( ‘utf8′ ) ,**Options ) Msg = tidy. ParseString (msg. encode ( 'utf8'), ** Options)

    31. #msg=tidy.parseString(’test’) Msg = # tidy.parseString ( 'test')

    32. #return msg # Return msg

    33. return str ( msg ) . decode ( ‘utf8′ ) Return str (msg). Decode ( 'utf8')

    34. if __name__== ‘__main__’ : If __name__ == '__main__':

    35. import win32com. server . register Import win32com. Server. Register

    36. win32com. server . register . UseCommandLine ( Parse, debug=debugging ) Win32com. Server. Register. UseCommandLine (Parse, debug = debugging)

    以上的代码创建了一个简单输出parseString的com server The above code to create a simple output of the com server parseString
    您可以像这样用vbs调用它 You can use this as you called it vbs

    PLAIN TEXT PLAIN TEXT

    Visual Basic: Visual Basic:

    1. Set TidyObj = CreateObject ( “pytidycom.Parse” ) Set TidyObj = CreateObject ( "pytidycom.Parse")

    2. theString =TidyObj. parseString ( “<a>總統” ) TheString = TidyObj. ParseString ( "<a> president")

    3. msgbox theString Msgbox theString

  4. 将其封装成成dll或exe Be packaged as a dll or exe
    这里我使用py2exe进行打包生成pytidycom.dll和pytidycom.exe(您也可以设置成只生成其中之一),使用regsrv32 pytidycom.dll或pytidycom.exe /register注册,这样就可以像其他正常的windows程序一样调用,而不用带python环境了,虽然体积有点大,但效果还是不错的。 Here, I use a package py2exe generated pytidycom.dll and pytidycom.exe (You also can be configured to generate only one), or use regsrv32 pytidycom.dll pytidycom.exe / register registered, and this can be like other normal windows procedure call, and not with python environment, although volume somewhat, but the effect is quite good. 这里要注意的是打包后需要将cygtidy-0-99-0.dll这个动态库和打包后的程序放在一起,否则调用会失败 It should be noted here is the need to pack after cygtidy-0-99-0.dll this dynamic library and package put together after the procedure, otherwise call will fail
  5. 这里是setup.py的代码 Here is the code setup.py

    PLAIN TEXT PLAIN TEXT

    PYTHON: PYTHON:

    1. #–coding:utf-8– #-Coding: utf-8 -

    2. # This is the distutils script for creating a Python-based com (exe or dll) # This is the distutils script for creating a Python-based com (exe or dll)

    3. # server using win32com. This script should be run like this: # Server using win32com. This script should be run like this:

    4. # #

    5. # % python setup.py py2exe #% Python setup.py py2exe

    6. # #

    7. # After you run this (from this directory) you will find two directories here: # After you run this (from this directory) you will find two directories here:

    8. # “build” and “dist”. The .dll or .exe in dist is what you are looking for. # "Build" and "dist." The. Dll or. Exe in dist is what you are looking for.

    9. ############################################################################## ################################################## ############################

    10. from distutils . core import setup From distutils. Core import setup

    11. import py2exe Import py2exe

    12. import sys Import sys

    13. if len ( sys . argv ) == 1 : If len (sys. Argv) == 1:

    14. sys . argv . append ( “py2exe” ) Sys. Argv. Append ( "py2exe")

    15. class Target: Class Target:

    16. def __init__ ( self , **kw ) : Def __init__ (self, ** kw):

    17. self . __dict__ . update ( kw ) Self. __dict__. Update (kw)

    18. # for the version info resources (Properties — Version) # For the version info resources (Properties - Version)

    19. self . version = “0.0.1″ Self. Version = "0.0.1"

    20. self . company_name = u “XXXXX” Self. Company_name = u "XXXXX"

    21. self . copyright = u “© 2006, yxl” Self. Copyright = u "© 2006, yxl"

    22. self . name = “htmltidy(python) activex” Self. Name = "htmltidy (python) activex"

    23. my_com_server_target = Target ( Target my_com_server_target = (

    24. description = “htmltidy(python) activex” , Description = "htmltidy (python) activex"

    25. # use module name for win32com exe/dll server # Use module name for win32com exe / dll server

    26. modules = [ “pytidycom” ] , Modules = [ "pytidycom"]

    27. # specify which type of com server you want (exe and/or dll) # Specify which type of com server you want (exe and / or dll)

    28. create_exe = True , Create_exe = True,

    29. create_dll = True Create_dll = True

    30. ) )

    31. excludes = [ ] #[”pywin”, “pywin.debugger”, “pywin.debugger.dbgcon”, Excludes = [] # [ "pywin", "pywin.debugger", "pywin.debugger.dbgcon"

    32. # “pywin.dialogs”, “pywin.dialogs.list”, “win32com.client”] # "Pywin.dialogs", "pywin.dialogs.list", "win32com.client"]

    33. setup ( Setup (

    34. name= “pytidycom” , Name = "pytidycom"

    35. # the following two parameters embed support files within exe/dll file # The following two parameters embed support files within exe / dll file

    36. options= { “py2exe” : { “bundle_files” : 1 , Options = ( "py2exe": ( "bundle_files": 1.

    37. “dll_excludes” : [ “w9xpopen.exe” ] , ‘excludes’ :excludes, ‘packages’ : [ ‘encodings’ , ‘tidy’ , ‘ctypes’ ] "Dll_excludes": [ "w9xpopen.exe"], 'excludes': excludes,' packages': [ 'encodings',' tidy ',' ctypes']

    38. } } , ))

    39. zipfile = None , Zipfile = None,

    40. version= “0.0.1″ , Version = "0.0.1"

    41. description= “htmltidy(python)” , Description = "htmltidy (python)."

    42. # author, maintainer, contact go here: # Author, maintainer, contact go here:

    43. author= “chyni” , Author = "chyni"

    44. author_email= “coolchyni@gmail.com” , Author_email = "coolchyni@gmail.com"

    45. com_server= [ my_com_server_target ] Com_server = [my_com_server_target]

    46. ) )

标签: com , python Tags: com, python
本文链接: http://chyni.com/develop/python-com.html This paper links: http://chyni.com/develop/python-com.html

你可能还喜欢 You may also like



发表你的意见 Express your views

追风逐月 Zhuifeng monthly

提供网络赚钱、网赚经验、网赚工具、SEO、国外主机信息 The provision of the network to make money, Wangzhuan experience Wangzhuan tool, SEO, foreign host information
Herd monthly subscription blog feed
Subscribe to google Subscribe to Battery
Subscribe to the fresh fruit Subscribe to grasp shrimp
* 更多订阅本站方式请看订阅帮助 * See More Subscribe way to help subscribers