python自动化办公------定时发送微信消息
身边的一位朋友的真实事迹分享给大家,就暂称他C吧。C是一个英俊潇洒的健身男,经常沾花惹草,桃花运不断,听C说他的微信好友就有5000+,平时回消息都回不过来,后来,他不知道从哪冒出来的想法,花了一个月报了一个IT培训班,听说是python自动化办公之类的吧,学成后自己开发了一个软件,叫定时发送微信消息。C建立了一个甜言蜜语的数据库,平时微信消息回不过来,他就使用自己开发的软件,调用数据库里的话发送给其微信好友,事业和生活胡不耽误。 听了这个消息,我不由得敬佩,看来python自动化已经深入大家的生活了!!!我联系到他,他将他的软件打包给了我之后,我稍微做了一些修改,分享给大家。 源代码如下:
```python
import pyautogui
import time
def send_mas_englishi_one(flag_i):
time.sleep(5)
pyautogui.click(x=118, y=361, clicks=2, interval=0, button='left')
time.sleep(1)
pyautogui.click(x=984, y=138, clicks=1, interval=0, button='left')
time.sleep(1)
print("现在准备给{}发送消息".format(id_que[flag_i]))
pyautogui.hotkey('shift')
time.sleep(2)
pyautogui.typewrite(id_que[flag_i], interval=0.1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(2)
time.sleep(2)
pyautogui.click(x=1195, y=653, clicks=1, interval=0, button='left')
time.sleep(1)
pyautogui.typewrite(msg_que[flag_i], interval=0.1)
time.sleep(1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.click(x=1704, y=116, clicks=1, interval=0, button='left')
print("发送成功!")
time.sleep(3)
time.sleep(1)
def send_mas_chinese_one(flag_i):
time.sleep(5)
pyautogui.click(x=118, y=361, clicks=2, interval=0, button='left')
time.sleep(1)
pyautogui.click(x=984, y=138, clicks=1, interval=0, button='left')
time.sleep(1)
print("现在准备给{}发送消息".format(id_que[flag_i]))
pyautogui.hotkey('shift')
time.sleep(2)
pyautogui.typewrite(id_que[flag_i], interval=0.1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(2)
pyautogui.hotkey('shift')
print("现在是中文模式")
time.sleep(2)
pyautogui.click(x=1195, y=653, clicks=1, interval=0, button='left')
time.sleep(1)
pyautogui.typewrite(msg_que[flag_i], interval=0.1)
time.sleep(1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.click(x=1704, y=116, clicks=1, interval=0, button='left')
print("发送成功!")
time.sleep(3)
time.sleep(1)
def send_mas_englishi_two(flag_i):
time.sleep(5)
pyautogui.click(x=118, y=361, clicks=2, interval=0, button='left')
time.sleep(1)
pyautogui.click(x=984, y=138, clicks=1, interval=0, button='left')
time.sleep(1)
print("现在准备给{}发送消息".format(id_que[flag_i]))
pyautogui.hotkey('shift')
time.sleep(2)
pyautogui.typewrite(id_que[flag_i], interval=0.1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(2)
pyautogui.hotkey('shift')
print("现在是中文模式")
time.sleep(2)
pyautogui.click(x=1195, y=653, clicks=1, interval=0, button='left')
time.sleep(1)
pyautogui.typewrite(msg_que[flag_i], interval=0.1)
time.sleep(1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.click(x=1704, y=116, clicks=1, interval=0, button='left')
print("发送成功!")
time.sleep(3)
time.sleep(1)
def send_mas_chinese_two(flag_i):
time.sleep(5)
pyautogui.click(x=118, y=361, clicks=2, interval=0, button='left')
time.sleep(1)
pyautogui.click(x=984, y=138, clicks=1, interval=0, button='left')
time.sleep(1)
print("现在准备给{}发送消息".format(id_que[flag_i]))
time.sleep(2)
pyautogui.typewrite(id_que[flag_i], interval=0.1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(2)
pyautogui.hotkey('shift')
print("现在是中文模式")
time.sleep(2)
pyautogui.click(x=1195, y=653, clicks=1, interval=0, button='left')
time.sleep(1)
pyautogui.typewrite(msg_que[flag_i], interval=0.1)
time.sleep(1)
time.sleep(1)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.click(x=1704, y=116, clicks=1, interval=0, button='left')
print("发送成功!")
time.sleep(3)
time.sleep(1)
if __name__=="__main__":
id_que=[]
msg_que=[]
send_time_que=[]
is_chinese_que=[]
zhongyingwen=["1"]
while True:
id = input("请输入发送人的备注,或者ID(请确保ID存在):").strip()
msg = input("请输入将要发送的内容():").strip()
is_chinese=input("发送的信息时中文请输入1,其他键为否").strip()
send_time = input("请输入发送的时间(格式14:00:00):").strip()
id_que.append(id)
msg_que.append(msg)
send_time_que.append(send_time)
is_chinese_que.append(is_chinese)
print(id)
print(msg)
print(send_time)
is_continue=input("结束请输入1,否则继续").strip()
if is_continue=='1':
break
else:
continue
print("结束录入,等待消息发送!!")
print("loading......")
send_number=0
length=len(send_time_que)
print(length)
print(id_que[0])
while True:
if send_number<length:
print("一共有{}条消息,共发送了{}条消息".format(length,send_number))
for i in range(len(send_time_que)):
if time.strftime("%H:%M:%S") == send_time_que[i]:
flag=i
print(i)
if zhongyingwen[-1]=="1":
if is_chinese_que[flag]=="1":
zhongyingwen.append("1")
send_mas_chinese_one(flag_i=flag)
else:
send_mas_englishi_one(flag_i=flag)
zhongyingwen.append("0")
else:
if is_chinese_que[flag]=="1":
zhongyingwen.append("1")
send_mas_chinese_two(flag_i=flag)
else:
send_mas_englishi_two(flag_i=flag)
zhongyingwen.append("0")
send_number+=1
else:
print("全部已经发送!")
break
比如说,我要在今天21:40发送给我的好友yangyang 发送消息,运行程序后,如下设置: 
 到了设定的时间后,电脑就在自动的打开微信,给我的好友yangyang发送消息。  是不是很神奇?其实这短代码非常简单,只是调用了pyautogui的API实现了鼠标移动和键盘键入,而我们平时使用的APP不都是这样的吗?如果对于一些简单而且重复性的工作,使用pyautogui就可以事半功倍。关于pyautogui,读者可以在pyautogui官网进行学习,建议使用谷歌浏览器进行阅读,毕竟谷歌自带的翻译功能是非常强大的,非常有利于英文水平低的读者进行学习。 如果对我的文章感兴趣,请为我点一个赞,如果有python的知识需要了解或探讨,可以加本人微信cuiliang1666457052
|