innoai commited on
Commit
ce26bd4
·
1 Parent(s): a443535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -20
app.py CHANGED
@@ -15,26 +15,69 @@ except Exception as e:
15
  # 如果权限设置失败,也无需中断,只要后续能调用即可
16
  print(f"⚠️ 警告:设置执行权限失败:{e}")
17
 
18
- # 1. 定位源目录:仓库根目录(当前工作目录)
19
- src_dir = os.getcwd()
20
-
21
- # 2. 目标目录:用户字体目录(可写)
22
- dst_dir = os.path.expanduser("~/.local/share/fonts")
23
- os.makedirs(dst_dir, exist_ok=True)
24
-
25
- # 3. 复制根目录下的所有 .ttf 和 .otf 文件
26
- for fname in os.listdir(src_dir):
27
- if fname.lower().endswith((".ttf", ".otf")):
28
- shutil.copy(
29
- os.path.join(src_dir, fname),
30
- os.path.join(dst_dir, fname)
31
- )
32
-
33
- # 4. 刷新指定目录的字体缓存,确保新字体可用
34
- subprocess.run(
35
- ["fc-cache", "-f", "-v", dst_dir],
36
- check=True
37
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  def convert_pptx_to_zip(pptx_file):
40
  """
 
15
  # 如果权限设置失败,也无需中断,只要后续能调用即可
16
  print(f"⚠️ 警告:设置执行权限失败:{e}")
17
 
18
+
19
+ def install_fonts_from_repository():
20
+ """
21
+ 将仓库中的 fonts 目录下的所有字体文件复制到 ~/.fonts 目录,并刷新字体缓存。
22
+ """
23
+ try:
24
+ # 获取当前用户的主目录路径
25
+ home_dir = Path.home()
26
+
27
+ # 定义目标字体目录路径:~/.fonts
28
+ fonts_dir = home_dir / ".fonts"
29
+
30
+ # 如果 ~/.fonts 目录不存在,则创建该目录
31
+ if not fonts_dir.exists():
32
+ fonts_dir.mkdir(parents=True)
33
+ print(f"已创建字体目录:{fonts_dir}")
34
+ else:
35
+ print(f"字体目录已存在:{fonts_dir}")
36
+
37
+ # 定义仓库中的 fonts 目录路径
38
+ repo_fonts_dir = Path(__file__).parent / "fonts"
39
+
40
+ # 检查仓库中的 fonts 目录是否存在
41
+ if not repo_fonts_dir.exists():
42
+ print(f"错误:仓库中的 fonts 目录不存在:{repo_fonts_dir}")
43
+ return
44
+
45
+ # 支持的字体文件扩展名
46
+ font_extensions = [".ttf", ".otf", ".ttc"]
47
+
48
+ # 统计已复制的字体文件数量
49
+ copied_count = 0
50
+
51
+ # 遍历 fonts 目录中的所有文件
52
+ for font_file in repo_fonts_dir.iterdir():
53
+ if font_file.suffix.lower() in font_extensions:
54
+ destination = fonts_dir / font_file.name
55
+ try:
56
+ shutil.copy(font_file, destination)
57
+ print(f"已复制字体文件:{font_file.name} 到 {destination}")
58
+ copied_count += 1
59
+ except Exception as e:
60
+ print(f"复制字体文件时出错:{font_file.name},错误信息:{e}")
61
+
62
+ if copied_count == 0:
63
+ print("未找到任何可复制的字体文件。")
64
+ return
65
+
66
+ # 刷新字体缓存(仅在 Linux/Mac 下)
67
+ if not sys.platform.startswith('win'):
68
+ try:
69
+ print("正在刷新字体缓存...")
70
+ subprocess.run(["fc-cache", "-f", "-v"], check=True)
71
+ print("字体缓存刷新完成。")
72
+ except subprocess.CalledProcessError as e:
73
+ print(f"刷新字体缓存时出错:{e}")
74
+ except Exception as e:
75
+ print(f"安装字体时出错:{e}")
76
+
77
+
78
+ # 启动时安装字体
79
+ install_fonts_from_repository()
80
+
81
 
82
  def convert_pptx_to_zip(pptx_file):
83
  """