CoolFace
Apppublic

v2431/FormulaShowcase

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
fonts.py58 linesDownload Raw Back to root
1import os
2import shutil
3from pathlib import Path
4
5def copy_chinese_fonts():
6    """
7    创建fonts文件夹并复制系统中的中文字体文件到项目中
8    """
9    # 创建项目的fonts文件夹
10    project_font_dir = Path(__file__).with_name('fonts')
11    project_font_dir.mkdir(exist_ok=True)
12    print(f"Created fonts directory: {project_font_dir}")
13    
14    # 常见的中文字体文件路径
15    font_paths = [
16        # 微软雅黑
17        "C:/Windows/Fonts/msyh.ttc",
18        "C:/Windows/Fonts/msyhbd.ttc",
19        "C:/Windows/Fonts/msyhl.ttc",
20        # 宋体
21        "C:/Windows/Fonts/simsun.ttc",
22        "C:/Windows/Fonts/simsunb.ttf",
23        # 黑体
24        "C:/Windows/Fonts/simhei.ttf",
25        # 楷体
26        "C:/Windows/Fonts/simkai.ttf",
27        # 仿宋
28        "C:/Windows/Fonts/simfang.ttf",
29    ]
30    
31    copied_fonts = []
32    
33    for font_path in font_paths:
34        font_file = Path(font_path)
35        if font_file.exists():
36            try:
37                # 复制到项目的fonts文件夹中
38                target_path = project_font_dir / font_file.name
39                if not target_path.exists():
40                    shutil.copy2(str(font_file), str(target_path))
41                    print(f"Successfully copied: {font_file.name}")
42                    copied_fonts.append(font_file.name)
43                else:
44                    print(f"Font already exists: {font_file.name}")
45                    copied_fonts.append(font_file.name)
46            except Exception as e:
47                print(f"Failed to copy {font_file.name}: {e}")
48    
49    if copied_fonts:
50        print(f"\nSuccessfully copied {len(copied_fonts)} font files to fonts directory")
51    else:
52        print("\nWarning: No Chinese font files found on system")
53        print("Please manually copy a Chinese font file (e.g., msyh.ttc) to the fonts folder")
54
55    return copied_fonts
56
57if __name__ == "__main__":
58    copy_chinese_fonts()