2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
pip install python-docx wordcloud matplotlib
import docx
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# 读取Word文件内容
def read_word_file(file_path):
doc = docx.Document(file_path)
full_text = []
for para in doc.paragraphs:
full_text.append(para.text)
return 'n'.join(full_text)
# 生成词云图
def generate_wordcloud(text):
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
# 显示词云图
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.show()
# 主函数
def main():
file_path = 'your_word_file.docx' # 替换为你的Word文件路径
text = read_word_file(file_path)
generate_wordcloud(text)
if __name__ == "__main__":
main()
Avis:
S'il y a des caractères chinois tronqués, vous pouvez les modifier des manières suivantes :
Ajouter une police
wordcloud = WordCloud(width=800, height=400, background_color='white', font_path='simhei.ttf').generate(text)
L'effet après modification:
Explication détaillée
Bibliothèque d'installation :
Lisez le contenu du fichier Word :
Générer un diagramme de nuage de mots :
Précautions
Grâce aux étapes ci-dessus, vous pouvez facilement lire des fichiers Word et générer de superbes diagrammes de nuages de mots.