1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python自动发送微信文件_Python脚本定期发送微信文件 定时

python自动发送微信文件_Python脚本定期发送微信文件 定时

时间:2020-12-27 12:51:37

相关推荐

python自动发送微信文件_Python脚本定期发送微信文件 定时

背景:朋友让我改一个文件,可我改完已经半夜了。这时候如果微信发给朋友恐会打扰他休息。于是决定用Python写一个自动发送微信信息的脚本,第二天早上自动发送。

任务简述:使用Python自动化程序,把file.txt在早上6点准时拖动到微信并发送。

0 准备

首先需要考虑的是如何通过Python模拟鼠标操作。在此我货比三家:

module

stars

latest commit

903

.2.26

274

.1.2

3600

.1.14

对比后,我们选用PyAutoGUI。

Windows/Mac安装:

pip install pyautogui

conda虚拟环境通过豆瓣源加速:

python -m pip install -i /simple/ pyautogui

1 获取坐标

要发送什么文件,首先需要获取文件坐标,然后再模拟鼠标拖动。

要获取屏幕坐标,可以用下面的代码:

import pyautogui

import time

time.sleep(2)

print(pyautogui.position())

上面的程序会先暂停2秒,等待你将鼠标放到文件上。2秒后,程序会输出文件的屏幕坐标。在我的测试中,我把文件放到了屏幕右上角,坐标为

(1857, 31)

但是这样做有些不够自动化。

pyautogui提供了更自动化的方法:

pyautogui.locateOnScreen()

pyautogui.locateCenterOnScreen()

我们需要

file.txt

的坐标,就先截图,记得不要太大,如下。

这种截图情况下是不可以移动文件的,否则文件的背景会改变。所以截图越小越好,最好没有背景,如下:

然后将截图命名后,放入python同目录下,用这行代码代替即可:

file_pos = pyautogui.locateCenterOnScreen('file.png')

注意,运行代码的时候不要遮住屏幕上的图片位置。

2 模拟拖动过程

这里要做的是把文件从

(1857, 31)

拖动到微信框的坐标。获取微信框坐标的代码就不赘述了。拖动使用的是dragTo方法,其中参数

duration

一定要设定,为拖动时间。直接上代码。

import pyautogui

file_pos = pyautogui.locateCenterOnScreen('file.png')

wechat_pos = pyautogui.locateCenterOnScreen('wechat.png')

print(file_pos,wechat_pos)

pyautogui.moveTo(file_pos)

pyautogui.dragTo(wechat_pos, duration=0.5)

pyautogui.click(wechat_pos)

pyautogui.press('enter')

3 定时

定时操作很简单,target_time为设置的时间,精确到秒。 time.sleep()可以设置每隔多久检测一次

import datetime

import time

target_time = datetime.datetime(, 3, 18, 6, 0, 0)

current_time = datetime.datetime.now()

while current_time < target_time:

time_delta = target_time-current_time

minutes_delta = int(time_delta.seconds/60)

seconds_delta = time_delta.seconds - int(time_delta.seconds/60)*60

print("current time:{}, {}:{} left".format(current_time, minutes_delta, seconds_delta))

if current_time < target_time:

time.sleep(60)

current_time = datetime.datetime.now()

import pyautogui

file_pos = pyautogui.locateCenterOnScreen('file.png')

wechat_pos = pyautogui.locateCenterOnScreen('wechat.png')

print(file_pos,wechat_pos)

pyautogui.moveTo(file_pos)

pyautogui.dragTo(wechat_pos, duration=0.5)

pyautogui.click(wechat_pos)

pyautogui.press('enter')

4 注意事项

如果设置的是很久以后的定时发送,要记得防止屏幕自动锁屏!

5 其他

这里的拖动可以用一些更简单的操作来代替,比如复制粘贴,键盘模拟ctrl+c、ctrl+v即可。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。