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

2024-04-25 07:43:35 发布

您现在位置:Python中文网/ 问答频道 /正文

使用下面的python方法,我调用一个Excel宏。我很高兴当我得到它的工作,但是我想知道每次我执行这个我可以看到一个Windows临时/锁文件的名称与我使用宏的.XLA相同。在

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是否正在启动对宏的异步调用?在这种情况下可能会很棘手。。。在

谢谢各位!!;)


Tags: toselfapplicationosdefdirexceloutputdir