Python查询sqlserver数据库保存数据结果到excel文件通过企业微信助手发送到企业微信群聊

完整程序

import datetime
from requests_toolbelt import MultipartEncoder
from urllib import parse
import xlwt
import time
import pymssql
import requests
import json
from fake_useragent import UserAgent
import os  # 要想使用路径相关功能需要导入 os 模块
from apscheduler.schedulers.blocking import BlockingScheduler
import pytz
import logging
timezone = pytz.timezone('Asia/Shanghai')
scheduler = BlockingScheduler()
requests.adapters.DEFAULT_RETRIES = 5  # 增加重连次数
s = requests.session()
s.keep_alive = False
ua=UserAgent()
# 配置日志格式
logging.basicConfig(filename='downLoad.log', format='%(asctime)s - %(levelname)s - %(message)s', encoding='utf-8')
def read_db_config():
    BASE_DIR = os.path.abspath(__file__)
    a = BASE_DIR.split("\\")[:-2]
    fat = '/'.join(a)
    # 读取数据库配置文件
    with open(fat+'/config.json') as file:
        config = json.load(file)
    return config


class downLoad():
    def __init__(self) -> object:
        self.logger = logging.getLogger(__name__)
        self.sheet_name = 'report_' + time.strftime("%Y-%m-%d")
        self.filename = 'report_' + time.strftime("%Y-%m-%d") + '.xls'
        self.out_path = 'd:/data/任务表'  + '.xls'
        # 下面有说明
        self.url = "XXXXXXXXX"
        self.file_path = self.out_path
        pass
    def selectData(self):
        config = read_db_config()
        with pymssql.connect(**config) as connect:
            print(config)            # if connect:
            #     print("连接成功")
            with connect.cursor() as cursor:
                sql = """
                    SELECT
                        task_area,
                        opinion_note,
                        task_title,
                        start_time
                    FROM
                        [dbo].[p_task_temp]
                    WHERE
                        start_time BETWEEN DATEADD( hh, - 1, GETDATE( ) )
                        AND GETDATE( )
                    order by start_time asc
                """
                 # 处理数据
                cursor.execute(sql)
                # 搜取所有结果
                results = cursor.fetchall()
                if len(results)==0:
                    return
                # 获取MYSQL里面的数据字段名称
                fields = cursor.description
                workbook = xlwt.Workbook(encoding='utf-8')  # workbook是sheet赖以生存的载体。
                sheet = workbook.add_sheet(self.sheet_name, cell_overwrite_ok=True)
                # 写上字段信息
                for field in range(0, len(fields)):
                    filedname = fields[field][0];
                    if filedname == 'task_area':
                        filedname = '序号'
                    if filedname == 'task_title':
                        filedname = '标题'
                    if filedname == 'start_time':
                        filedname = '开始时间'
                    if filedname == 'opinion_note':
                        filedname = '处理意见'
                    sheet.write(0, field, filedname)
                # 获取并写入数据段信息
                row = 1
                col = 0
                for row in range(1, len(results) + 1):
                    for col in range(0, len(fields)):
                        sheet.write(row, col, u'%s' % results[row - 1][col])
                workbook.save(self.out_path)
                self.logger.info(f"下载成功!")
    #             发送企业微信
                self.QYWXSendGroupFile(self.file_path,self.url)

    def QYWXSendGroupFile(self,filepath, url):
        """
        发送微信群组机器人文件
        :param filepath: 文件路径
        :param url: 群组机器人WebHook
        :return:
        """
        # url为群组机器人WebHook,配置项
        headers = {
            "content-type": "application/json"
        }
        # 发送文件需要先上传文件获取media_id
        media_id = self.UploadFile(filepath, url)
        msg = {"msgtype": "file", "file": {"media_id": media_id}}
        # 发送请求
        try:
            result = requests.post(url, headers=headers, json=msg)
            return True
        except Exception as e:
            print("企业微信机器人发送文件失败,详细信息:" + str(e))
            return False

    def UploadFile(self,filepath, webHookUrl):
        """
        企业微信机器人上传文件,发送文件前需要先上传--要求文件大小在5B~20M之间
        :param filepath: 文件路径
        :param webHookUrl: 群组机器人WebHook
        :return: media_id
        """
        # url为群组机器人WebHook,配置项
        url = webHookUrl
        params = parse.parse_qs(parse.urlparse(webHookUrl).query)
        webHookKey = params['key'][0]
        upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={webHookKey}&type=file'
        headers = {"Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, deflate",
                   "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36"}
        filename = os.path.basename(filepath)
        try:
            multipart = MultipartEncoder(
                fields={'filename': filename, 'filelength': '', 'name': 'media',
                        'media': (filename, open(filepath, 'rb'), 'application/octet-stream')},
                boundary='-------------------------acebdf13572468')
            headers['Content-Type'] = multipart.content_type
            resp = requests.post(upload_url, headers=headers, data=multipart)
            json_res = resp.json()
            if json_res.get('media_id'):
                # print(f"企业微信机器人上传文件成功,file:{filepath}")
                return json_res.get('media_id')
        except Exception as e:
            # print(f"企业微信机器人上传文件失败,file: {filepath}, 详情:{e}")
            print("企业微信机器人上传文件失败,详细信息:" + str(e))
            return ""

    def run(self):
        # 判断文件是否存在
        if (os.path.exists(self.out_path)):
            # 存在,则删除文件
            os.remove(self.out_path)
        self.selectData()



# 测试
# if __name__=="__main__":
#     env = downLoad()
#     env.run()



# 每一个整点执行
@scheduler.scheduled_job('cron', year="*", month="*", day="*", hour="*", minute="0", second="0")
def request_update_status():
    env = downLoad()
    env.run()


scheduler.start()

微信群聊机器人路径url
在这里插入图片描述
/config.json 数据库配置文件

{
    "server": "数据库ip地址,例:127.0.0.1",
    "user": "数据库用户名,root",
    "password": "数据库密码,root",
    "database": "数据库名称:dataBase",
    "port": 3306,
    "charset": "utf8"
}

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 中的 `requests` 库通过企业微信机器人发送文件。具体步骤如下: 1. 获取企业微信机器人的 Webhook 地址:在企业微信管理后台中,创建一个机器人,获取机器人的 Webhook 地址。将 Webhook 地址保存在一个变量中,例如: ```python webhook_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' ``` 2. 指定要发送文件路径:使用 Python 中的 `os` 库指定要发送文件的路径。 ```python import os file_path = os.path.join(os.getcwd(), 'example.xlsx') # 假设文件在当前目录下 ``` 3. 读取文件内容:使用 Python 中的 `open()` 方法打开文件,并使用 `read()` 方法读取文件内容。将文件内容保存在一个变量中,例如: ```python with open(file_path, 'rb') as f: file_content = f.read() ``` 4. 使用 `requests` 库发送文件:使用 `requests.post()` 方法向机器人的 Webhook 地址发送一个 POST 请求,将文件作为请求的正文发送。 ```python import requests response = requests.post(webhook_url, data=file_content) ``` 在上述代码中,`data` 参数指定了请求的正文,即要发送文件内容。由于企业微信机器人要求发送文件必须是 base64 编码后的格式,因此需要在发送前将文件内容进行 base64 编码。可以使用 Python 中的 `base64` 库来进行编码,例如: ```python import base64 file_content_base64 = base64.b64encode(file_content) response = requests.post(webhook_url, data=file_content_base64) ``` 在发送请求时,还需要在请求头中指定 `Content-Type` 参数为 `application/json`,并在请求正文中指定要发送文件文件名和文件类型,例如: ```python headers = {'Content-Type': 'application/json'} data = { 'msgtype': 'file', 'file': { 'base64': file_content_base64.decode(), 'filename': 'example.xlsx', 'md5': hashlib.md5(file_content).hexdigest(), 'fileext': 'xlsx' } } response = requests.post(webhook_url, headers=headers, json=data) ``` 在上述代码中,`msgtype` 参数指定要发送的消息类型为文件,`file` 参数指定要发送文件的相关信息,包括 base64 编码后的文件内容、文件名、文件 MD5 值和文件扩展名。 注意,使用企业微信机器人发送文件需要保证机器人账号已经加入了要发送的群或个人的联系人列表中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值