How can I launch an instance of an application using Python?

后端 未结 7 1357
北恋
北恋 2020-12-09 04:57

I am creating a Python script where it does a bunch of tasks and one of those tasks is to launch and open an instance of Excel. What is the ideal way of accomplishing that i

相关标签:
7条回答
  • 2020-12-09 05:17

    or

    os.system("start excel.exe <path/to/file>")
    

    (presuming it's in the path, and you're on windows)

    and also on Windows, just start <filename> works, too - if it's an associated extension already (as xls would be)

    0 讨论(0)
  • 2020-12-09 05:22

    I like popen2 for the ability to monitor the process.

    excelProcess = popen2.Popen4("start excel %s" % (excelFile))
    status = excelProcess.wait()
    

    https://docs.python.org/2/library/popen2.html

    EDIT: be aware that calling wait() will block until the process returns. Depending on your script, this may not be your desired behavior.

    0 讨论(0)
  • 2020-12-09 05:30

    The subprocess module intends to replace several other, older modules and functions, such as:

    • os.system
    • os.spawn*
    • os.popen*
    • popen2.*
    • commands.*

    .

    import subprocess
    
    process_one = subprocess.Popen(['gqview', '/home/toto/my_images'])
    
    print process_one.pid
    
    0 讨论(0)
  • As others have stated, I would suggest os.system. In case anyone is looking for a Mac-compatible solution, here is an example:

    import os
    os.system("open /Applications/Safari.app")
    
    0 讨论(0)
  • 2020-12-09 05:38

    While the Popen answers are reasonable for the general case, I would recommend win32api for this specific case, if you want to do something useful with it:

    It goes something like this:

    from win32com.client import Dispatch
    xl = Dispatch('Excel.Application')
    wb = xl.Workbooks.Open('C:\\Documents and Settings\\GradeBook.xls')
    xl.Visible = True    # optional: if you want to see the spreadsheet
    

    Taken from a mailing list post but there are plenty of examples around.

    0 讨论(0)
  • 2020-12-09 05:40

    os.system("open file.xls")

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