# Author: Alex.Zhang from wordcloud Import wordcloud with Open ( " 2.txt " , encoding = " UTF-8 " ) AS File: # 1. read text text = File.read () # 2. Set word cloud background color, width and height, the number of words wordcloud = wordcloud (font_path = " C: /Windows/Fonts/simfang.ttf " , BACKGROUND_COLOR = " Black " , width = 600 , height = 300, MAX_WORDS = 50 ) .generate (text) # 3. generate Image image =wordcloud.to_image() #4.显示图片 image.show() # from wordcloud import WordCloud # import jieba # #1.将字符串切分为单个字符 # def chinese_jieba(text): # wordlist_jieba=jieba.cut(text) # space_wordlist=''.join(wordlist_jieba) # return space_wordlist # with open("test.txt" ,encoding="utf-8")as file: # text=file.read() # text=chinese_jieba(text) # wordcloud = WordCloud(font_path="C:/Windows/Fonts/simfang.ttf", # background_color="black", width=600, # height=300, max_words=50,min_font_size=8).generate(text) # image=wordcloud.to_image() # image.show()