def extract_file_icon(path): largeicons, smallicons = win32gui.ExtractIconEx(path, 0) fIcon, xHotspot, yHotspot, hbmMask, hbmColor = win32gui.GetIconInfo(largeicons[0]) pixmap = QPixmap.fromWinHBITMAP(hbmColor, QPixmap.PremultipliedAlpha) icon = QIcon(pixmap) hbmColor.close() hbmMask.close() for hIcon in largeicons + smallicons: win32gui.DestroyIcon(hIcon) return icon
def get_file_icon_by_shell(path): path = translate_path(path) flags, (hIcon, iIcon, dwAttributes, displayName, typeName) = shell.SHGetFileInfo( path, 0, shellcon.SHGFI_ICON|shellcon.SHGFI_SMALLICON|shellcon.SHGFI_SYSICONINDEX|shellcon.SHGFI_TYPENAME|shellcon.SHGFI_USEFILEATTRIBUTES, ) if hIcon == win32con.NULL: return None pixmap = QPixmap.fromWinHICON(hIcon) win32gui.DestroyIcon(hIcon) icon = QIcon(pixmap) return icon