【问题标题】:Create shortcut files in Windows 10 using Python 3.7.1使用 Python 3.7.1 在 Windows 10 中创建快捷方式文件
【发布时间】:2018-11-12 19:24:08
【问题描述】:

我找到了这段代码,但它不再在 Windows 10 和 Python 3.7.1 上运行:

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\XXXXX\Desktop' # path to where you want to put the .lnk
path = os.path.join(desktop, 'NameOfShortcut.lnk')
target = r'C:\Users\XXXXX\Desktop\muell\picture.gif'
icon = r'C:\Users\XXXXX\Desktop\muell\icons8-link-512.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()

是否有类似(或更简单)的方法来创建 Windows 快捷方式?

【问题讨论】:

  • 代码怎么跑不了?这只是Python。如果您安装了 Python,它应该仍然可以运行。
  • 是的,但没有。
  • 那么“不运行”是什么意思呢?什么都没有发生?
  • 我自己发现了问题:目标错了。这是正确的行:target = r'C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.EXE'

标签: python python-3.x windows shortcut


【解决方案1】:

这对我有用:(Win 10,python 2)

http://timgolden.me.uk/python/win32_how_do_i/create-a-shortcut.html

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

shortcut = pythoncom.CoCreateInstance (
  shell.CLSID_ShellLink,
  None,
  pythoncom.CLSCTX_INPROC_SERVER,
  shell.IID_IShellLink
)
shortcut.SetPath (sys.executable)
shortcut.SetDescription ("Python %s" % sys.version)
shortcut.SetIconLocation (sys.executable, 0)

desktop_path = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
persist_file = shortcut.QueryInterface (pythoncom.IID_IPersistFile)
persist_file.Save (os.path.join (desktop_path, "python.lnk"), 0)

【讨论】:

  • 非常感谢。您能否解释一下,我如何使用此代码构建例如 excel.exe 或 C:\Users\XXXXX\Desktop\muell\script.py 的快捷方式?
【解决方案2】:

对于网址:

import win32com.client

from os.path import join as join_paths

ALL_USERS_DESKTOP = r'C:\Users\Public\Desktop'



def create_shortcut(name, link, destination=None):
    """create_shortcut

        Create shortcut
    :param name: shortcut's name
    :type name: str
    :param link: shortcut's link
    :type link: str
    :param destination: directory where to deploy the shortcut
    :type destination: str
    :return: process result
    :rtype: bool
    """
    print('Deploying shortcut {}...'.format(name))
    if not destination:
        destination = ALL_USERS_DESKTOP

    if not name.lower().endswith('.url'):
        name = '{}.url'.format(name)

    path = join_paths(destination, name)
    print('\tDeploying shortcut at: {}.'.format(path))

    try:
        ws = win32com.client.Dispatch("wscript.shell")
        shortcut = ws.CreateShortCut(path)
        shortcut.TargetPath = link
        shortcut.Save()
    except Exception as exception:
        error = 'Failed to deploy shortcut! {}\nArgs: {}, {}, {}'.format(exception, name, link, destination)
        print(error)
        return False

    return True

【讨论】:

    【解决方案3】:

    取自https://www.codespeedy.com/create-the-shortcut-of-any-file-in-windows-using-python/的代码的用户友好版本

    import win32com
    from pathlib import Path
    
    def make_shortcut(source, dest_dir, dest_name=None, verbose=False):
        """Make shortcut of `source` path to file in `dest_dir` target folder.
        If `dest_name` is None, will use `source`'s filename.
        """
        # process user input
        if dest_name is None:
            dest_name = Path(source).name
        dest_path = str(Path(dest_dir, dest_name)) + '.lnk'
        
        # make shortcut
        shell = win32com.client.Dispatch("WScript.Shell")
        shortcut = shell.CreateShortCut(dest_path)
        shortcut.IconLocation = source
        shortcut.Targetpath = source
        shortcut.save()
        
        # print status
        if verbose:
            print("{}\n-->\n{}".format(source, dest_path))
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      相关资源
      最近更新 更多