Python Forum
run an excel macro with python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: run an excel macro with python (/thread-16462.html)



run an excel macro with python - mmolet - Mar-01-2019

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


RE: run an excel macro with python - Vinci141 - Apr-02-2019

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