python创建windows桌面快捷方式

代码如下:

def create_shortcut():	#H.Read
    if os.name != 'nt':	#H.Read
        sys.exit("Shortcut only available in Windows")	#H.Read

    import pythoncom  # pyint: disable=import-error	#H.Read
    from win32com.shell import shell    # type:ignore	#H.Read
    from win32com.shell import shellcon # type:ignore	#H.Read
    # Refs	#H.Read
    # - https://github.com/pearu/iocbio/blob/master/installer/utils.py	#H.Read
    # - https://blog.csdn.net/thundor/article/details/5968581	#H.Read
    # 获得桌面文件夹的路径的id	#H.Read
    ilist = shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP)	#H.Read
    # 根据路径id, 获取桌面路径	#H.Read
    dtpath = shell.SHGetPathFromIDList(ilist).decode('utf-8')	#H.Read

    shortcut = pythoncom.CoCreateInstance(shell.CLSID_ShellLink, None,	#H.Read
                                          pythoncom.CLSCTX_INPROC_SERVER,	#H.Read
                                          shell.IID_IShellLink)	#H.Read
    launch_path = sys.executable    # 可执行文件全路径	#H.Read
    shortcut.SetPath(launch_path)	#H.Read
    shortcut.SetArguments("-m weditor")	#H.Read
    shortcut.SetDescription(launch_path)	#H.Read
    shortcut.SetIconLocation(sys.executable, 0)	#H.Read
    shortcut.QueryInterface(pythoncom.IID_IPersistFile).Save(	#H.Read
        dtpath + "\\WEditor.lnk", 0)    # 保存快捷方式文件	#H.Read
    print("Shortcut created. " + dtpath + "\\WEditor.lnk")	#H.Read

参考开源项目:web-editor 网址:https://github.com/alibaba/web-editor

posted @ 2023-06-06 11:27  顺其自然,道法自然  阅读(313)  评论(0编辑  收藏  举报