找回密码
 立即注册
查看: 403|回复: 2

求批量重命名字体的工具

17

主题

106

回帖

0

VC币

中级会员

Rank: 3Rank: 3

积分
9993
QH7B 发表于 2026-1-11 18:42:22 | 显示全部楼层 |阅读模式
如题,搜索到了这个python脚本:

https://raw.githubusercontent.co ... main/rename_font.py
但是这个脚本好像在处理ttc字体时会报错,所以不知道有没有大佬能改进一下它,输出由“&”连接的名称或者单独的 ttf 文件都可以。

回复

使用道具 举报

4

主题

18

回帖

240

VC币

中级会员

Rank: 3Rank: 3

积分
16522
ktuup6 发表于 2026-1-11 23:44:53 | 显示全部楼层
这样改你看行不行

  1. import traceback

  2. from fontTools.ttLib import TTFont, TTCollection
  3. import re
  4. import os
  5. import argparse

  6. # 默认目标文件夹
  7. folder_path = './'

  8. # 创建解析器
  9. parser = argparse.ArgumentParser(description='命令行参数解析')

  10. # 装有ttf或者otf字体的目标文件夹
  11. parser.add_argument('-d', '--directory', type=str, help='目标文件夹')

  12. # 解析参数
  13. args = parser.parse_args()

  14. if(args.directory!=None):
  15.     folder_path = str(args.directory)
  16. else:
  17.     folder_path = './'
  18.    

  19. # 判断字符串中是否包含中文
  20. def contains_chinese(text):
  21.     return re.search(r'[\u4e00-\u9fff]', text) is not None

  22. # 获取指定.ttf字体文件的内部中文名,如果没有中文名将使用英文名
  23. def get_font_chinese_name(ttfFile):
  24.     if ttfFile.lower().endswith(".ttc"):
  25.         font_collection = TTCollection(ttfFile).fonts
  26.     else:
  27.         font_collection = [TTFont(ttfFile)]
  28.     names = []
  29.     for font in font_collection:
  30.         name_dict = {}  #名称字典, 如果有多个中文名,按优先级序列选择(简体中文>繁体中文,windows>mac),如果没有中文名,则按优先级选择英文名(英语美国>英语英国,windows>mac)
  31.         name_priority = [2052, 3076, 33, 36 ,1028, 19, 1033, 0, 2057]
  32.         for i, font_name in enumerate(font['name'].names):
  33.             # print(i,font_name,font_name.langID,font_name.nameID)
  34.             if font_name.nameID == 4: #包含字体家族详细信息:
  35.                 try:
  36.                     name_dict[font_name.langID] = font_name.toUnicode()
  37.                 except Exception:
  38.                     continue

  39.         # 按优先级序列搜索
  40.         for lang_id in name_priority:
  41.             if lang_id in name_dict.keys():
  42.                 names.append(name_dict[lang_id].replace(" ","-"))
  43.                 break
  44.         else:
  45.             break

  46.     if not names:
  47.         print("错误:",ttfFile,"无法找到中文名或英文名。")
  48.         return -1
  49.     else:
  50.         return '&'.join(names)

  51. #重命名文件
  52. def rename_file(old_name, new_name):
  53.     try:
  54.         os.rename(old_name, new_name)
  55.         print(f"文件 {old_name} 已成功重命名为 {new_name}")
  56.     except FileNotFoundError:
  57.         print(f"文件 {old_name} 不存在,请检查路径是否正确。")

  58. #批量重命名文件
  59. if __name__ == "__main__":
  60.     for root, dirs, files in os.walk(folder_path):
  61.         for filename in files:
  62.             if filename.lower().endswith(".ttf") or filename.lower().endswith(".otf") or filename.lower().endswith(".ttc"):
  63.                 ttfFile = os.path.join(root, filename)
  64.                 _, fileExtension = os.path.splitext(filename)
  65.                 try:
  66.                     c_name = get_font_chinese_name(ttfFile)
  67.                     if c_name==-1: continue
  68.                     newFileName = os.path.join(root,c_name+fileExtension)
  69.                     rename_file(ttfFile,newFileName)
  70.                 except Exception as e:
  71.                     traceback.print_exc()
复制代码


回复

使用道具 举报

17

主题

106

回帖

0

VC币

中级会员

Rank: 3Rank: 3

积分
9993
QH7B  楼主| 发表于 2026-1-12 13:33:05 | 显示全部楼层
ktuup6 发表于 2026-1-11 23:44
这样改你看行不行

可以了!非常感谢大佬帮忙修改!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表