IronPython - 运行Excel宏 [英] IronPython - Run an Excel Macro

查看:649
本文介绍了IronPython - 运行Excel宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行TIBCO Spotfire v4。它内置了IronPython。希望运行一个Spotfire报告,它将导出.xls文件(该部分已完成)。寻找打开Excel文件的脚本,并运行一个宏来格式化文件。



这是我发现并尝试使用的一些代码。不知道进口物品来自哪里!

  import os,os.path,win32com.client 

def run_macro(fName,macName,path = os.getcwd()):

pre:fName是具有宏macName的有效Excel文件的名称
post:fName!macName运行,fName保存并关闭

fName = os.path.join(path,fName)
xlApp = win32com.client.Dispatch(Excel.Application)
fTest = xlApp.Workbooks.Open (fName)
macName = fTest.Name +'!'+ macName xlApp.Run(macName)
fTest.Close(1)
xlApp.Quit()
xlApp =无

编辑器 - 代码看起来来自无法从Python中迭代VBA宏

解决方案

我有一个类似的问题(试图从ipy运行一个Excel VBA宏),最终得到它的工作。



尝试此代码:

 #.net访问
import clr
clr.AddReference(Microsoft.Office.Interop.Excel)

#打开工作簿
excel = Excel.ApplicationClass()
excel。 Visible = True
excel.DisplayAlerts = False
workbook = excel.Workbooks.Open(rC:\your\file\here.xls)
#或在本地打开:
#workbook = ex.Workbooks.Open('here.xls')

#运行宏
excel.Run('YOUR-MACRO-NAME')

#关闭
excel.Exit()

第一部分是使用.NET,第二部分实际上是执行宏。请注意,这是从不打开的excel文件运行excel代码。如果你想从一个已经打开的文件运行一个宏(我也必须做的,所以我可以这样放在这里),你需要将它识别为一个活动对象,而不是打开它(见下面)。运行宏的代码仍然与上述相同。

 #识别已经打开的窗口
从系统.Runtime.InteropServices import Marshal
excel = Marshal.GetActiveObject(Excel.Application)

当我寻找解决方案时,此链接非常有用:



http://www.ironpython.info/index.php?title=Interacting_with_Excel


I'm running TIBCO Spotfire v4. It has IronPython built in. Looking to run a Spotfire report that will export a .xls file (that part is done). Looking for a script to open an Excel file and run a macro to be able to format the file.

Here's some code I found and tried using. Not sure where the import items come from!

import os, os.path, win32com.client

def run_macro(fName, macName, path=os.getcwd()):
    """ 
    pre: fName is the name a valid Excel file with macro macName
    post: fName!macName is run, fName saved and closed
    """ 
    fName = os.path.join(path, fName) 
    xlApp = win32com.client.Dispatch("Excel.Application") 
    fTest = xlApp.Workbooks.Open(fName) 
    macName = fTest.Name + '!' + macName xlApp.Run(macName) 
    fTest.Close(1)
    xlApp.Quit() 
    xlApp = None

EDITOR - Code looks to be from Cannot iterate VBA macros from Python.

解决方案

I had a similar problem (trying to run an Excel VBA Macro from ipy), and ended up getting it working.

Try this code:

# .net access
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")

# opening the workbook
excel = Excel.ApplicationClass()   
excel.Visible = True
excel.DisplayAlerts = False   
workbook = excel.Workbooks.Open(r"C:\your\file\here.xls")
# or open locally:
# workbook = ex.Workbooks.Open('here.xls')

# running the macro
excel.Run('YOUR-MACRO-NAME')

# closing down
excel.Exit()

The first section is using .NET , and the second part is actually executing the macro. Note that this is to run excel code from a excel file that is not open. If you want to run a macro from a file that is already open (which something I also had to do, so figured I may as well put in here), you would need to recognize it as an active object instead of opening it (see below). The code to run the macro would still be the same as above.

# recognizing an already opened window
from System.Runtime.InteropServices import Marshal
excel = Marshal.GetActiveObject("Excel.Application")

This link was very useful when I was looking for a solution:

http://www.ironpython.info/index.php?title=Interacting_with_Excel

这篇关于IronPython - 运行Excel宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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