Open document with default OS application in Python, both in Windows and Mac OS

前端 未结 13 1612
刺人心
刺人心 2020-11-22 10:36

I need to be able to open a document using its default application in Windows and Mac OS. Basically, I want to do the same thing that happens when you double-click on the do

13条回答
  •  萌比男神i
    2020-11-22 11:06

    I am pretty late to the lot, but here is a solution using the windows api. This always opens the associated application.

    import ctypes
    
    shell32 = ctypes.windll.shell32
    file = 'somedocument.doc'
    
    shell32.ShellExecuteA(0,"open",file,0,0,5)
    

    A lot of magic constants. The first zero is the hwnd of the current program. Can be zero. The other two zeros are optional parameters (parameters and directory). 5 == SW_SHOW, it specifies how to execute the app. Read the ShellExecute API docs for more info.

提交回复
热议问题