如何使用xlwings从Python调用Excel宏? [英] How do I call an Excel macro from Python using xlwings?
问题描述
我已经阅读了xlwings的 API文档,并使用工作簿和表格对象在解释器中,但我无法弄清楚如何从Python调用宏。
I've read the API docs for xlwings, and played around with Workbook and Sheet objects in the interpreter, but I can't figure out how to call a macro from Python.
如何使用xlwings从Python调用Excel宏?
How do I use xlwings to call an Excel macro from Python?
推荐答案
这还没有实现,但是有一个开放的问题,请参阅 here 。在此期间,您可以像这样操作(这是Windows,但Mac版本相应地工作,请再次查看问题):
This is not implemented yet, but there's an open issue for it, see here. In the meantime, you can work around it like so (this is for Windows, but the Mac version works accordingly, see again in the issue):
from xlwings import Workbook
wb = Workbook(...)
wb.application.xl_app.Run("your_macro")
更新:对于更新版本,您必须执行以下操作:
update: for more recent versions, you have to do:
from xlwings import Workbook, Application
wb = Workbook(...)
Application(wb).xl_app.Run("your_macro")
更新2 :v0.7.1现在支持此功能。我们假设有一个VBA函数 YourMacro
总结了两个数字:
update 2: This functionality is now natively supported with v0.7.1. Let's assume, there is a VBA function YourMacro
that sums up two numbers:
>>> import xlwings as xw
>>> wb = xw.Workbook.active()
>>> your_macro = wb.macro('YourMacro')
>>> your_macro(1, 2)
3.0
这篇关于如何使用xlwings从Python调用Excel宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!