[{"data":1,"prerenderedAt":579},["ShallowReactive",2],{"doc:\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002Fxlwings-run-macro-from-python-example":3,"surround:\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002Fxlwings-run-macro-from-python-example":571},{"id":4,"title":5,"body":6,"description":564,"extension":565,"meta":566,"navigation":87,"path":567,"seo":568,"stem":569,"__hash__":570},"docs\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002Fxlwings-run-macro-from-python-example\u002Findex.md","xlwings Run Macro from Python Example",{"type":7,"value":8,"toc":559},"minimark",[9,13,32,37,40,346,350,447,451,479,494,512,529,555],[10,11,5],"h1",{"id":12},"xlwings-run-macro-from-python-example",[14,15,16,17,21,22,25,26,31],"p",{},"To run a VBA macro from Python using xlwings, instantiate an ",[18,19,20],"code",{},"xw.App"," object, open the target workbook, bind the subroutine with ",[18,23,24],{},"app.macro()",", and invoke it as a callable. This method routes directly through Excel’s COM (Windows) or AppleScript (macOS) bridge, providing the most reliable execution path for Python developers automating legacy reporting pipelines. For foundational concepts on workbook lifecycle and object mapping, review ",[27,28,30],"a",{"href":29},"\u002Fgetting-started-with-python-excel-automation\u002F","Getting Started with Python Excel Automation"," before integrating macro calls into scheduled jobs.",[33,34,36],"h2",{"id":35},"working-code-example","Working Code Example",[14,38,39],{},"The following pattern handles workbook lifecycle, macro binding, and resource cleanup safely. It assumes your VBA subroutine resides in a standard module and accepts optional positional arguments.",[41,42,47],"pre",{"className":43,"code":44,"language":45,"meta":46,"style":46},"language-python shiki shiki-themes github-light github-dark","import xlwings as xw\nfrom pathlib import Path\n\ndef run_excel_macro(workbook_path: str, macro_name: str, *args):\n \"\"\"Open a workbook, execute a VBA macro, and clean up resources.\"\"\"\n app = xw.App(visible=False) # Set True to debug VBA UI or dialogs\n try:\n wb = app.books.open(str(Path(workbook_path).resolve()))\n # Bind macro using 'WorkbookName!MacroName' syntax\n macro = app.macro(f\"'{wb.name}'!{macro_name}\")\n macro(*args) # Pass parameters positionally\n wb.save()\n except Exception as e:\n print(f\"Macro execution failed: {e}\")\n raise\n finally:\n wb.close()\n app.quit()\n\n# Usage\nrun_excel_macro(\"C:\u002Freports\u002Fmonthly_summary.xlsm\", \"FormatAndExport\", True)\n","python","",[18,48,49,68,82,89,120,127,155,164,180,186,228,242,248,263,288,294,302,308,314,319,325],{"__ignoreMap":46},[50,51,54,58,62,65],"span",{"class":52,"line":53},"line",1,[50,55,57],{"class":56},"szBVR","import",[50,59,61],{"class":60},"sVt8B"," xlwings ",[50,63,64],{"class":56},"as",[50,66,67],{"class":60}," xw\n",[50,69,71,74,77,79],{"class":52,"line":70},2,[50,72,73],{"class":56},"from",[50,75,76],{"class":60}," pathlib ",[50,78,57],{"class":56},[50,80,81],{"class":60}," Path\n",[50,83,85],{"class":52,"line":84},3,[50,86,88],{"emptyLinePlaceholder":87},true,"\n",[50,90,92,95,99,102,106,109,111,114,117],{"class":52,"line":91},4,[50,93,94],{"class":56},"def",[50,96,98],{"class":97},"sScJk"," run_excel_macro",[50,100,101],{"class":60},"(workbook_path: ",[50,103,105],{"class":104},"sj4cs","str",[50,107,108],{"class":60},", macro_name: ",[50,110,105],{"class":104},[50,112,113],{"class":60},", ",[50,115,116],{"class":56},"*",[50,118,119],{"class":60},"args):\n",[50,121,123],{"class":52,"line":122},5,[50,124,126],{"class":125},"sZZnC"," \"\"\"Open a workbook, execute a VBA macro, and clean up resources.\"\"\"\n",[50,128,130,133,136,139,143,145,148,151],{"class":52,"line":129},6,[50,131,132],{"class":60}," app ",[50,134,135],{"class":56},"=",[50,137,138],{"class":60}," xw.App(",[50,140,142],{"class":141},"s4XuR","visible",[50,144,135],{"class":56},[50,146,147],{"class":104},"False",[50,149,150],{"class":60},") ",[50,152,154],{"class":153},"sJ8bj","# Set True to debug VBA UI or dialogs\n",[50,156,158,161],{"class":52,"line":157},7,[50,159,160],{"class":56}," try",[50,162,163],{"class":60},":\n",[50,165,167,170,172,175,177],{"class":52,"line":166},8,[50,168,169],{"class":60}," wb ",[50,171,135],{"class":56},[50,173,174],{"class":60}," app.books.open(",[50,176,105],{"class":104},[50,178,179],{"class":60},"(Path(workbook_path).resolve()))\n",[50,181,183],{"class":52,"line":182},9,[50,184,185],{"class":153}," # Bind macro using 'WorkbookName!MacroName' syntax\n",[50,187,189,192,194,197,200,203,206,209,212,215,217,220,222,225],{"class":52,"line":188},10,[50,190,191],{"class":60}," macro ",[50,193,135],{"class":56},[50,195,196],{"class":60}," app.macro(",[50,198,199],{"class":56},"f",[50,201,202],{"class":125},"\"'",[50,204,205],{"class":104},"{",[50,207,208],{"class":60},"wb.name",[50,210,211],{"class":104},"}",[50,213,214],{"class":125},"'!",[50,216,205],{"class":104},[50,218,219],{"class":60},"macro_name",[50,221,211],{"class":104},[50,223,224],{"class":125},"\"",[50,226,227],{"class":60},")\n",[50,229,231,234,236,239],{"class":52,"line":230},11,[50,232,233],{"class":60}," macro(",[50,235,116],{"class":56},[50,237,238],{"class":60},"args) ",[50,240,241],{"class":153},"# Pass parameters positionally\n",[50,243,245],{"class":52,"line":244},12,[50,246,247],{"class":60}," wb.save()\n",[50,249,251,254,257,260],{"class":52,"line":250},13,[50,252,253],{"class":56}," except",[50,255,256],{"class":104}," Exception",[50,258,259],{"class":56}," as",[50,261,262],{"class":60}," e:\n",[50,264,266,269,272,274,277,279,282,284,286],{"class":52,"line":265},14,[50,267,268],{"class":104}," print",[50,270,271],{"class":60},"(",[50,273,199],{"class":56},[50,275,276],{"class":125},"\"Macro execution failed: ",[50,278,205],{"class":104},[50,280,281],{"class":60},"e",[50,283,211],{"class":104},[50,285,224],{"class":125},[50,287,227],{"class":60},[50,289,291],{"class":52,"line":290},15,[50,292,293],{"class":56}," raise\n",[50,295,297,300],{"class":52,"line":296},16,[50,298,299],{"class":56}," finally",[50,301,163],{"class":60},[50,303,305],{"class":52,"line":304},17,[50,306,307],{"class":60}," wb.close()\n",[50,309,311],{"class":52,"line":310},18,[50,312,313],{"class":60}," app.quit()\n",[50,315,317],{"class":52,"line":316},19,[50,318,88],{"emptyLinePlaceholder":87},[50,320,322],{"class":52,"line":321},20,[50,323,324],{"class":153},"# Usage\n",[50,326,328,331,334,336,339,341,344],{"class":52,"line":327},21,[50,329,330],{"class":60},"run_excel_macro(",[50,332,333],{"class":125},"\"C:\u002Freports\u002Fmonthly_summary.xlsm\"",[50,335,113],{"class":60},[50,337,338],{"class":125},"\"FormatAndExport\"",[50,340,113],{"class":60},[50,342,343],{"class":104},"True",[50,345,227],{"class":60},[33,347,349],{"id":348},"compatibility-requirements","Compatibility & Requirements",[351,352,353,372,385,406,427,437],"ul",{},[354,355,356,360,361,364,365,367,368,371],"li",{},[357,358,359],"strong",{},"xlwings Version",": ",[18,362,363],{},">=0.24.0",". The ",[18,366,24],{}," API replaced ",[18,369,370],{},"wb.macro()"," for consistent scope resolution.",[354,373,374,360,377,380,381,384],{},[357,375,376],{},"Python Version",[18,378,379],{},"3.8+"," recommended for stable ",[18,382,383],{},"pathlib"," and type-hint support.",[354,386,387,360,390,393,394,397,398,401,402,405],{},[357,388,389],{},"Excel Version",[18,391,392],{},"2016+"," or Microsoft 365. Windows relies on ",[18,395,396],{},"pywin32","; macOS uses ",[18,399,400],{},"appscript","\u002F",[18,403,404],{},"osascript",".",[354,407,408,411,412,113,415,418,419,422,423,426],{},[357,409,410],{},"File Format",": Must be ",[18,413,414],{},".xlsm",[18,416,417],{},".xlsb",", or ",[18,420,421],{},".xlam",". Standard ",[18,424,425],{},".xlsx"," files cannot store VBA.",[354,428,429,432,433,436],{},[357,430,431],{},"Macro Security",": Enable macros via ",[18,434,435],{},"File > Options > Trust Center > Macro Settings"," or place the workbook in a Trusted Location. Untrusted files will trigger security prompts that hang headless execution.",[354,438,439,442,443,446],{},[357,440,441],{},"Cross-Platform",": Avoid Windows-specific COM constants. Test with ",[18,444,445],{},"visible=True"," during development to surface silent VBA errors.",[33,448,450],{"id":449},"troubleshooting-common-errors","Troubleshooting Common Errors",[14,452,453,466,467,470,471,474,475,478],{},[357,454,455,458,459,462,463],{},[18,456,457],{},"com_error"," \u002F ",[18,460,461],{},"pywintypes.com_error"," or ",[18,464,465],{},"AppleEventTimeoutError","\nExcel is hung, blocked by security, or already locked by another process. Fix: Add ",[18,468,469],{},"app.display_alerts = False"," before opening, terminate orphaned ",[18,472,473],{},"EXCEL.EXE"," processes (",[18,476,477],{},"taskkill \u002FF \u002FIM EXCEL.EXE","), and verify paths use forward slashes or raw strings.",[14,480,481,486,487,490,491,493],{},[357,482,483],{},[18,484,485],{},"AttributeError: 'App' object has no attribute 'macro'","\nYour xlwings installation is outdated. Run ",[18,488,489],{},"pip install --upgrade xlwings",". Pre-0.24 versions used the deprecated ",[18,492,370],{}," method.",[14,495,496,503,504,507,508,511],{},[357,497,498,499,502],{},"Macro Not Found or ",[18,500,501],{},"NameError"," in VBA","\nExcel resolves macros strictly via ",[18,505,506],{},"'WorkbookName!MacroName'",". For personal macros, use ",[18,509,510],{},"app.macro(\"'Personal.xlam'!MyRoutine\")",". Wrap workbook names containing spaces in single quotes.",[14,513,514,517,518,401,521,524,525,528],{},[357,515,516],{},"Headless\u002FServer Execution Fails","\nxlwings requires a licensed desktop Excel installation. It cannot run in pure cloud environments (AWS Lambda, Docker without GUI, GitHub Actions). Fallback: Port VBA logic to ",[18,519,520],{},"pandas",[18,522,523],{},"openpyxl",", or use ",[18,526,527],{},"win32com.client"," on Windows-only infrastructure.",[14,530,531,534,535,538,539,462,542,545,546,549,550,554],{},[357,532,533],{},"Argument Passing Mismatch","\nxlwings passes arguments positionally only. Map VBA ",[18,536,537],{},"Optional"," parameters to ",[18,540,541],{},"None",[18,543,544],{},"\"\"",". Keyword arguments are unsupported. When aligning Python ",[18,547,548],{},"Range"," objects with VBA expectations, reference the ",[27,551,553],{"href":552},"\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002F","Automating Excel with xlwings Basics"," guide for proper sheet and cell mapping.",[556,557,558],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":46,"searchDepth":70,"depth":70,"links":560},[561,562,563],{"id":35,"depth":70,"text":36},{"id":348,"depth":70,"text":349},{"id":449,"depth":70,"text":450},"To run a VBA macro from Python using xlwings, instantiate an xw.App object, open the target workbook, bind the subroutine with app.macro(), and invoke it as a callable. This method routes directly through Excel’s COM (Windows) or AppleScript (macOS) bridge, providing the most reliable execution path for Python developers automating legacy reporting pipelines. For foundational concepts on workbook lifecycle and object mapping, review Getting Started with Python Excel Automation before integrating macro calls into scheduled jobs.","md",{},"\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002Fxlwings-run-macro-from-python-example",{"title":5,"description":564},"getting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002Fxlwings-run-macro-from-python-example\u002Findex","AOZ1hhbwzmPQKuw2LUwI8Mby6FhgH4Yo4TI_9QUb8J8",[572,575],{"title":553,"path":573,"stem":574,"children":-1},"\u002Fgetting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics","getting-started-with-python-excel-automation\u002Fautomating-excel-with-xlwings-basics\u002Findex",{"title":576,"path":577,"stem":578,"children":-1},"Reading Excel Files with Pandas: A Professional Workflow for Automated Reporting","\u002Fgetting-started-with-python-excel-automation\u002Freading-excel-files-with-pandas","getting-started-with-python-excel-automation\u002Freading-excel-files-with-pandas\u002Findex",1777830515017]