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

0 投票
1 回答
1036 浏览
提问于 2025-04-17 07:29

我用下面这个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 个回答

2

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

xl.Quit

撰写回答