Python Forum

Full Version: run an excel macro with python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Coders, I am looking for a code that would run an excel macro (from 1 sheet), I have been searching the internet for days but I cannot find any good codes for a MAC to borrow, does anyone have a suggestion?
Many thanks in advance
def runMacro():
    # Run the macro :DONE
    # output in excel sheet
    if os.path.exists( r"<Path to macro>" ):
        xl = win32com.client.Dispatch( "Excel.Application" )
        print( "Opening Macro File now." )
        time.sleep( 2 )
        xl.Workbooks.Open( os.path.abspath( r"<Path to macro> ) )
        print( "Running Macro now..." )
        xl.Application.Run( r"<Macro_name>" )
        #xl.ActiveWorkbook.SaveAs( Filename=r"Output_report", FileFormat="56" )
        xl.ActiveWorkbook.Close()
        #print( "File Saved. Exiting now." )
        # xl.Application.Close()
        xl.Application.Quit()
        del xl