You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.5 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
import matplotlib.pyplot as plt
import pandas as pd
from wordcloud import WordCloud
def generate_wordcloud_from_excel(file_path):
# 1、读取相应文件
df = pd.read_excel(file_path)
# 2、确认是否包含'词汇'和'频率'两列
if '词汇' not in df.columns or '频率' not in df.columns:
print("excel文件中缺少 '词汇''频率' 列。")
return
# 3、将词汇和频率转换为字典
content = dict(zip(df['词汇'], df['频率']))
# 4、生成词云图
wc = WordCloud(font_path='simhei.ttf', background_color='white', width=1371, height=771) # 生成参数
wc.generate_from_frequencies(content) # WordCloud库中的generate_from_frequencies方法用于根据提供的词频数据生成词云。这个方法需要一个字典作为输入其中键是单词值是对应的词频。
# 5、保存词云图到指定位置
output_folder = 'output' # 指定输出文件夹
if not os.path.exists(output_folder): # 如果文件夹不存在就创建它
os.makedirs(output_folder)
output_path = os.path.join(output_folder, '词云图.png') # 生成完整的文件路径
wc.to_file(output_path)
# 6、显示词云图
plt.imshow(wc, interpolation='bilinear')
plt.axis('off') # 不显示坐标轴
plt.show()
if __name__ == '__main__':
file_path = 'output\\高频词.xlsx' # 可替换为任意excel文件路径
generate_wordcloud_from_excel(file_path)