如何从python中调用VBA Sub

时间:2013-04-09 08:49:08

标签: python excel pywin32

在ThisWorkbook中声明了Public Sub的Excel文件:

Public Sub Build(data)
    Me.Sheets(1).Range("A1") = "Hello, world! " & data
End Sub

有一个python脚本需要打开Excel文件并运行在sub:

之上
import win32com.client, os

xla = win32com.client.DispatchEx('Excel.Application')
xlb = xla.Workbooks.Add(os.getcwd()+r'\test.xls')

VbMethod = 1

try:
 # Error: object has no attribute 'Build'
 xlb.Build('data')

 # CallByName should help, but how do I call it from python?
 # CallByName(xlb,'Build',VbMethod,xla,'data')
finally:
 xlb.Close()
 xla.Quit()

请告知我如何从python中调用Sub?提前谢谢!

0 个答案:

没有答案