怎样将VB与python结合在一起

Python011

怎样将VB与python结合在一起,第1张

#需要先安装pipywin32模块

class PythonUtilities:

_public_methods_=['SplitString']

_reg_progid_='PythonDemos.Utilities'

# 使用"print (pythoncom.CreateGuid())" 得到一个自己的clsid,不要用下面这个!!

_reg_clsid_='{5FCAC95E-653A-484C-8568-A02D5E0256E8}'

def SplitString(self, val, item=None):

import string

if item !=None: item=str(item)

val=str(val)

return val.split(item)

if __name__=='__main__':

print ('Registering COM server...')

import win32com.server.register

win32com.server.register.UseCommandLine(PythonUtilities)

下面是VB的相关代码:

Private Sub Form_Load()

Set PythonUtils = CreateObject("PythonDemos.Utilities")

response = PythonUtils.SplitString("Hello from VB")

For Each Item In response

MsgBox Item

Next

End Sub

完成后在cmd中使用(py_name是上面python文件的名称)

>python py_name.py --unregister

使用命令行参数

文件test.py

import sys

  print "脚本名:", sys.argv[0]

VB中设置为从Sub Main启动(而不是Form1)

Sub main()

    MsgBox Command$

End Sub

编译为vb.exe

python给vb传参数

os.system("vb.exe 123")

vb给python传参数

Shell "python.exe test.py -q"

你的报错截图发出来看看,或者检查你的PATH是不是正确设置,难道你没有本机管理员权限吗?看看《Linux就该这么学》 里面有个专栏是 Linux命令大全(手册,

当我们在windows下执行python脚本时,经常会遇到权限不足,需要管理员权限才能执行的情况。

现在我们采用比较笨的方法来达到我们能执行的目的:

1.创建一个空的bat文件,动态写入cmd命令;

2.创建一个固定内容的vb脚本;

3.python调用vb脚本去执行bat文件。

vb脚本内容:

cwd = CreateObject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path

path = cwd &"\cmd.bat"

Set shell = CreateObject("Shell.Application")

shell.ShellExecute path,"","","runas",1

WScript.Quit