HI am trying to open an excel using python codes on windows (python version is 3.6.10)
I used codes from the linkRunning an Excel macro via Python?
import win32com.client as winclexcel_macro = wincl.DispatchEx("Excel.application")excel_path = os.path.expanduser("Valuation.xlsm")workbook = excel_macro.Workbooks.Open(Filename = excel_path, ReadOnly =1)but the excel doesn't open at all, and I don't get any errors either!
if however I run the rest of the code to run the vba macro it does work fine (but the file still doesn't open)
excel_macro.Application.Run("Macro1")excel_macro.Application.Quit()del excel_macroQuestion is, how do I simply open the excel file using python?
3 Answers3
Simply make the Excel application visible. Without setting thisApplication.Visible property toTrue, the application runs in background and be sure not to callQuit. And of course with any API integration, be sure to wrap intry/except:
import win32com.client as winclexcel_path = os.path.expanduser("Valuation.xlsm")try: excel_app = wincl.DispatchEx("Excel.application") workbook = excel_app.Workbooks.Open(Filename = excel_path, ReadOnly =1) # RUN PROCESSING excel_app.Run("Macro1") # LAUNCH EXCEL VISIBLY TO SCREEN excel_app.Visible = Trueexcept Exception as e: print(e)finally: workbook = None; excel_app = None del workbook; del excel_app1 Comment
import osos.system("start EXCEL.EXE dummyfile.xlsx")
Comments
Really simple way to open excel file saving the structure is using pandas
import pandas as pddf = pd.read_excel(excel_path)Comments
Explore related questions
See similar questions with these tags.


