0

HI am trying to open an excel using python codes on windows (python version is 3.6.10)

I used codes from the link Running an Excel macro via Python?

import win32com.client as wincl

excel_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?

3 Answers 3

0

Simply make the Excel application visible. Without setting this Application.Visible property to True, the application runs in background and be sure not to call Quit. And of course with any API integration, be sure to wrap in try/except:

import win32com.client as wincl

excel_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 = True

except Exception as e:
    print(e)

finally:
    workbook = None; excel_app = None
    del workbook; del excel_app
Sign up to request clarification or add additional context in comments.

1 Comment

perfect! that was helpful.
0

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

Comments

0

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

import pandas as pd
df = pd.read_excel(excel_path)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy 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.