import jieba
import wordcloud
#上面两个包需要自己手动安装
import imageio
with open('sample.txt',encoding='utf-8') as f:
data = f.read() #读取文本
ls=jieba.lcut(data) #分词
txt=" ".join(ls)
w=wordcloud.WordCloud(width=2000,
height=1400,
font_path="msyh.ttc") #设置图片宽度、高度、字体
w.generate(txt)
w.to_file(r'D:\1.png') #运行并输出
结果如下