用python编写com server程序
因为同事工作上需要使用到htmltidy(一个开源的用于规正html代码的工具。用来对html代码进行格式化,效果非常好),但平时使用的是delphi,虽然其主页上提供了delphi的封装包,但是可能是版本比较旧的缘故,无法处理繁体中文和日文。
我看了一下,上面有phthon的封装包,于是试了一下,发现能很好的处理多国语言,于是就想起写一个com server让他调用.
用python实现com非常简单,具体实现可以参考《Python Programming on Win32》,这是一本介绍python window编程,非常经典的书籍。
这里我介绍一下实现步骤:
- 下载并安装Python wrapper for TidyLib,因为该库依赖于ctypes库,所以您可能还需要安装ctypes库
- 下载并安装pywin32
- 实现起来很简单,代码如下:PLAIN TEXT
PYTHON:
-
#–coding:utf8–
-
# pytidycom.py
-
#
-
# A contrived sample Python server that demonstrates
-
# wrapping, unwrapping, and passing IDispatch objects.
-
-
# Import the utilities for wrapping and unwrapping.
-
from win32com.server.util import wrap, unwrap
-
-
import win32com.client
-
import tidy
-
-
# Although we are able to register our Parent object for debugging,
-
# our Child object is not registered, so this won’t work. To get
-
# the debugging behavior for our wrapped objects, we must do it ourself.
-
debugging = 1
-
if debugging:
-
from win32com.server.dispatcher import DefaultDebugDispatcher
-
useDispatcher = DefaultDebugDispatcher
-
else:
-
useDispatcher = None
-
-
# Our Parent object.
-
# This is registered, and therefore creatable
-
# using CreateObject etc from VB.
-
class Parse:
-
_public_methods_ = [‘parseString’]
-
# Use “print pythoncom.CreateGuid()” to make a new one.
-
_reg_clsid_ = “{7AB98634-4E1E-40D0-8970-27CC9B1A1C40}”
-
_reg_progid_ = “pytidycom.Parse”
-
-
def parseString(self,msg):
-
msg=unicode(msg)
-
Options=dict(char_encoding=‘utf8′,show_body_only=True,output_html=True,input_encoding=‘utf8′,output_encoding=‘utf8′)
-
msg=tidy.parseString(msg.encode(‘utf8′),**Options)
-
#msg=tidy.parseString(’test’)
-
#return msg
-
return str(msg).decode(‘utf8′)
-
-
if __name__==‘__main__’:
-
import win32com.server.register
-
win32com.server.register.UseCommandLine(Parse, debug=debugging)
以上的代码创建了一个简单输出parseString的com server
您可以像这样用vbs调用它Visual Basic:
-
Set TidyObj = CreateObject(“pytidycom.Parse”)
-
theString =TidyObj.parseString(“<a>總統”)
-
msgbox theString
-
- 将其封装成成dll或exe
这里我使用py2exe进行打包生成pytidycom.dll和pytidycom.exe(您也可以设置成只生成其中之一),使用regsrv32 pytidycom.dll或pytidycom.exe /register注册,这样就可以像其他正常的windows程序一样调用,而不用带python环境了,虽然体积有点大,但效果还是不错的。这里要注意的是打包后需要将cygtidy-0-99-0.dll这个动态库和打包后的程序放在一起,否则调用会失败 -
#–coding:utf-8–
-
# This is the distutils script for creating a Python-based com (exe or dll)
-
# server using win32com. This script should be run like this:
-
#
-
# % python setup.py py2exe
-
#
-
# After you run this (from this directory) you will find two directories here:
-
# “build” and “dist”. The .dll or .exe in dist is what you are looking for.
-
##############################################################################
-
-
from distutils.core import setup
-
import py2exe
-
import sys
-
if len(sys.argv) == 1:
-
sys.argv.append(“py2exe”)
-
-
class Target:
-
def __init__(self, **kw):
-
self.__dict__.update(kw)
-
# for the version info resources (Properties — Version)
-
self.version = “0.0.1″
-
self.company_name = u“XXXXX”
-
self.copyright = u“© 2006, yxl”
-
self.name = “htmltidy(python) activex”
-
-
my_com_server_target = Target(
-
description = “htmltidy(python) activex”,
-
# use module name for win32com exe/dll server
-
modules = [“pytidycom”],
-
# specify which type of com server you want (exe and/or dll)
-
create_exe = True,
-
create_dll = True
-
)
-
excludes = []#[”pywin”, “pywin.debugger”, “pywin.debugger.dbgcon”,
-
# “pywin.dialogs”, “pywin.dialogs.list”, “win32com.client”]
-
setup(
-
name=“pytidycom”,
-
# the following two parameters embed support files within exe/dll file
-
options={“py2exe”: {“bundle_files”: 1,
-
“dll_excludes”: [“w9xpopen.exe”],‘excludes’:excludes,‘packages’:[‘encodings’,‘tidy’,‘ctypes’]
-
}},
-
zipfile=None,
-
version=“0.0.1″,
-
description=“htmltidy(python)”,
-
# author, maintainer, contact go here:
-
author=“chyni”,
-
author_email=“coolchyni@gmail.com”,
-
-
com_server=[my_com_server_target]
-
)
这里是setup.py的代码
PYTHON:









