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

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

问题描述

我想要做的就是调用从我的蟒蛇code宏。这里的来源ISA示例:

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() 

前两个宏工作正常。但在过去需要(在本例为笑),也可以设置的参数。有了这个尝试:

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.

谢谢你。

达。

推荐答案

不幸的是这种情况下,win32com与问候Excel中的完整文档不存在;你将不得不看MS Office Excel中MSDN对于大多数的一切(这是不是太糟糕,你只需要有时欺骗了code):

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 /图书馆/办公室/ 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 code将通过把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)

我不知道你的情况宏期待什么变量类型,但'笑'是,但它被发送到宏观,从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天全站免登陆