pyautogui实现微信发送消息

```
import pyautogui
import time
import pyperclip

def type_chinese_text(text):
pyperclip.copy(text) # 将中文文本复制到剪贴板
pyautogui.hotkey('ctrl', 'v') # 模拟按下Ctrl+V进行粘贴

def Open_Wechat():
#使用快捷键打开微信。这个微信的默认设置的快捷键。
pyautogui.hotkey('ctrl', 'alt', 'c')
time.sleep(1)

def Chat_Who(ContactPerson):
#使用快捷键打开查找,找一个叫Tom的联系人。
pyautogui.hotkey("ctrl","f")
pyautogui.write(ContactPerson)
time.sleep(1)
pyautogui.hotkey('Enter')
time.sleep(1)

def Sent_Msg(Msg):
#给Tom发消息,然后回车发送。
# pyautogui.write(Msg)
text_to_type = "今天不加班" # 需要输入的中文文本
type_chinese_text(text_to_type)
pyautogui.hotkey('Enter')

if __name__ == '__main__':
Open_Wechat()
ContactPerson = "jiatinghuiyi"
Chat_Who(ContactPerson)
msg = "下班了"
Sent_Msg(msg)
# import pyautogui
# import time
# # # 循环直到找到图像或者超时
# # start_time = time.time()
# # timeout = 10 # 设置超时时间为10秒
# # while True:
# # # 在屏幕上搜索图像
# # image_location = pyautogui.locateOnScreen('wx.png', confidence=0.9)

# # # 如果找到图像,打印位置并跳出循环
# # if image_location is not None:
# # print(f"图像位置:{image_location}")
# # break

# # # 如果超过超时时间,跳出循环
# # current_time = time.time()
# # if current_time - start_time > timeout:
# # print("超时,未找到图像。")
# # break

# # # 等待一段时间再次进行搜索
# # time.sleep(1)

# # exit()
# import pyautogui
# x, y = pyautogui.position()
# print(f"鼠标当前坐标:({x}, {y})")
# # 寻找微信图标
# # 设置相似度为 0.9
# confidence = 0.9
# # 在屏幕上查找指定图像,并返回其位置
# wechat_icon = pyautogui.locateOnScreen('D:\\wx.png', confidence=confidence)
# if wechat_icon:
# print(f"找到图像在 {wechat_icon} 的位置")
# else:
# print("未找到图像")
# # exit()
# # 点击微信图标
# # pyautogui.click(wechat_icon)
# msg_input_center = pyautogui.center(wechat_icon)
# # print(msg_input_center)
# pyautogui.doubleClick(msg_input_center)
# # 定位并点击微信消息输入框
# msg_input_pos = pyautogui.locateOnScreen('search.png')
# # msg_input_center = pyautogui.center(msg_input_pos)
# # pyautogui.click(msg_input_center)
# # print(msg_input_center)
# # # 输入要发送的消息
# # pyautogui.typewrite('jiatinghuiyi', interval=0.1)
# # # 模拟按下回车键
# # pyautogui.press('enter')
# # 使用 pyautogui.click 函数来单击搜索框
# pyautogui.click(msg_input_pos)

# # 使用 pyautogui.write 函数来输入要搜索的关键字
# pyautogui.write('jiatinghuiyi')

# # 使用 pyautogui.press 函数来模拟按下回车键
# pyautogui.press('enter')

# msg_input = pyautogui.locateOnScreen('input.png')
# pyautogui.click(msg_input)
# # 输入要发送的消息
# pyautogui.typewrite('hello', interval=0.1)
# # 模拟按下回车键
# pyautogui.press('enter')

# # image = pyautogui.screenshot()
# # image.save('input.png')
```

    A+
发布日期:2023年08月02日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: