如果当了班委,怎样在WeChat上自动给同学发通知消息?
项目工程的目录内容如下:
main.py的代码如下:
# coding: utf-8
from time import sleep
from os import system
from pywinauto.keyboard import send_keys
import pyperclip
'''
不存在对象库.txt:存放没有加好友的名单。
发送内容.txt:群发的消息内容。
待发送对象.txt:群发的名单;程序运行后,保留没有发送成功的名单。
无需确认对象库.txt:该文件里累积存入,已经成功群发成功的对象,这些对象之后每次群发时,都无需在终端里确认。
'''
if __name__ == '__main__':
notObjects = [] # 不存在的联系人
sendFailObjects = [] # 发送失败的对象
confirmedObjects = [] # 已经确认过可成功发送的对象
newConfirmedObjects = [] # 本次新增确认可成功发送的对象
content = "" # 发送内容
with open('不存在对象库.txt', 'r', encoding='utf-8') as notexistf:
notObjects = notexistf.read().split()
notexistf.close()
with open('无需确认对象库.txt', 'r', encoding='utf-8') as confirmedf:
confirmedObjects = confirmedf.read().split()
confirmedf.close()
with open('发送内容.txt', 'r', encoding='utf-8') as contentf:
content = contentf.read()
contentf.close()
pyperclip.copy(content) # 将待发送内容复制到粘贴板
wechat_path = r'C:\Tencent\WeChat\WeChat.exe'
cnts = 0
cntf = 0
with open('待发送对象.txt', 'r', encoding='utf-8') as objectf:
for object in objectf.read().split():
system(wechat_path) # 打开微信
send_keys('^f') # 查找快捷键
send_keys(object) # 查找聊天对象
sleep(1)
if (object in confirmedObjects): # 该对象之前已经被确认可发送成功,无需核对,直接发送
send_keys('{ENTER}') # 回车进入聊天框
sleep(1)
send_keys('^v') # ctrl v ,粘贴聊天内容
send_keys('{ENTER}') # 回车发送
print('*' + object + ',发送完毕!')
cnts += 1
else:
print(object + ',是否查找正确?\n 正确请直接Enter,否则输入其他任意字符再Enter.')
if (input() == '' and object not in notObjects):
system(wechat_path) # 打开微信
send_keys('{ENTER}') # 回车进入聊天框
sleep(1)
send_keys('^v') # ctrl v ,粘贴聊天内容
send_keys('{ENTER}') # 回车发送
newConfirmedObjects.append(object)
print('*' + object + '发送完毕!')
cnts += 1
else:
sendFailObjects.append(object)
cntf += 1
system(wechat_path) # 打开微信
send_keys('^f') # 查找快捷键
send_keys('{BKSP}{BKSP}{BKSP}') # 清除搜索框里的无效联系人
objectf.close()
tmp = ''
for i in sendFailObjects:
tmp += i + '\n'
with open('待发送对象.txt', 'w', encoding='utf-8') as sendFailObjectsf:
sendFailObjectsf.write(tmp)
sendFailObjectsf.close()
tmp = ''
for i in newConfirmedObjects:
tmp += i + '\n'
with open('无需确认对象库.txt', 'a', encoding='utf-8') as confirmedf:
confirmedf.write(tmp)
confirmedf.close()
print(str(cnts) + '人 发送成功!!!\n' + str(cntf) + '人 发送失败!!!')
system('pause')
qunfa.bat的代码如下:
chcp 65001
start python .\main.py