Python Forum
Run macros of excel sheet from python scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Run macros of excel sheet from python scripts
#1
Hello,
I am getting the following error while running the macro using python scripts

Error:
Traceback (most recent call last): File "C:\Users\username\AppData\Local\Programs\Python\Python37\macro_call.py", line 25, in <module> excel.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook()') # This runs the macro that is on Sheet1 File "C:\Users\sri\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x7\_Application.py", line 376, in Run , Arg26, Arg27, Arg28, Arg29, Arg30 File "C:\Users\sri\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\__init__.py", line 467, in _ApplyTypes_ self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Cannot run the macro 'workbook1.xlsm!Grey Scaling.OpenWorkbook()'. The macro may not be available in this workbook or all macros may be disabled.", 'xlmain11.chm', 0, -2146827284), None)
Following is the code used:
import win32com.client as win32
excel = win32.Dispatch("Excel.Application") # create an instance of Excel
book = excel.Workbooks.Open(Filename=r'C:\Users\workbook1.xlsm')
excel.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook') # This runs the macro that is on Sheet1 whose name is Grey Scalling
book.Save()
book.Close()
excel.Quit()
Thanks in advance for your help
Reply
#2
try
import win32com.client as win32
excel = win32.Dispatch("Excel.Application") # create an instance of Excel
book = excel.Workbooks.Open(Filename=r'C:\Users\workbook1.xlsm')
excel.Application.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook') # This runs the macro that is on Sheet1 whose name is Grey Scalling
book.Save()
book.Close()
excel.Application.Quit()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for the reply.
I tried above code still getting the same error.
Error:
Traceback (most recent call last): File "C:\Users\username\AppData\Local\Programs\Python\Python37\macro_call.py", line 25, in <module> excel.Application.Run('workbook1.xlsm!Grey Scaling.OpenWorkbook') # This runs the macro that is on Sheet1 File "C:\Users\sri\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x7\_Application.py", line 376, in Run , Arg26, Arg27, Arg28, Arg29, Arg30 File "C:\Users\sri\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\__init__.py", line 467, in _ApplyTypes_ self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args), pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Cannot run the macro 'workbook1.xlsm!Grey Scaling.OpenWorkbook'. The macro may not be available in this workbook or all macros may be disabled.", 'xlmain11.chm', 0, -2146827284), None)
Reply
#4
what is the macro security setting?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to communicate between scripts in python via shared file? daiboonchu 4 1,470 Dec-31-2024, 01:56 PM
Last Post: Pedroski55
  New on python. Needs help with Google sheet jeromep 1 846 Apr-25-2024, 06:47 PM
Last Post: deanhystad
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 4,877 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Trying to us python.exe from our network to run scripts cubangt 3 1,903 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 16,686 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Reading Excel file and use a wildcard in file name and sheet name randolphoralph 6 11,152 Jan-13-2022, 10:20 PM
Last Post: randolphoralph
  Python script for excel sheet Nabil 4 4,650 Jun-01-2021, 05:09 AM
Last Post: Pedroski55
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 2,717 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Running python scripts from github etc pacmyc 7 4,997 Mar-03-2021, 10:26 PM
Last Post: pacmyc
  Append excel sheet using openpyxl TammyP 1 3,376 Feb-02-2021, 06:32 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020