【案例20220328】通过pyautogui给微信指定用户发消息

功能1:pyautogui查找微信图标后登陆
功能2:找到指定用户(如果不指定,默认发给自己)
功能3:从语录中摘录行内容,通过pyperclip复制到剪贴板后发送
功能4:发送图片

#coding=utf-8
#功能:微信发消息
#时间:20220328

import time
import pyautogui
import pyperclip #模拟剪切板,读取文件然后放在剪切板中
import os
import random

def mapping_img(img,click):
    box_location = pyautogui.locateOnScreen(img)
    center = pyautogui.center(box_location)
    if click == 'double':
        pyautogui.doubleClick(center)
    else:
        pyautogui.leftClick(center)
    time.sleep(1)


def chat_user(user): #消息发送的对象
    if user != '':
        mapping_img('sousuokuang.png','single')
        pyautogui.typewrite(user) #键盘输入要搜索的用户
        time.sleep(1)
        pyautogui.moveRel(xOffset=0,yOffset=80) #移动像素是测算好的
        pyautogui.click()
        time.sleep(1)
    else:
        mapping_img('myhead.png','single')
        pyautogui.moveRel(xOffset=316,yOffset=238) #移动像素是测算好的
        pyautogui.click()

def read_txt(txt):

    hellofile = open(txt,"r", encoding="UTF-8")
    #读取并存到列表里,知道共有多少行
    hellocontent = hellofile.readlines()
    print(len(hellocontent))
    # 随机选取一行,并输出选取的哪一行
    number = random.randint(0,len(hellocontent)-1)
    print( 'line number is {}'.format(number))
    pyperclip.copy(hellocontent[number])
    pyautogui.hotkey('ctrl','v')
    hellofile.close() #记得read完要close



def read_img(img_name): #点击微信里‘发送文件’,选取图片
    #可以在文件名出选择完整路径,直接选择图片文件
    mapping_img('file.png','single')
    time.sleep(1)
    pyautogui.typewrite(img_name)
    pyautogui.hotkey('enter')




def main():
    os.chdir('E:\\Python study\\python二级\\')
    print(os.getcwd())
    #登陆微信
    mapping_img('wechat.png','double')
    #所搜要发的人
    chat_user('la vie') #不写默认是自己
    #发送语句1
    read_txt('yuju1.txt')
    pyautogui.hotkey('ctrl','enter')
    time.sleep(1)
    #发送语句2
    read_txt('yuju2.txt')
    pyautogui.hotkey('ctrl','enter')
    time.sleep(1)
    #发送照片
    read_img('img.jpg')
    time.sleep(1)
    pyautogui.hotkey('ctrl','enter')

if __name__ == '__main__':
    main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值