Use Python to launch Excel file

烈酒焚心 提交于 2019-12-18 05:16:22

问题


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!


回答1:


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




回答2:


Just these two lines

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

Provided that file.xlsx is in the current directory.




回答3:


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().



来源:https://stackoverflow.com/questions/35940748/use-python-to-launch-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!