使用python更改Windows快捷方式.lnk文件的图标 [英] change the icon of a windows shortcut .lnk file using python
问题描述
我要在创建.lnk
文件后更改其图标.到目前为止,这是我的主要代码:
I want to change the icon of a .lnk
file after I have created it. This is my main code so far:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut("shortcut.lnk")
shortcut.Targetpath = "C:\\Users\Benjie\AppData\Local\Programs\Python\Python36\python.exe"
shortcut.save()
这将创建带有python图标的快捷方式,但我想将其更改为其他图标(如果可能),以更改为其他.exe
文件的图标.
我该怎么办?
This creates a shortcut with the python icon, but I want to change it to a different icon, if possible, to the icon of a different .exe
file.
How can I do this?
我最好使用Windows api库之一,但是如果不可能,则外部库也可以工作.
I'd preferably use one of the windows api librarys, but if this is not possible, an external library would work aswell.
谢谢
推荐答案
Ok, after a few hours of researching stuff with the help of this, I managed to find what I was looking for: shortcut.IconLocation
. This sets the icon of a shortcut to an icon from an .exe, .dll .icl or .ico file. For example:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortcut("shortcut.lnk")
shortcut.TargetPath = "C:\\Users\Benjie\AppData\Local\Programs\Python\Python36\python.exe"
shortcut.IconLocation = "C:\path_to_.exe,1"
shortcut.Save()
图标路径是文件的路径,带有逗号和文件中图标的编号.如果创建快捷方式并在其属性中更改其图标,则可以看到该文件的图标,然后浏览该文件.
The icon path is a path to the file, with a comma and the number of the icon in the file. You can see the icons for a file if you create a shortcut and change its icon in its properties, Then browse for the file.
这篇关于使用python更改Windows快捷方式.lnk文件的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!