使用Python调用打开文件夹中所有文件的Excelmacros

我有一个Python程序(如块1所示),打开一个Excel文件并调用一个macros。

Excelmacros(如框2所示)打开特定文件夹中的所有Excel文件。

Python程序打开Excel文件并调用macros,我用不同的文件testing了它,但是当我尝试这个Excel文件时,它会打开,但macros不能运行。

这是Python

from win32com.client import Dispatch xl = Dispatch('Excel.Application') wb = xl.Workbooks.Open("K:\\updatebloomberg.xlsm") xl.Visible = True wb.Application.Run("'updatebloomberg.xlsm'!Module1.runfiles()") wb.Close(True) 

这里是VBA

 Sub runfiles() Dim wb As Workbook Dim myPath As String Dim myFile As String Dim myExtension As String Dim ws As Worksheet Dim c As Range myPath = "K:\Bloomberg Data\" myExtension = "*.xlsx" myFile = Dir(myPath & myExtension) Do While myFile <> "" Set wb = Workbooks.Open(Filename:=myPath & myFile) myFile = Dir Loop Application.OnTime (Now + TimeValue("00:00:02")), "refreshSheet" End Sub 

为什么在最初你会这样做?

 import glob for fnam in glob.glob("K:\Bloomberg Data\*.xlsx"): os.startfile(fname) 

如果这不起作用,你检查你的networking驱动器((K我假设是一个networking驱动器)没有一个X,当你在文件浏览器中看)