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

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

问题描述

是否有一种简单的方法可以在 Python 上的 Windows 7 中创建快捷方式?我在网上查了一下,但似乎没有那么简单.

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:

hi = open("ShortcutFile.lnk", "w")
hi.close()

创建快捷方式文件.但是我将如何编辑它所连接的链接.就像它会打开的文件?有人能给我一个正确的方向吗?

Which creates a shortcut file. But how would I edit the link it is connected to. Like the file it would open? Can someone give me a push in the right direction?

推荐答案

旧的,但我仍然想发布一个答案来帮助可能有相同问题并需要代码示例的任何人.

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.

首先,使用 pip install pywin32 下载 pywin32 或下载 sourceforge二进制文件或 pywin32 wheel 文件和 pip install.

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()

当我有一个带有调试控制台的 GUI 时,我使用了 WindowStyle,我不希望控制台一直弹出.我还没有用没有安慰过的程序试过.

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.

希望这会有所帮助!

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

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