如何在python中使用ActiveX控件
[html] view plaincopy
<span style="font-family: Arial, Verdana, sans-serif; white-space: normal; background-color: rgb(255, 255, 255); ">首先,这个东蠢者正东貌似只有windows上才能实现,所以,需要部署windows下的相关环境</span>
1、需要安装python
2、安装python的win32com的lib,下载地址:
http://sourceforge.net/projects/pywin32/files/
3、安装本地的web容器,我用的apache的tomcat
4、编写例子:
[html] view plaincopy
import uuid
import win32com.server.register
class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
#_reg_clsid_ = "{A6688635-62F5-41cb-AF54-CBA84C2F0F86}"
_reg_clsid = "{"+uuid.uuid1()+"}"
def SplitString(self, val):
return "Hello world ", val
嫌亩
if __name__ == '__main__':
print "Registering COM server..."
win32com.server.register.UseCommandLine(PythonUtilities)
将这个文件保存到tomcat的webapps下的一个目录中
5、编写测试的html页面
[html] view plaincopy
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<SCRIPT LANGUAGE="JavaScript">
window.onload = function(){
var obj = new ActiveXObject("Python.Utilities");
alert(obj.SplitString("Hello world"));
}
</SCRIPT>
</head>
<body>
</body>
</html>
放到python文件的同一个目录
6、到命令行执行该python 代码
7、再请求该带悔网页
推荐于2016-09-01 · 知道合伙人软件行家
复制代码代码如下:
# SimpleCOMServer.py
class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
_reg_clsid_ = "{A6688635-62F5-41cb-AF54-CBA84C2F0F86}"
def SplitString(self, val):
return "Hello world ", val
if __name__ == '__main__':
print "Registering COM server..."
import win32com.server.register
win32com.server.register.UseCommandLine(PythonUtilities)
在console下运行:python SimpleCOMServer.py
在HTML页面中则消宏调用该Activex组件:
复制代码代桥厅码如下孙册:
window.onload = function(){
var obj = new ActiveXObject("Python.Utilities");
alert(obj.SplitString("Hel"));
}