0

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_macro

Question is, how do I simply open the excel file using python?

askedApr 30, 2020 at 17:48
user13412850's user avatar

3 Answers3

0

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_app
answeredApr 30, 2020 at 19:38
Parfait's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

perfect! that was helpful.
0

import osos.system("start EXCEL.EXE dummyfile.xlsx")

answeredApr 30, 2020 at 18:13
kavi Raj's user avatar

Comments

0

Really simple way to open excel file saving the structure is using pandas

import pandas as pddf = pd.read_excel(excel_path)
answeredApr 30, 2020 at 18:22
m_chera's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.