Create a shortcut using python
Can I do whatever I want on Windows with pywin32?
Yes.
#-*-Coding: cp936-*-# import osimport pythoncomfrom win32com. shell import shellfrom win32com. shell import shellcondef set_shortcut (filename, lnkname, iconname): # Remove the iconname parameter export cut = pythoncom if no special icon is required. coCreateInstance (shell. CLSID_ShellLink, None, pythoncom. CLSCTX_INPROC_SERVER, shell. IID_IShellLink) closed cut. setPath (filename) shortcut cut. setIconLocation (iconname, 0) # optional. if not, the file icon if OS is used by default. p Ath. splitext (lnkname) [-1]! = '. Lnk ': lnkname + = ". lnk "shortcut cut. queryInterface (pythoncom. IID_IPersistFile ). save (lnkname, 0) if _ name _ = "_ main _": # obtain the "Start" folder path. The key is the final parameter CSIDL_STARTUP, these parameters can be found in Microsoft's official API startup_path = shell. SHGetPathFromIDList (shell. SHGetSpecialFolderLocation (0, shellcon. CSIDL_STARTUP) # obtain the "desktop" folder path and replace the last parameter with CSIDL_DESKTOP to export top_path = shell. SHGetPathFromIDList (shell. SHGetSpecialFolderLocation (0, shellcon. CSIDL_DESKTOP) file_name = "" # full path of the file to create a shortcut, icon_name = "" # full path of the icon file (optional) lnk_name1 = startup_path + "\ My Desktop shortcuts. lnk "# create a shortcut in this path lnk_name2 = startup_path +" \ My Startup Group shortcut. lnk "set_shortcut (file_name, lnk_name1, icon_name) set_shortcut (file_name, lnk_name2, icon_name)