怎么添加python桌面快捷方式图标_使用python更改Windows快捷方式.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()

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

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值