Need skeleton code to call Excel VBA from PythonWin

后端 未结 2 876
故里飘歌
故里飘歌 2020-12-16 08:39

I need to invoke a VBA macro within an Excel workbook from a python script. Someone else has provided the Excel workbook with the macro. The macro grabs updated values fro

2条回答
  •  春和景丽
    2020-12-16 09:15

    OK, I got it! Thanks for the help on the Application.Run method. This info, plus the "Microsoft Excel Visual Basic Reference": http://msdn.microsoft.com/en-us/library/aa209782(office.10).aspx--as recommended by Hammond & Robinson in "Python Programming on Win32"--was what was needed.

    Here's the skeleton code:

    import win32com.client
    xl=win32com.client.Dispatch("Excel.Application")
    xl.Workbooks.Open(Filename="",ReadOnly=1)
    xl.Application.Run("")
    #...access spreadsheet data...
    xl.Workbooks(1).Close(SaveChanges=0)
    xl.Application.Quit()
    xl=0
    

提交回复
热议问题