本文介绍了如何使用Python win32com调用Excel VBA函数和subs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Excel工作簿包含与以下类似的VBA子程序和宏:他们坐在Module1中。

My Excel workbook contains VBA subs and macros similar to those below; they sit in Module1.

如何使用Python win32com模块调用它们?

How to call them using Python win32com module?

Public Sub setA1(ByVal s As String)
    ThisWorkbook.ActiveSheet.Range("A1").Value = s
End Sub

Public Function getA1() As String
    getA1 = ThisWorkbook.ActiveSheet.Range("A1").Value
End Function

感谢提前!

推荐答案

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename="c:\\temp\\book1.xls",ReadOnly=1)
xl.Application.Run("setA1", '4')
res = xl.Application.Run("getA1")
print res
xl = 0

就像这样简单....

Just as simple as this ....

这篇关于如何使用Python win32com调用Excel VBA函数和subs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 00:06
查看更多