Use Python to Inject Macros into Spreadsheets

前端 未结 4 768
自闭症患者
自闭症患者 2020-11-30 04:02

I\'ve got a macro that I\'d like a bunch of existing spreadsheets to use. The only problem is that there are so many spreadsheets that it would be too time consuming to do i

4条回答
  •  甜味超标
    2020-11-30 04:45

    As I also struggled some time to get this right, I will provide another example which is supposed to work with Excel 2007/2010/2013's xlsm format. There is not much difference to the example provided above, it is just a little bit more simple without the looping over different files and with more comments included. Besides, the macro's source code is loaded from a textfile instead of hard-coding it in the Python script.

    Remember to adapt the file paths at the top of the script to your needs.

    Moreover, remember that Excel 2007/2010/2013 only allows to store Workbooks with macros in the xlsm format, not in xlsx. When inserting a macro into a xlsx file, you will be prompted to save it in a different format or the macro will not be included in the file.

    And last but not least, check that Excel's option to execute VBA code from outside the application is activated (which is deactivated by default for security reasons), otherwise, you will get an error message. To do so, open Excel and go to

    File -> Options -> Trust Center -> Trust Center Settings -> Macro Settings -> activate checkmark on Trust access to the VBA project object model.

    # necessary imports
    import os, sys
    import win32com.client
    
    
    # get directory where the script is located
    _file = os.path.abspath(sys.argv[0])
    path = os.path.dirname(_file)
    
    # set file paths and macro name accordingly - here we assume that the files are located in the same folder as the Python script
    pathToExcelFile = path + '/myExcelFile.xlsm'
    pathToMacro = path + '/myMacro.txt'
    myMacroName = 'UsefulMacro'
    
    # read the textfile holding the excel macro into a string
    with open (pathToMacro, "r") as myfile:
        print('reading macro into string from: ' + str(myfile))
        macro=myfile.read()
    
    # open up an instance of Excel with the win32com driver
    excel = win32com.client.Dispatch("Excel.Application")
    
    # do the operation in background without actually opening Excel
    excel.Visible = False
    
    # open the excel workbook from the specified file
    workbook = excel.Workbooks.Open(Filename=pathToExcelFile)
    
    # insert the macro-string into the excel file
    excelModule = workbook.VBProject.VBComponents.Add(1)
    excelModule.CodeModule.AddFromString(macro)
    
    # run the macro
    excel.Application.Run(myMacroName)
    
    # save the workbook and close
    excel.Workbooks(1).Close(SaveChanges=1)
    excel.Application.Quit()
    
    # garbage collection
    del excel
    

提交回复
热议问题