15

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!

C2P1
  • 397
  • 2
  • 4
  • 13
  • Those are exit codes. `0` means success, and any other number means failure. Often the `man` page for a program will tell you a list of the failure codes and what they mean. – zondo Mar 11 '16 at 12:58

7 Answers7

18

Just these two lines

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

Provided that file.xlsx is in the current directory.

kenlukas
  • 3,616
  • 9
  • 25
  • 36
Ahmed Adewale
  • 2,943
  • 23
  • 19
  • 2
    A tweak: for filenames with embedded blanks enclose the entire parameter with SINGLE quotes and put DOUBLE quotes around the file name (e.g. ' start EXCEL.EXE " C:/temp/new file.csv " '. The extra spaces don't hurt and can prevent parameters from being "merged" when the command is run. BTW on Python 3.x (and possibly 2.x) you should use '/' as the file separator, never "\" - even on Windows. Python will do the conversion so you don't need to use double backslashes in paths (necessary to prevent the first character from being interpreted as a control character, e.g "\n" = line feed). – user1459519 Nov 21 '18 at 20:39
  • Thanks for the addition user1459519 it was helpfull. – Ahmed Adewale Dec 27 '18 at 20:13
  • 1
    Worth noting `os.system('start excel.exe file.xlsx')` will open file with same name from your documents folder and `os.system('start "excel.exe" "file.xlsx"')` (note the extra quotes) will open file from same folder as code is in. Testing this on my end I am not sure y this happens but probably an important distinction. – Mike - SMT Nov 04 '19 at 19:20
9

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

Deusdeorum
  • 1,426
  • 2
  • 14
  • 23
  • 2
    What does the `a` parameter do? Thank you. – Moondra Jul 26 '17 at 20:36
  • 1
    Just a heads up! xlsxwriter can overwrite your Excel files while OpenPyxl will not. The best way is to copy the file in another folder. – technazi Oct 02 '18 at 17:44
  • In mac, `os.system("open -a 'Microsoft Excel' 'path/file.xlsx'")` and this works too: `subprocess.check_call(['open', '-a', 'Microsoft Excel','path/file.xlsx'])` – Javiar Sandra Jan 04 '21 at 19:15
6

I don't know about Mac OS, but if Windows Operating System is the case and provided the Microsoft Windows is properly installed, then consider using :

import os
os.system('start "excel" "C:\\path\\to\\myfile.xlsx"')

double-quotation is important for excel and C:\\path\\to\\myfile.xlsx ( where C just denotes the letter for the partition within the file system, might be replaced by D,E ..etc. ), and single-quotation is needed for the whole string within the os.system().

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
6

On Windows 10, this works for me:

import os
full_path_to_file = "C:\blah\blah\filename.xlsx"
os.system(full_path_to_file)

Copy-pasting any full path to a file in the command prompt (or passing it to os.system()) seems to work as long as you have permission to open the file. I suppose this only works when Excel is selected as default application for the .xlsx extention.

2

As in: How to open an Excel file with Python to display its content?

import os
file = "C:\\Documents\\file.xlsx"
os.startfile(file)

It opens the file with the default application.

Camilo
  • 35
  • 4
0

If you want this to work with your most recent downloaded xlsx file, you can use the following:

import glob

import os

list_of_files = glob.glob('C:\\Downloads\\*.xlsx')   *#note the .xlsx extension is needed to retrieve the last downloaded xlsx file.* 

latest_file = max(list_of_files, key=os.path.getctime)

os.system(latest_file)
Azhar Khan
  • 3,829
  • 11
  • 26
  • 32
Esanel
  • 1
  • 1
0

A minor improvement that handles spaces and explicitly uses Excel:

import os
file = "C:/Documents/file name with spaces.xlsx"
file_with_quotes = '"' + file + '"'
os.system('start "EXCEL.exe" {}'.format(file_with_quotes))