python - 获取文件的关联文件类型图标

标签 python file icons file-type

我想要什么

我正在尝试编写一个函数,它接受一个文件名并返回与我的系统(Windows 7)上文件的文件类型关联的应用程序的图标。

我尝试过的

我看过 this question ,但答案并未提供我需要的详细信息。我对 ctypes 模块不是很熟悉,我发现 VC++ 函数的文档很难理解。

我还看到了this question ,但我陷入了第一个障碍。 当我尝试时:

import _winreg
_winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Software\Microsoft\CurrentVersion\Explorer\FileExts')

它引发了一个WindowsError: [Error 2] The system cannot find the file specified

即使我这样做

_winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Software\Microsoft')

它返回一个 PyHKEY 对象,我尝试对其执行的任何“键”操作都会引发一个 TypeError: The object is not a PyHKEY object

最佳答案

我找到了答案 here

链接中的代码是:

import win32ui
import win32gui
import win32con
import win32api
import cStringIO
import Image

tempDirectory = os.getenv("temp")
ico_x = win32api.GetSystemMetrics(win32con.SM_CXICON)

dst = cStringIO.StringIO()

large, small = win32gui.ExtractIconEx(path,0)
win32gui.DestroyIcon(small[0])

#creating a destination memory DC
hdc = win32ui.CreateDCFromHandle( win32gui.GetDC(0) )
hbmp = win32ui.CreateBitmap()
hbmp.CreateCompatibleBitmap(hdc, ico_x, ico_x)
hdc = hdc.CreateCompatibleDC()

hdc.SelectObject( hbmp )

#draw a icon in it
hdc.DrawIcon( (0,0), large[0] )
win32gui.DestroyIcon(large[0])

#convert picture
hbmp.SaveBitmapFile( hdc, tempDirectory + "\Icontemp.bmp")

im = Image.open(tempDirectory + "\Icontemp.bmp")
im.save(dst, "JPEG")

dst.seek(0)

os.remove(tempDirectory + "\Icontemp.bmp")    
return dst.read()

关于python - 获取文件的关联文件类型图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25511706/

相关文章:

c# - 如何在搜索中排除 protected 操作系统文件

google-chrome-extension - Chrome扩展程序图标更改问题

java - Netbeans 中的 JFrame 图标

python - MultiplicativeLR 和 LambdaLR 的区别

python - SQLAlchemy 条件过滤中的抽象

python - Django Hive 连接

python - Python 中的 3D 几何相交

Linux:inode 和 file_inode(file) 的区别?

java - unicode 格式的日语文本

html - 将图标放在表单中的输入元素中(不是作为背景图像!)