Python 微信 教师节贺卡 自动发送

想要在教师节给徐老师发送一份特别的电子贺卡吗?使用 Python 的 itchat 库,您可以轻松实现微信自动发送贺卡的功能。

以下代码示例展示了如何创建电子贺卡并通过微信发送给备注名为'徐老师' 的好友。

import itchat
from PIL import Image, ImageDraw, ImageFont

# 创建贺卡图片
def create_card(teacher_name):
    # 贺卡背景图片
    bg_img = Image.open('background.jpg')
    # 在贺卡上添加文字
    draw = ImageDraw.Draw(bg_img)
    font = ImageFont.truetype('font.ttf', size=50)
    text = '亲爱的{}老师,祝您教师节快乐!'.format(teacher_name)
    text_width, text_height = draw.textsize(text, font)
    draw.text(((bg_img.width - text_width) // 2, (bg_img.height - text_height) // 2), text,
              font=font, fill=(255, 255, 255))
    # 保存贺卡图片
    card_path = 'card.jpg'
    bg_img.save(card_path)
    return card_path

# 发送贺卡
def send_card(teacher_name):
    # 登录微信
    itchat.auto_login(hotReload=True)
    # 创建贺卡
    card_path = create_card(teacher_name)
    # 获取好友列表
    friends = itchat.get_friends(update=True)
    # 查找好友
    for friend in friends:
        if friend['RemarkName'] == '徐老师':
            # 发送贺卡
            itchat.send_image(card_path, friend['UserName'])
            break
    # 登出微信
    itchat.logout()

# 主函数
if __name__ == '__main__':
    teacher_name = '徐老师'
    send_card(teacher_name)

使用步骤:

  1. 安装依赖库:
    pip install itchat pillow
    
  2. 准备素材:
    • 一张背景图片 (background.jpg)
    • 一个字体文件 (font.ttf) 将它们放在代码所在目录下。
  3. 运行代码: 运行代码后,会弹出一个二维码,使用手机微信扫描二维码进行登录。代码会自动在好友列表中查找备注为 '徐老师' 的好友,并发送贺卡。

注意事项:

  • 由于微信的限制,该代码只能在电脑上运行,无法直接在手机上运行。
  • 您需要先将微信账号登录到电脑上,才能使用该代码发送贺卡。
  • 请确保您已添加 '徐老师' 为好友,并且已设置备注名为 '徐老师'。

使用该代码,您就可以轻松快捷地给徐老师发送一份充满心意的教师节贺卡!

Python 微信 教师节贺卡 自动发送

原文地址: https://www.cveoy.top/t/topic/npUs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录