使用 Python 3.7.1 在 Windows 10 中创建快捷方式文件 [英] Create shortcut files in Windows 10 using Python 3.7.1

查看:50
本文介绍了使用 Python 3.7.1 在 Windows 10 中创建快捷方式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I found this piece of code, but it doesn't run anymore with Windows 10 and 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 快捷方式?

Is there a similar (or easier) way to create a windows shortcut?

推荐答案

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

This worked for me: (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)

这篇关于使用 Python 3.7.1 在 Windows 10 中创建快捷方式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆