demo_roob.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2024/3/22 18:03
  3. # @Author : Clown
  4. # @File : demo_roob.py
  5. # @Software : PyCharm
  6. import requests
  7. from requests_toolbelt import MultipartEncoder,MultipartEncoderMonitor
  8. def my_callback(monitor):
  9. # 进度条读取
  10. progress = (monitor.bytes_read / monitor.len) * 100
  11. print("\r 文件上传进度:%d%%(%d/%d)" % (progress, monitor.bytes_read, monitor.len), end="")
  12. return progress
  13. def sendFile(path,file_name,webhook_url):
  14. key = webhook_url.split('key=')[1]
  15. url_upload = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={key}&type=file'
  16. # 流文件上传方式
  17. data_upload = MultipartEncoder(
  18. {'files': (file_name, open(path+'/'+file_name, 'rb'), 'text/csv')})
  19. data_upload = MultipartEncoderMonitor(data_upload, my_callback)
  20. headers_upload = {
  21. "Content-type": data_upload.content_type}
  22. resp_upload = requests.post(url_upload, headers = headers_upload, data = data_upload).json()
  23. media_id = resp_upload['media_id']
  24. params = {
  25. "msgtype": "file",
  26. "file": {
  27. "media_id": f"{media_id}"
  28. }
  29. }
  30. resp = requests.post(webhook_url,json=params).json()
  31. print(resp)
  32. if __name__ == '__main__':
  33. webhook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cf7139e1-e623-41ca-8541-5dc6e26d43b0'
  34. path = r'C:\Users\ClownHe\Desktop'
  35. file_name = 'order_formsgoods_zzx.xlsx'
  36. sendFile(path, file_name, webhook_url)