功能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()