Python 生成词云:一篇文章生成漂亮词云图
from wordcloud import WordCloud
import jieba
def make_wordcloud(text_file, output='wordcloud.png'):
with open(text_file, 'r', encoding='utf-8') as f:
text = f.read()
words = ' '.join(jieba.cut(text))
wc = WordCloud(
font_path='C:/Windows/Fonts/msyh.ttc',
width=1200, height=800,
background_color='white',
max_words=200
).generate(words)
wc.to_file(output)
print(f"已生成:{output}")
make_wordcloud('文章.txt')
import jieba
def make_wordcloud(text_file, output='wordcloud.png'):
with open(text_file, 'r', encoding='utf-8') as f:
text = f.read()
words = ' '.join(jieba.cut(text))
wc = WordCloud(
font_path='C:/Windows/Fonts/msyh.ttc',
width=1200, height=800,
background_color='white',
max_words=200
).generate(words)
wc.to_file(output)
print(f"已生成:{output}")
make_wordcloud('文章.txt')

更新时间:2026-09-14 13:42:48