从Python运行Excel宏(但Excel进程保留在内存中)

时间:2011-12-01 18:35:41

标签: python excel win32com

使用以下python方法,我调用Excel宏。当我开始工作时,我感到很高兴,但是每当我执行此操作时我都想知道我可以看到与我使用宏的.XLA同名的Windows临时/锁定文件。

class XlaMakeUnmake:

    __configFile = 'xla_svn.cfg'
    __vbaTools = 'makeProtected.xla'
    __entries = {}

    def __init__(self):
        self.__readConfig()

    def __newDirs(self, dir):
        try:
            os.makedirs(dir)
        except OSError, e:
            if e.errno != errno.EEXIST:
                raise 

    ### WILL ONLY WORK FOR UNPROTECTED
    '''
    filePath: path of XLA to unmake
    outputDir: folder in which the lightweight xla and its components will be pushed to
    '''
    def unmake(self, filePath, outputDir):
        xl = win32com.client.Dispatch("Excel.Application")
        xl.Visible = 0
        self.__newDirs(outputDir)
        xl.Application.Run("'" + os.getcwd() + os.sep + self.__vbaTools +   "'!Unmake", filePath, outputDir)

当我打开任务管理器时,我可以看到Excel进程仍在运行...如何在作业完成后以干净的方式将其删除? xl.Application.Run是否启动了对宏的异步调用?在这种情况下,它可能会很棘手...

谢谢你们! ;)

1 个答案:

答案 0 :(得分:2)

我不懂Python,但你需要使用:

xl.Quit