Python给应用程序创建桌面快捷方式 | 马犇-技术博客

Python给应用程序创建桌面快捷方式

来源:本站原创 Python, 原创 超过6,497 views围观 2条评论

参考了一下网上的办法,简化了一下。

import os
import pythoncom
from win32com.shell import shell    
from win32com.shell import shellcon 

def createDesktopLnk(filename,lnkname):
    shortcut = pythoncom.CoCreateInstance(    
        shell.CLSID_ShellLink, None,    
        pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)    
    shortcut.SetPath(filename)    
    if os.path.splitext(lnkname)[-1] != '.lnk':    
        lnkname += ".lnk"
    # get desktop path
    desktopPath = shell.SHGetPathFromIDList(shell.SHGetSpecialFolderLocation(0,shellcon.CSIDL_DESKTOP))
    lnkname = os.path.join(desktopPath,lnkname)
    shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(lnkname,0)   

if __name__ == '__main__':
    createDesktopLnk(u"C:\Python27\python.exe","MyPython")