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?
CodePudding user response:
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 :)
CodePudding user response:
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