微信好友批量发送定制祝福

概述:微信好友批量发送定制祝福,生成定制贺卡并结合itchat批量发送贺卡和祝福语

代码分享(2019)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from PIL import Image, ImageDraw, ImageFont
import glob
import itchat # pip install itchat

# 微信自动登录
itchat.auto_login(hotReload=True)

# 写入祝福语
def generatePic(name):
pattern = 'news.jpg'
setFont = ImageFont.truetype('simkai.ttf', 35)
fillColor = "red"
for img in glob.glob(pattern):
image = Image.open(img)
if image.mode == "P":
image = image.convert('RGB')
draw = ImageDraw.Draw(image)
draw.text((200, 370), u'祝愿'+name+':',font=setFont,fill=fillColor)
mywidth,myheight,lineheight = 235,460,70
draw.text((mywidth, myheight), u'岁华新至,除夕乐融,', font=setFont, fill=fillColor)
draw.text((mywidth, myheight+lineheight), u'喜乐平安,诸事顺遂,', font=setFont, fill=fillColor)
draw.text((mywidth, myheight+lineheight*2), u'吉吉利利,百事如意,', font=setFont, fill=fillColor)
draw.text((mywidth, myheight+lineheight*3), u'家兴百和,万福骈臻。', font=setFont, fill=fillColor)
draw.text((mywidth, myheight+lineheight*5), u'署名', font=setFont, fill=fillColor)
image.save("mycard2.jpg")
return "mycard2.jpg"

# 获取好友信息
friends = itchat.get_friends(update=True)[:]

# 查看所有朋友的全部详细信息
# print(friends)

# 获取备注名字
# for i in friends:
# print(i.RemarkName,'\t')

# 指定发送对象(备注名)
RemarkNames = ['a1文杰']

# 发送祝福语
for person in RemarkNames:
users=itchat.search_friends(person)
userName= users[0]['UserName']
picfile = generatePic(person[-2:])

# 发送文字
try:
itchat.send('Hey'+person[-2:]+'~ Happy New Year!', toUserName=userName)
print("发送给" + person + "的祝福语文字 成功")
except:
print("发送给"+person+"的祝福语文字 失败")

# 发送图片
try:
itchat.send_image(picfile,toUserName=userName)
print("发送给"+person+"的祝福语图片 成功")
except:
print("发送给"+person+"的祝福语图片 失败")

效果图(2019)


代码分享(2020)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from PIL import Image, ImageDraw, ImageFont
import glob
import itchat
import random
import time
# 微信自动登录
itchat.auto_login(hotReload=True)

# 文字祝福语
blessing_word_list = [
'牛年快乐!命途风霜尽,乾坤气象和,历添新岁月,福满旧山河。 [福][跳跳][庆祝]',
'牛年快乐!所念皆如意,未来皆可期,仍有阳光满路,温暖如初。 [福][跳跳][庆祝]',
'牛年快乐!喜庆期盼,不负爱意,慢品人间烟火,闲看人间岁月长。 [福][跳跳][庆祝]',
'牛年快乐!多喜乐,长安宁,祈沐暖阳,盼所愿成所得,所遇为知己。 [福][跳跳][庆祝]',
'牛年快乐!喜乐平安,诸事顺遂,岁华新至,除夕乐融,家兴百和,万福骈臻。 [福][跳跳][庆祝]',
'牛年快乐!劝君今夕不须眠,且满满,泛觥船,大家沈醉对芳筵,愿新年,胜旧年。——《双雁儿(除夕)》 [福][跳跳][庆祝]',
'牛年快乐!爆竹声中一岁除,春风送暖入屠苏。千门万户曈曈日,总把新桃换旧符。——王安石《元日》 [福][跳跳][庆祝]'
]


# 自动发送新年快乐消息
def send_happy_new_year_msg():
friends = itchat.get_friends(update=True)[:]
pattern = 'card.jpg'
font_type = ImageFont.truetype('new_year.ttf', 88)
font_color = 'white'

for friend in friends:
if len(friend.RemarkName) != 0:
remark_name = friend.RemarkName

if '_' in remark_name:
real_name = str(remark_name).split('_')[-1]
to_word = f'祝 {real_name} {random.choice(blessing_word_list)}'
print(to_word)

# 生成祝福文字
users = itchat.search_friends(remark_name)
userName = users[0]['UserName']

# 发送祝福文字
itchat.send(to_word, toUserName=userName)
itchat.send('[烟花]', toUserName=userName)
time.sleep(5)
# 生成祝福图片
for img in glob.glob(pattern):
image = Image.open(img)
init_pic_word_loc = 50
if image.mode == "P":
image = image.convert('RGB')
draw = ImageDraw.Draw(image)
words = '新的一年丶祝愿' + str(real_name)

for word in words:
draw.text((125, init_pic_word_loc), u' ' + word, font=font_type, fill=font_color)
init_pic_word_loc += 66
image.save('images\\' + str(real_name) + ".jpg")

# 发生祝福图片
itchat.send_image('images\\' + str(real_name) + ".jpg", toUserName=userName)
time.sleep(3)


if __name__ == '__main__':
send_happy_new_year_msg()

效果图(2020)