Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用Python 3.7.1在Windows 10中创建快捷方式文件_Python 3.x_Windows_Shortcut - Fatal编程技术网

Python 3.x 使用Python 3.7.1在Windows 10中创建快捷方式文件

Python 3.x 使用Python 3.7.1在Windows 10中创建快捷方式文件,python-3.x,windows,shortcut,Python 3.x,Windows,Shortcut,我找到了这段代码,但它不再在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

我找到了这段代码,但它不再在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快捷方式?

这对我来说很有效:(Win 10,python 2)

对于URL:

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

为什么代码不运行?这只是Python。如果您安装了Python,它应该仍然运行。是的,但是它没有运行。那么“不运行”是什么意思呢?什么都没发生?我自己发现了问题:目标错了。这是正确的一行:target=r'C:\ProgramFiles(x86)\Microsoft Office\Office16\EXCEL.EXE'非常感谢。请您解释一下,我如何使用此代码创建excel.exe或C:\Users\XXXXX\Desktop\muell\script.py的快捷方式?
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