|
|
@ -1,40 +0,0 @@
|
|
|
|
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)
|
|
|
|
|