调用带参数的宏:Python win32com API [英] Call a macro with parameters : Python win32com API

查看:49
本文介绍了调用带参数的宏:Python win32com API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是从我的 python 代码中调用一个宏.以下是来源示例:

What I want to do is call a macro from my python code. Here isa sample of the sources :

xl = win32.gencache.EnsureDispatch('Excel.Application')
xl.Visible = 1
xl.Workbooks.Open("C:\Program Files\Microsoft Office\Office14\XLSTART\perso.xlsm")
xl.Workbooks.Open(argv[1])
xl.Application.Run('perso.xlsm!' + argv[2])
xl.Application.Run('perso.xlsm!' + argv[2] + '2')
xl.Workbooks.Open(argv[0])
xl.Application.Run('perso.xlsm!aggregate_report_ouverture_appli')
xl.Application.Run('perso.xlsm!macro', 'lol')
xl.Save()
xl.Quit() 

前两个宏工作正常.但是最后一个需要设置一个参数(在这种情况下是lol").通过这个尝试:

The first two macro are working fine. But the last need a parameter to be set ("lol" in this case). With this try :

xl.Application.Run('perso.xlsm!macro', 'lol')

我的宏已调用但参数未设置.知道如何执行此操作或在哪里可以找到此模块的javadoc"(是的,我来自 Java 世界!).

My macro is call but the parameter is not set. Any idea how to do this or where I can find the "javadoc" of this module (yeah i am from Java world !).

如果您需要更多解释,请告诉我.

If you need more explanations just le me know.

谢谢.

达米安.

推荐答案

不幸的是,在这种情况下,关于 excel 的 win32com 的完整文档不存在;您将不得不查看 MS Office Excel MSDN 中的大多数内容(这还不错,您有时只需要修改代码即可):

Unfortunately in this case, the complete documentation for win32com with regards to excel does not exist; you will have to look at the MS office Excel MSDN for most everything (which isn't too bad you just have to finagle the code sometimes):

http://msdn.microsoft.com/en-us/library/office/bb149081(v=office.12).aspx

至于您的问题,请考虑带有两个参数的示例宏:

As for your question, consider the example macro which takes two parameters:

Sub Proc(sParam1 As String, iParam2 As Integer)
        MsgBox sParam1 & " is " & iParam2 & " Years Old"
End Sub

该宏有两个要查找的参数.以下 python 代码将使用 params 集调用宏:

The macro has two parameters that it's looking for. The following python code will call the macro using the params set:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Visible = True
Path = "C:\Program Files\Microsoft Office\Office14\XLSTART\perso.xlsm"
xl.Workbooks.Open(Filename=Path)
param1 = "Jeremy"
param2 = 3
xl.Application.Run("Proc", param1, param2)

我不确定您的情况下的宏期望什么变量类型,但是 'lol' 是,但是它在您的示例中作为字符串从 python 发送到宏.希望这会有所帮助.

I'm not sure what variable type the macro in your case is expecting, but 'lol' is but it is being sent to the macro from python as a string in your example. Hope this helps.

这篇关于调用带参数的宏:Python win32com API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆