import matplotlib
import os
import shutil
# 修改matplotlib 配置文件 并复制字体文件
matplotlibPath = matplotlib.matplotlib_fname()
print(f"change {matplotlibPath}")
with open(matplotlibPath, "r") as f:
text = f.readlines()
pass
for i in range(len(text)):
if "#font.family:" in text[i]:
print(f"find location {i}")
text[i] = "font.family: Microsoft YaHei, sans-serif\n"
break
for i in range(i, len(text)):
if "#font.serif:" in text[i]:
print(f"find location {i}")
text[i] = "font.serif: Microsoft YaHei, DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif\n"
pass
break
with open(matplotlibPath, "w+") as f:
for i in range(len(text)):
f.write(text[i])
print("finish")
matplotlibDir = matplotlibPath[:-len("matplotlibrc")]
matplotlibTtfPath = matplotlibDir + "\\fonts\\ttf\\"
files = ["msyh.ttc", "msyhbd.ttc", "msyhl.ttc"]
print(f"copy {files} to {matplotlibTtfPath}")
for i in range(len(files)):
shutil.copyfile(files[i], matplotlibTtfPath + files[i])
print("finish")