欢迎访问宙启技术站
智能推送

在python中使用nametofont()函数获取随机的中文字体名称标题

发布时间:2024-01-13 16:34:14

import matplotlib.font_manager as fm

import random

# 获取中文字体名称

def get_random_chinese_font():

    # 获取系统中所有中文字体

    font_list = fm.findfont(fm.FontProperties(family='SimHei')).split('

')

    # 随机选择一个中文字体

    font_name = random.choice(font_list).split('/')[-1].split('.')[0]

    # 返回中文字体名称

    return font_name

# 使用例子

if __name__ == '__main__':

    # 获取1000个随机中文字体名称

    chinese_fonts = []

    for _ in range(1000):

        chinese_font = get_random_chinese_font()

        chinese_fonts.append(chinese_font)

    

    # 打印结果

    for chinese_font in chinese_fonts:

        print(chinese_font)