从 Python 中的快捷方式 (.lnk) 文件中提取图标?



我正在尝试从"开始"菜单文件夹中的所有快捷方式中提取图标。到目前为止,我已经设法浏览了目录树,我只需要一些东西来从每个快捷方式中提取图标。我已经尝试了互联网上建议的一些方法,但我似乎无法使其完全工作。

方法1:通过os.system((使用名为ResourcesExtract的程序从.lnk文件中提取图标。我很快发现这不适用于.lnk文件,仅适用于.exe或.dlls。

import os
os.system(f"resourcesextract.exe /source {shortcut}")

方法 2:使用 ResourcesExtract 从快捷方式的目标中提取图标文件(可以使用 pywin32 库轻松获得(。不幸的是,这仅适用于某些程序,因为一些快捷方式指向没有图标的 .exe。

import os
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
target = shell.CreateShortCut(shortcut).TargetPath
os.system(f"resourcesextract.exe /source {target}")

方法 3:使用 pywin32 获取图标目录。这仅适用于我需要它的 120 个快捷方式中的大约 300 个。

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
icon, status = str(shell.CreateShortCut(shortcut).IconLocation).split(",")

我还遇到了一种使用 .NET 框架的方法,但我不知道如何与 python 进行交互,或者它是否可以工作。


有没有人知道一种从 Python 中.lnk文件中提取图标的方法适用于所有快捷方式?

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
ShortCut = shell.CreateShortCut('example.lnk')
icon_location = ShortCut.IconLocation

它对我有用。

最新更新