Run Excel macro from different Excel file - python

I know there already are many questions regarding this topic, but I couldn't figure out a solution that works out for me.
I want to run an Excel macro stored in an 'xlsm' file, but I need to run it on a different Excel file ('xlsx').
Whenever I try the code below, I get an error message saying that the macro wasn't found, so I suppose the problem has to do with the pathing.
This is what I have:
import win32com.client
import os
try:
excel = win32com.client.Dispatch('Excel.Application')
excel_path = r'C:\Users\martin\Desktop\testing_excel.xlsx'
workbook = excel.Workbooks.Open(excel_path, ReadOnly=1)
excel.Application.Run("'macro_testing.xlsm'!local_macro")
workbook.Save()
excel.Quit()
print('Macro run succesfully')
except Exception as e:
print(e)
excel.Quit()
The 'xlsm' file with the macro is named 'macro_testing.xlsm', and the macro name is 'local_macro'.
Thanks in advance!

When I have to do similar, I store the macro in a command book (.xlsm) - or even .xlam that is called from the command book (which is .xlsm) and it then opens, manipulates, saves and closes .xlsx as appropriate.
If you try to run it from within an .xlsx, you'll never be able to save the code that has done the run, not exactly advisable for repetitive workflows.
So,
macro_testing.xlsm could have another sub which would be something like:
sub runExternal()
with Sheets("Sheet1").
extPath = cells(1,2)
extBook = cells(2,2)
'Declaring the path & workbook to use within the master workbook's "Sheet1"
workbooks.open(extPath & extBook) ReadOnly:False
call local_macro()
application.displayalerts = false 'Just in case there are any GUI prompts
workbooks(extBook).close saveChanges:=True
application.displayalerts = true
end sub

For anybody there who was experiencing the same problem I had, what I finally did was open the two Excel files, like this:
import os
excel_path = os.path.abspath('Excel_to_run_macro.xlsx')
if os.path.exists(excel_path):
xl = win32com.client.Dispatch('Excel.Application')
xl.Workbooks.Open(os.path.abspath('Excel_with_macro.xlsm'), ReadOnly=1)
workbook = xl.Workbooks.Open(excel_path, ReadOnly=1)
xl.Application.Run("'Excel_with_macro.xlsm'!Macro_name")
workbook.Save()
xl.Application.Quit()
del xl

Related

Question about error when saving xlsm file using xlwings

I want to open an xlsm file via xlwings and then edit it and save it. However, some problems arose.
If I run the code with no excel file working, or just open another excel file and do not edit the excel file, it works fine. However, if I open an Excel file and do some work, for example open a blank Excel file and enter 'test' in cell A1, and run the code, sometimes it works, but sometimes it becomes unresponsive in the third line.(wb_xl = xw.Book(copy)) In this case, the code does not jump from the third line in an unresponsive state. What makes more sense is that the code works fine in some cases.
I want to know when the code works fine in all cases.
And there is one more problem.
If this code is executed while working with another Excel, only wb_xl should be terminated. I don't want another Excel to be closed. I want to exit only wb_xl. However, when the app.quit() code is executed, all open Excels are closed. In this case, how can I close only the Excel(wb_xl) opened through the code without closing the working Excel?
import xlwings as xw
copy = 'C:/Users/ijung/Desktop/210919_Mk_Lot_test/210922_101test.xlsm'
wb_xl = xw.Book(copy) #sometimes no response in this line
ws_xl = wb_xl.sheets['Main']
app = xw.apps.active
ws_xl.range('A1').value = 'test'
wb_xl.save()
app.quit()
#wb_xl.app.kill()
#wb_xl.close()
I also used openpyxl. However, in this part of wb_open.save(copy), an error such as xml.etree.ElementTree.ParseError: mismatched tag: line 20, column 8 occurred. When I use xlsx, the save works fine, but when I use xlsm, an error occurs.
import openpyxl
wb_open = openpyxl.load_workbook(copy, read_only = False, keep_vba = True)
ws_open = wb_open.active
ws_open.cell(1,1).value = 'test'
wb_open.save(copy) #error
wb_open.close()
As a result, the purpose of this code is to open the xlsm file by executing this code even when working with another Excel, edit and save, and close only this xlsm file.However, using multiple packages and searching multiple sites could not solve the problem.I'm under a lot of stress with this issue. Any help would be greatly appreciated. Please help me.
Thanks in advance.
openpyxl does not works with xlsm files that contains form objects
I think the problem is in app.quit() you are closing the excel instance, just use wb_xl.close()
import xlwings as xw
copy = 'C:/Users/ijung/Desktop/210919_Mk_Lot_test/210922_101test.xlsm'
wb_xl = xw.Book(copy) #sometimes no response in this line
ws_xl = wb_xl.sheets['Main']
#app = xw.apps.active # don't needed
ws_xl.range('A1').value = 'test'
wb_xl.save()
wb_xl.close()
This should only close the book, take a look this post has insteresting answers

How to ignore "Enable Editing" in excel after writing data using openpyxl

I am using a Excel template which have 6 tabs (All unprotected) and writing the data on each worksheet using openpyxl module.
Once the excel file is created and when tried to open the generated file, its not showing all data untill and unless I click "Enable editing" pop up.
Is there any attribute to disable in openpyxl.
This sounds like Windows has quarantined files received over a network. As this is done when the files are received, there is no way to avoid this when creating the files.
I solved this for me.
I found the answer here:
https://codereview.stackexchange.com/questions/240136/python-script-for-refreshing-and-preprocessing-data-in-excel-report-files
I only used the refresh function and it basically opened the excel file, click/refreshed, and closed/saved. You see an Excel file appear briefly on the screen. I'll insert this in a loop to go through all the files I am creating. It might take a little while to run hundreds, but much faster than open-click-save.
Here is all the code I used:
import win32com.client as win32
def refresh(directory, file_name):
xlapp = win32.DispatchEx('Excel.Application')
xlapp.DisplayAlerts = False
xlapp.Visible = True
xlbook = xlapp.Workbooks.Open(directory + '\\' + file_name)
xlbook.RefreshAll()
xlbook.Save()
xlbook.Close()
xlapp.Quit()
return()

Using python to call a excel macro

I'm trying to write a python code that will run a macro in a excel workbook. I know the file name and name of the vba macro. I don't need to read any information into the python file I simple need to perform this function.
I ultimately want to run this python code periodically and I want it to call the same VBA function multiple times in a day.
import openpyxl
excel_document = openpyxl.load_workbook('python test.xlsm')
print type(excel_document)
I found this Code on Stackoverflow. Maybe it helps you.
Resource:
Running an Excel macro via Python?
import win32com.client
if os.path.exists("excelsheet.xlsm"):
xl = win32com.client.Dispatch("Excel.Application")
wb = xl.Workbooks.Open(os.path.abspath("C:\Users\{Username}\Desktop\test.xlsm"), ReadOnly=0)
wb.Sheets('Sheet1').Select()
xl.Application.Run("test.xlsm!Module1.macroname")
## xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function.
xl.Application.Quit() # Comment this out if your excel script closes
del xl```

Python: Open Excel Workbook using Win32 COM Api

I'm using the following code to open and display a workbook within Excel:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('my_sheet.xlsm')
ws = wb.Worksheets('blaaaa')
excel.Visible = True
When the File 'my_sheet.xlsm' is already opened, Excel asks me whether I want to re-open it without saving.
How could I check in advance whether the workbook is already opened up and in case it is, just bring it to the front?
EDIT: Found out by now:
if excel.Workbooks.Count > 0:
for i in range(1, excel.Workbooks.Count+1):
if excel.Workbooks.Item(i).Name is 'my_sheet.xlsm':
wb = excel.Workbooks.Item(i)
break
And one more question: My worksheet contains some headers where I enabled some filtering. So when the filters are set, and when I open the Workbook from Python, it sometimes asks me to enter a unique name to save the filter. Why is that? This is the dialogue:
EDIT Ok here it says (in german language) that the latter problem is a known bug in 2007 and 2010 files: https://social.msdn.microsoft.com/Forums/de-DE/3dce9f06-2262-4e22-a8ff-5c0d83166e73/excel-api-interne-namen and it seems to exist if you open Excel-Files programmatically. Don't know whether there is a workaround.
While you found a solution, consider using try/except/finally block. Currently your code will leave the Excel.exe process running in background (check Task Manager if using Windows) even if you close the visible worksheet. As an aside, in Python or any other language like VBA, any external API such as this COM interface should always be released cleanly during application code.
Below solution uses a defined function, openWorkbook() to go two potential routes: 1) first attempts to relaunch specified workbook assuming it is opened and 2) if it is not currently opened launches a new workbook object of that location. The last nested try/except is used in case the Workbooks.Open() method fails such as incorrect file name.
import win32com.client as win32
def openWorkbook(xlapp, xlfile):
try:
xlwb = xlapp.Workbooks(xlfile)
except Exception as e:
try:
xlwb = xlapp.Workbooks.Open(xlfile)
except Exception as e:
print(e)
xlwb = None
return(xlwb)
try:
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = openWorkbook(excel, 'my_sheet.xlsm')
ws = wb.Worksheets('blaaaa')
excel.Visible = True
except Exception as e:
print(e)
finally:
# RELEASES RESOURCES
ws = None
wb = None
excel = None

Why does Excel macro work in Excel but not when called from Python?

I have an Excel macro that deletes a sheet, copies another sheet and renames it to the same name of the deleted sheet. This works fine when run from Excel, but when I run it by calling the macro from Python I get the following error message:
Run-time error '1004' - Cannot rename a sheet to the same name as
another sheet, a referenced object library or a workbook referenced by
VisualBasic.
The macro has code like the following:
Sheets("CC").Delete
ActiveWindow.View = xlPageBreakPreview
Sheets("FY").Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "CC"
and the debugger highlights the error on the last line where the sheet is renamed. I've also tried putting these calls directly in python but get the same error message.
Any suggestions are much appreciated!
Thanks.
I ran the code inside Excel VBA.
I am guessing that the following line is failing.
Sheets("CC").Delete
And that is the reason, you can't give the new sheet same name as existing (non-deleted) sheet.
Put Application.DisplayAlerts = False before Sheets("CC").Delete and Application.DisplayAlerts = True once you are finished with the code.
I haven't used python but it seems the library is swallowing that error for you and letting you go ahead to the next statement.
Hope that helps.
Behind the scenes, VB and VBA are maintaining references to COM objects for the application, worksheets etc. This is why you have the globals 'Application', 'Worksheets' etc. It is possible that VBA is still holding a reference to the worksheet, so Excel hasn't tidied it up properly.
Try not using these implicit globals and referencing the items in the object model explicitly. Alternatively you could do it directly in Python.
Here's a python script that will do something like what you want:
import win32com.client
xl = win32com.client.Dispatch ('Excel.Application')
xl.Visible = True
wb = xl.Workbooks.Add()
wb.Worksheets[0].Delete()
wb.Worksheets.Add()
wb.Worksheets[0].Name = 'Sheet1'

Categories