Use Python to launch Excel file

后端 未结 5 1413
日久生厌
日久生厌 2020-12-18 04:58

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

similarly, trying
os.system(\"start excel.exe myfil

相关标签:
5条回答
  • 2020-12-18 05:09

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

    0 讨论(0)
  • 2020-12-18 05:18

    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.

    0 讨论(0)
  • 2020-12-18 05:20

    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

    0 讨论(0)
  • 2020-12-18 05:21
    import os
    
    os.startfile(dirpath + '\\' + 'filename.xlsx')
    
    0 讨论(0)
  • 2020-12-18 05:27

    Just these two lines

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

    Provided that file.xlsx is in the current directory.

    0 讨论(0)
提交回复
热议问题