Python批量自定义文件夹图标

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 12:54   825   0

为了使用方便,很多文件夹存放了很多照片,通过文件名很难记住相关文件夹内容,因此利用python批量修改文件夹图标为该文件夹内第一张照片,方便记忆和管理。

处理前:

处理后:

前期准备:

  1. python运行环境及基本语法,安装第三方库
  2. 下载Quick_Any2Ico【下载链接】并与脚本放在同一目录

代码主要分为三块:

  1. 对目录进行遍历,获取子文件夹下的图片文件并生成图标文件 .ico。
  2. 生成并编辑子文件desktop.ini,添加系统属性
  3. 对子文件夹设置只读/系统属性

使用:

  1. 双击运行.py
  2. 输入目标路径
import os,subprocess,codecs
import win32con,win32api
 
 
ini_str = '''
[.ShellClassInfo]\r\n
IconResource=icon.ico,0\r\n
[ViewState]\r\n
Mode=\r\n
Vid=\r\n
FolderType=Pictures\r\n
'''
Any2Ico_path = 'Quick_Any2Ico.exe'    
ext = ["jpg", "jpeg", "png", "gif","icns", "ico"]
 
while True:
 root=input('请输入目录(q/Q=Quit):')
 if root.upper()=='Q':
  break
 root=root.strip('"').strip("'")
 print('--->',root)
 
 for parent,dirnames,filenames in os.walk(root):
  if not dirnames:
   print(parent)   
   first = min(p for p in os.listdir(parent) if p.split(".")[-1].lower() in ext)
   cmd = '"{0}" "-img={1}\{2}" "-icon={1}\icon.ico"'.format(Any2Ico_path,parent,first)
   subprocess.run(cmd)
   win32api.SetFileAttributes('{0}/icon.ico'.format(parent), win32con.FILE_ATTRIBUTE_HIDDEN)
   
   desktop_ini = '{0}/desktop.ini'.format(parent)
   if os.path.exists(desktop_ini):
    os.remove(desktop_ini)
   f=codecs.open(desktop_ini,'w','utf-8')
   f.write(ini_str)
   f.close()
   win32api.SetFileAttributes(desktop_ini, win32con.FILE_ATTRIBUTE_HIDDEN + win32con.FILE_ATTRIBUTE_SYSTEM)
   
   win32api.SetFileAttributes(parent, win32con.FILE_ATTRIBUTE_READONLY)

参考资料:

https://docs.microsoft.com/en-us/windows/desktop/shell/how-to-customize-folders-with-desktop-ini

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP