Use Python to launch Excel file

user283471 picture user283471 · Mar 11, 2016 · Viewed 23.1k times · Source

when i try os.system("open " + 'myfile.xlsx')
i get the output '0'

similarly, trying
os.system("start excel.exe myfilepath")
gives the result 32512

I have imported os and system, and I'm on mac. How can I change this so it does actually launch that excel file? And out of curiosity, what do the numbers it prints out mean?

Thanks!

Answer

Deusdeorum picture Deusdeorum · Mar 11, 2016

If you only want to open the excel application you could use subprocess:

import subprocess
subprocess.check_call(['open', '-a', 'Microsoft Excel'])

You can also use os and open a specific file:

import os
os.system("open -a 'path/Microsoft Excel.app' 'path/file.xlsx'")

If you on other hand want to open an excel file within python and modify it there's a number of packages to use as xlsxwriter, xlutils and openpyxl where the latter is prefered by me.

Another note, if you're on mac the excel application isn't .exe