需要骨架代码从 PythonWin 调用 Excel VBA [英] Need skeleton code to call Excel VBA from PythonWin
问题描述
我需要从 python 脚本调用 Excel 工作簿中的 VBA 宏.其他人为 Excel 工作簿提供了宏.该宏从外部数据库中获取更新的值,并对数据执行一些相当复杂的处理.我需要这个按摩的结果,如果可以避免的话,我真的不想在我的 Python 脚本中复制它.所以,如果我可以从我的脚本中调用宏,并获取经过处理的结果,那就太好了.
我从Win32 上的 Python 编程"中学到的关于 COM 的一切.好书,但不足以完成我手头的任务.我搜索了,但没有找到任何关于如何做到这一点的好例子.有没有人有任何好的例子,或者可能有一些关于如何寻址/调用 VBA 宏的骨架代码?Excel COM 接口上的一般参考资料(书籍、网络链接等)在这里也会有所帮助.谢谢.
对不起,我对python不够了解.但是,以下内容应该会有所帮助.
Excel 的 Application 对象有一个 Run 方法 - 它接受宏的名称及其参数.
假设工作簿有一个名为 test 的宏.
<上一页><代码>子测试(ByVal i As Integer)MsgBox "你好世界" & i结束子代码>您可以使用 Application.Run "test", 1234 调用它
这将调用宏并显示带有hello world 1234"的消息框.
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.
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.
I am sorry, I dont know python enough. However, the following should help.
Excel's Application object has a Run method - which takes the name of the macro alongwith arguments to it.
Lets assume that the workbook has a macro named test.
Sub test(ByVal i As Integer)
MsgBox "hello world " & i
End Sub
You can call this using Application.Run "test", 1234
This will call the macro and show the messagebox with "hello world 1234".
这篇关于需要骨架代码从 PythonWin 调用 Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!