需要骨架代码从PythonWin调用Excel VBA [英] Need skeleton code to call Excel VBA from PythonWin
问题描述
我需要从一个python脚本调用一个Excel工作簿中的VBA宏。还有人为Excel工作簿提供了宏。宏从外部数据库获取更新的值,并执行一些相当复杂的数据按摩。我需要这个按摩的结果,我真的不想复制这个在我的Python脚本,如果我可以避免它。所以,如果我可以从我的脚本调用宏,并抓取按摩的结果,这将是巨大的。
I need to invoke a VBA macro within an Excel workbook from a python script. Someone else has provided the Excel workbook with the macro. The macro grabs updated values from an external database, and performs some fairly complex massaging of the data. I need the results from this massaging, and I don't really want to duplicate this in my Python script, if I can avoid it. So, it would be great if I could just invoke the macro from my script, and grab the massaged results.
我知道的COM我知道从Python编程Win32。好书,但不足以满足我手头的任务。我搜索,但没有找到任何好的例子如何做到这一点。有没有人有任何好的例子,或者一些如何解决/调用VBA宏的框架代码? Excel COM接口上的一般参考(书,Web链接等)也将有所帮助。谢谢。
Everything I know about COM I learned from "Python Programming on Win32". Good book, but not enough for my task at hand. I searched, but haven't found any good examples on how to do this. Does anyone have any good examples, or perhaps some skeleton code of how to address/invoke the VBA macro? A general reference (book, web link, etc) on the Excel COM interfaces would also help here. Thanks.
推荐答案
对不起,我不知道python。
然而,以下应该有帮助。
I am sorry, I dont know python enough. However, the following should help.
Excel的Application对象有一个Run方法 - 它接受宏的名称及其参数。
Excel's Application object has a Run method - which takes the name of the macro alongwith arguments to it.
假设工作簿有一个名为test的宏。
Lets assume that the workbook has a macro named test.
Sub test(ByVal i As Integer)
MsgBox "hello world " & i
End Sub
您可以使用Application.Runtest,1234
You can call this using Application.Run "test", 1234
调用此函数。这将调用宏并显示messagebox hello world 1234。
This will call the macro and show the messagebox with "hello world 1234".
这篇关于需要骨架代码从PythonWin调用Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!