# -*- codeing = utf-8 -*- # @Time : 2024/3/22 18:03 # @Author : Clown # @File : demo_roob.py # @Software : PyCharm import requests from requests_toolbelt import MultipartEncoder,MultipartEncoderMonitor def my_callback(monitor): # 进度条读取 progress = (monitor.bytes_read / monitor.len) * 100 print("\r 文件上传进度:%d%%(%d/%d)" % (progress, monitor.bytes_read, monitor.len), end="") return progress def sendFile(path,file_name,webhook_url): key = webhook_url.split('key=')[1] url_upload = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={key}&type=file' # 流文件上传方式 data_upload = MultipartEncoder( {'files': (file_name, open(path+'/'+file_name, 'rb'), 'text/csv')}) data_upload = MultipartEncoderMonitor(data_upload, my_callback) headers_upload = { "Content-type": data_upload.content_type} resp_upload = requests.post(url_upload, headers = headers_upload, data = data_upload).json() media_id = resp_upload['media_id'] params = { "msgtype": "file", "file": { "media_id": f"{media_id}" } } resp = requests.post(webhook_url,json=params).json() print(resp) if __name__ == '__main__': webhook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cf7139e1-e623-41ca-8541-5dc6e26d43b0' path = r'C:\Users\ClownHe\Desktop' file_name = 'order_formsgoods_zzx.xlsx' sendFile(path, file_name, webhook_url)