0

I am having issues with opening an Excel file from Jupyter Notebooks on my Mac. I previously saved it to my cloud.

To create the Excel, I copy my df to Excel and save it. So far so good.

path = '/Users/username/name.xlsx'

writer = pd.ExcelWriter(path, 
                        engine = 'xlsxwriter') 

df.to_excel(writer, sheet_name = 'Sheet1') 

writer.save() 
writer.close()

When I then try to launch it, it won't work.

Here's my code:

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

All I get is Output 256 in my notebook and Excel won't open.

Would anyone know why?

Lucas
  • 17
  • 6
  • I just used [here](https://stackoverflow.com/a/29823295/8508004) to trigger opening an Excel file. It ran in Jupyter and then Excel opened the file on my system. You don't say what type of system you are running it on? That works on Linux and Macs it says. I tried on a Mac. From your path example, it seems you aren't on Windows, and so hopefully that may work. I used `%cd` to change the working directory in Jupyter to where the Excel file was and I assigned 'Filename' the name of the file I wanted in my working directory. – Wayne Oct 28 '22 at 19:32
  • Oh wait. About the command you have. The `path` part isn't going to work. Shouldn't it be more like: `os.system(f"open -a '/Applications/Microsoft Excel.app' {path}")`, where you use f-string to have the path become the string before it passes it to your system shell? Right now you have path only as a string in the command and `path` doesn't mean what you want to the shell. – Wayne Oct 28 '22 at 19:40

2 Answers2

0

Have you tried this:

import os
os.system("start EXCEL.EXE  path")

If that dosn't work try yo use this Use Python to launch Excel file as help :)

CobraCabbe
  • 105
  • 6
  • Note that the `.EXE` extension is specific to windows and it looks _from the paths in the question_ as if the OP is on Mac so this **will not** work for the OP. – Steve Barnes Oct 29 '22 at 08:55
0

You need to pass the Python string stored in path as a value into the shell system in a form it will use.

An option is to use f-strings to do that:

import os
path = '/Users/username/name.xlsx'
os.system(f"open -a '/Applications/Microsoft Excel.app' {path}")

Alternatively, you can drop use of os.system and use IPython/Jupyter's ability to run shell commands on a line in a cell where you start with an exclamation point. That has built in that it will pass in Python variables for bracketed items in the call to the shell.
So in your Jupyter cell, you put:

path = '/Users/username/name.xlsx'
!open -a '/Applications/Microsoft Excel.app' {path}

On my Mac, I was able to replace the equivalent of /Users/username with ~. And so path = '~/name.xlsx' also worked from in a Jupyter to trigger launching of the Excel application with the specified spreadsheet file open.

You were using the full path, and so I kept that in my examples.
You can also change the working directory inside the notebook using the magic command %cd. If you change the current working directory to the directory where the file to open resides, you can and then shorten the 'path' to just be the name of the file.

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • 1
    Thanks so much, `os.system(f"open -a '/Applications/Microsoft Excel.app' {path}")` did the trick for me. – Lucas Oct 29 '22 at 21:09