简体   繁体   English

如何使用 xlwings 从 Python 调用 Excel 宏?

[英]How do I call an Excel macro from Python using xlwings?

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 的API 文档,并在解释器中使用了 Workbook 和 Sheet 对象,但我不知道如何从 Python 调用宏。

How do I use xlwings to call an Excel macro from Python?如何使用 xlwings 从 Python 调用 Excel 宏?

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 ):同时,您可以像这样解决它(这是针对 Windows 的,但 Mac 版本相应地工作,请在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")

update 2 : This functionality is now natively supported from >=v0.7.1.更新 2 :现在从 >=v0.7.1 开始支持此功能。 Let's assume, there is a VBA function YourMacro that sums up two numbers:让我们假设,有一个 VBA 函数YourMacro可以总结两个数字:

>>> import xlwings as xw
>>> wb = xw.Book(r'C:\path\to\mybook.xlsm')
>>> your_macro = wb.macro('YourMacro')
>>> your_macro(1, 2)
3.0

I got issues when I updated xlwings to 0.9+ version.将 xlwings 更新到 0.9+ 版本时遇到问题。 To run vba macro with xlwings, I used the code written below for running macros inside the personal workbook (PERSONAL.XLSB).要使用 xlwings 运行 vba 宏,我使用下面编写的代码在个人工作簿 (PERSONAL.XLSB) 中运行宏。 The updated code no2 of Felix didn't work for me, for macro inside the personal workbook. Felix 的更新代码 no2 对我不起作用,对于个人工作簿中的宏。

import xlwings as xw

wb = xw.Book(excel_file_path)
app = wb.app
# into brackets, the path of the macro
macro_vba = app.macro("'PERSONAL.XLSB'!my_macro") 
macro_vba()

Hope it will help.希望它会有所帮助。

I know it is a late answer, but all the ways posted above didn't work for me.我知道这是一个迟到的答案,但上面发布的所有方法对我都不起作用。 But I have found another way, by using the awesome api-interface provided by xlwings.但是我找到了另一种方法,通过使用 xlwings 提供的很棒的 api 接口。

Here is my code I used to run a macro:这是我用来运行宏的代码:

xlApp = xw.App(visible=False)
wb= xw.books.open('.\\Path\\To\\File.xlsm')
a = xlApp.api.Application.Run("macroTest")

My macro opened a MsgBox and returned the value 1 just for test and it worked very well.我的宏打开了一个 MsgBox 并返回值 1 只是为了测试,它工作得很好。 Although one should avoid using MsgBox, since it was opened in background.尽管应该避免使用 MsgBox,因为它是在后台打开的。

Btw.顺便提一句。 the api-interface is available in many (when not all) objects and it is really powerfull if you are used to VBA programming. api 接口在许多(不是全部)对象中都可用,如果您习惯 VBA 编程,它真的很强大。

如果您已经打开了一个excel,另一种简单的方法。

xw.apps.active.macro('yourMacro')(1, 2)

Extending @Felix Zumstein's answer and all other answers here, please ensure you have disabled Macro settings as you may face COM issues later.在这里扩展@Felix Zumstein 的答案和所有其他答案,请确保您已禁用宏设置,因为您稍后可能会遇到 COM 问题。

You can change that under,你可以改变下,

File > Options > Trust Center > Trust Center Settings > Macro Settings > Enable all macros (not recommended...)文件 > 选项 > 信任中心 > 信任中心设置 > 宏设置 > 启用所有宏(不推荐...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM