Create a Heart‑Shaped WeChat Word Cloud with Python: Step‑by‑Step Guide
This article explains how to build a Python tool that monitors real‑time WeChat chats, uses wxpy to capture messages, applies jieba for Chinese word segmentation, and generates a heart‑shaped word cloud with the wordcloud library, complete with code examples and setup instructions.
On the occasion of Qixi (Chinese Valentine’s Day), a Python utility is presented that captures real‑time WeChat conversations and visualizes them as a heart‑shaped word cloud.
Real‑time WeChat Chat Monitoring (wxpy)
Run wx_word.exe to display a QR code for login. After scanning, input the WeChat friend’s remark name. The program then listens to the friend’s messages and saves them locally, ensuring no data leakage.
Generating the Word Cloud
When you want to create the cloud, execute the parse.py script (requires Python 3+). The script processes the saved chat records and produces a heart‑shaped word cloud.
Environment Requirements
Windows computer
Python 3 or higher installed
Necessary Python libraries (wxpy, jieba, wordcloud, matplotlib, etc.)
wxpy – Capture Real‑time Messages
# Get friend chat
@bot.register(my_friend)
def print_others(msg):
# Output the captured message
if msg.text:
f = open('data.txt', 'a')
f.write(msg.text + '
')
f.close()
embed()jieba – Chinese Word Segmentation
# encoding=utf-8
import jieba
# Full mode example
seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode: ", "/ ".join(seg_list))
# Precise mode example
seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print(", ".join(seg_list))
# Search engine mode example
seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造")
print(", ".join(seg_list))wordcloud – Generate the Cloud
# wordcloud.WordCloud parameters example
from wordcloud import WordCloud
wc = WordCloud(font_path=None, width=400, height=200, margin=2,
ranks_only=None, prefer_horizontal=0.9, mask=None,
scale=1, color_func=None, max_words=200,
min_font_size=4, stopwords=None, random_state=None,
background_color='black', max_font_size=None,
font_step=1, mode='RGB', relative_scaling=0.5,
regexp=None, collocations=True, colormap=None,
normalize_plurals=True)Heart‑Shaped Word Cloud Creation
First, load a heart‑shaped mask image:
# Generate the cloud
pic = imread('./xin.jpg')
wc = WordCloud(mask=pic, font_path='./simhei.ttf', width=500, height=300,
background_color='white')
wc.generate(word)
wc.to_file(os.path.join(d, "wechat_cloud.png"))
plt.imshow(wc)
plt.axis('off')
plt.show()The resulting image shows a personalized word cloud shaped like a heart, visualizing the most frequent words from the captured chat history.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
