python自动发送微信信息和文件

from wxauto import *
import schedule
import time
class WeChatAuto:
    def __init__(self):
        # 获取当前微信客户端
        self.wx = WeChat()
        # 获取会话列表
        self.wx.GetSessionList()
    # 向单个用户发消息
    @staticmethod
    def send_msg_to_single_user(self, msg, user):
        try:
            print(f"向用户`{user}`发送消息:{msg}")
            self.wx.ChatWith(user)  # 打开`对方`聊天窗口
            self.wx.SendMsg(msg)
            print("发送完毕")
        except Exception as e:
            print("发送失败,原因:", e)
    # 函数功能: 单个用户 单个文件发送
    @staticmethod
    def send_file_to_single_user(self, file, user):
        try:
            print(f"向用户`{user}`发送文件:{file}")
            self.wx.ChatWith(user)  # 打开`对方`聊天窗口
            self.wx.SendFiles(file)
            print("发送完毕")
        except Exception as e:
            print("发送失败,原因:", e)
    # 向多个用户发多个文件
    @staticmethod
    def send_files_to_mul_user(self, files, users):
        for who in users:
            self.wx.ChatWith(who)
            for file in files:
                print(f"向用户`{who}`发送文件:{file}")
                self.wx.SendFiles(file)
        print("发送成功")
if __name__ == '__main__':
    wechat = WeChatAuto()
    # 定时任务,每10秒发一次消息
    def send_msg_job():
        wechat.send_msg_to_single_user(wechat, "测试内容", "眠羊")
        file = r'D:\花生搬家\python项目\有趣小工具\wifi.png'
        who = '眠羊'
        wechat.send_file_to_single_user(wechat,file=file, user=who)
        # whos = ['对方1', '对方2']
        # files = [r'C:\Users\111\Desktop\1.jpg',
        #          r'C:\Users\111\Desktop\2.jpg']
        # wechat.send_files_to_mul_user(wechat, files=files, users=whos)
    schedule.every(10).seconds.do(send_msg_job)
    while True:
        schedule.run_pending()
        time.sleep(1)

VBA无法直接与微信进行文件传输,因为微信没有开放相关的API供第三方程序调用。然而,我们可以通过模拟键盘操作来实现自动发送文件微信好友的功能。 首先,我们需要准备好待发送文件以及微信的窗口。然后,我们可以使用VBA的SendKeys函数来模拟按键操作,将待发送文件路径输入到微信消息框中。 下面是一个示例代码,演示了如何使用VBA发送文件微信好友: ```vba Sub SendFileToWeChat() Dim filePath As String Dim weChatWindow As String ' 设置待发送文件路径微信窗口标题 filePath = "C:\path\to\your\file.txt" weChatWindow = "微信" ' 激活微信窗口 AppActivate weChatWindow ' 模拟键盘操作,将文件路径输入到微信消息框中 SendKeys filePath & "{ENTER}", True ' 模拟按下回车键,发送文件 SendKeys "{ENTER}", True End Sub ``` 在上述示例代码中,我们首先定义了待发送文件路径微信窗口标题。然后,使用`AppActivate`函数激活微信窗口。接下来,使用`SendKeys`函数将文件路径输入到微信消息框中,并按下回车键发送文件。 需要注意的是,上述示例代码仅适用于Windows系统,并且需要确保微信窗口已经打开并且在前台显示。 总的来说,通过模拟键盘操作,我们可以实现使用VBA发送文件微信好友的功能。但是,这种方法可能不够稳定,且依赖于操作系统微信窗口的状态。因此,建议根据具体情况考虑其他方式来实现文件传输的功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值