change the icon of a windows shortcut .lnk file using python

ぃ、小莉子 提交于 2019-12-06 15:31:01

问题


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

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?

I'd preferably use one of the windows api librarys, but if this is not possible, an external library would work aswell.

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/47369501/change-the-icon-of-a-windows-shortcut-lnk-file-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!