Python Forum
run an excel macro with python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run an excel macro with python
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 6,147 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Auto-populate Macro variables Spartan314 3 4,240 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  How do I reduce the time to Invoke Macro via Python? JaneTan 1 3,203 Dec-28-2020, 06:46 AM
Last Post: buran
  Jython macro which uses PythonInterpreter and Redis script gives an error rkanumola 2 3,447 Oct-30-2019, 07:37 AM
Last Post: rkanumola
  SAS Equivalent of Macro variable in Python AI_ML_Neophyte 4 15,348 Mar-29-2019, 08:14 AM
Last Post: FelixS
  Is it possible to access and list all macro names in an excel file? kwfine 0 3,373 Feb-07-2018, 04:21 PM
Last Post: kwfine
  No output from xlwings RunPython Macro branches 0 4,410 Oct-29-2017, 04:27 PM
Last Post: branches

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.