Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import os
import re
import sys
import shutil
from PIL import ImageFont, ImageDraw, Image
# import fonttools
# 检查 Python 版本是否为 3.x
if sys.version_info[0] < 3:
print("此脚本需要 Python 3。")
sys.exit(1)
# 定义字体文件路径
font_folder_path = "temp"
font__folder_path = font_folder_path.replace("\\", "/")
# # 删除字体文件的隐藏属性
# def unhide_files(folder_path):
# for root, dirs, files in os.walk(font_folder_path):
# for file in files:
# file_path = os.path.join(root, file)
# os.chmod(file_path, os.stat(file_path).st_mode | 0o777)
# unhide_files(font_folder_path)
# 查找所有文件(不考虑扩展名)
font_files = [f for f in os.listdir(font_folder_path) if os.path.isfile(os.path.join(font_folder_path, f))]
# 遍历每个文件
for font_file in font_files:
font_path = os.path.join(font_folder_path, font_file)
font_path = font_path.replace("\\", "/")
# 尝试加载字体
try:
font = ImageFont.truetype(font_path)
# font = fonttools.ttLib.TTFont(font_fp=font_path)
except Exception as e:
print(f"无法加载字体文件 '{font_file}': {e}")
continue
# 读取字体名称
font_name = font.getname()
# 提取字体名称
# font_name = font["name"].getName()
# # 提取字体全名
# font_fullname = font["name"].getFullName()
# # 提取字体系列
# font_family = font["name"].getFamily()
# # 提取特定语言下的显示名称(例如中文)
# font_display_name = font["name"].getStrings()[0]
# print(f"字体名称:{font_name}")
# print(f"字体全名:{font_fullname}")
# print(f"字体系列:{font_family}")
# print(f"显示名称(中文):{font_display_name}")
font_name_str = str(font_name)
font_name = "-".join(font_name_str.split(", ")).replace("'","").replace("(","").replace(")","")
print(font_name)
# print(font.container[0])
# if not re.search(r"\.ttf$|\.otf$", font_file):
# # 如果没有,则添加扩展名
# if font.container[0].startswith(b'OTTO'):
# new_font_file = font_name + ".otf"
# else:
# new_font_file = font_name + ".ttf"
# else:
# new_font_file = font_name
# new_font_file = font_name + ".ttf"
if not re.search(r"\.ttf$|\.otf$", font_file):
# 如果没有,则添加扩展名
new_font_file = font_name + ".ttf"
# 构建新的文件路径
new_font_path = os.path.join("font\\", new_font_file)
# new_font_path = os.path.join(font_folder_path, font_name)
new_font_path = new_font_path.replace("\\", "/")
# 检查新文件名是否已存在
if os.path.exists(new_font_path):
print(f"新文件名 '{new_font_file}' 已存在。跳过 '{font_file}'。")
continue
# 重命名文件
try:
shutil.copy(font_path, new_font_path)
print(f"已重命名 '{font_file}' 为 '{new_font_file}'。")
except Exception as e:
print(f"无法重命名 '{font_file}':{e}")