Create shortcut files in Windows 7 using Python

前端 未结 5 1127
天命终不由人
天命终不由人 2021-02-08 18:07

Is there a simple way to create shortcuts in Windows 7 on Python? I looked it up online but they did not seem that simple.

I have tried using this simple method:

<
5条回答
  •  醉酒成梦
    2021-02-08 18:42

    Old, but I would still like to post an answer to help out anyone who might have the same question and in need of a code example.

    First, download pywin32 with pip install pywin32 or download the sourceforge binaries or the pywin32 wheel file and pip install.

    import win32com.client
    import pythoncom
    import os
    # pythoncom.CoInitialize() # remove the '#' at the beginning of the line if running in a thread.
    desktop = r'C:\Users\Public\Desktop' # path to where you want to put the .lnk
    path = os.path.join(desktop, 'NameOfShortcut.lnk')
    target = r'C:\path\to\target\file.exe'
    icon = r'C:\path\to\icon\resource.ico' # not needed, but nice
    
    shell = win32com.client.Dispatch("WScript.Shell")
    shortcut = shell.CreateShortCut(path)
    shortcut.Targetpath = target
    shortcut.IconLocation = icon
    shortcut.WindowStyle = 7 # 7 - Minimized, 3 - Maximized, 1 - Normal
    shortcut.save()
    

    I have used the WindowStyle when I have a GUI with a debug console, and I don't want the console to pop up all the time. I haven't tried it with a program that isn't consoled.

    Hope this helps!

提交回复
热议问题