Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit daacb14

Browse files
author
root
committedNov 23, 2021
export_mysql_data_to_csv
1 parent 0fa2954 commit daacb14

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# export_mysql_to_csv_send_to_wocom
2+
3+
Export the data in mysql into CSV files and send them to enterprise wechat group chat.
4+
5+
### Prerequisites
6+
7+
- PyMySQL==1.0.2
8+
- requests==2.26.0
9+
10+
### How to run the script
11+
12+
```shell
13+
# 1. edit config.ini
14+
$ vim config.ini
15+
# 2. run script
16+
$ python export_mysql_data_to_csv.py
17+
```
18+
19+
### Screenshot/GIF showing the sample use of the script
20+
21+
![pic](./pic.png)
22+
23+
### Author Name
24+
25+
[Yuan Lei(雷园)](https://github.com/LeiyuanBlog)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[db]
2+
host = 127.0.0.1
3+
username = root
4+
password = 123456
5+
database = user
6+
[wecom]
7+
key = *
8+
[message]
9+
sql = select * from user
10+
title = 测试文件
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
import codecs
3+
import configparser
4+
import csv
5+
import time
6+
7+
import pymysql
8+
import requests as requests
9+
10+
ini = configparser.ConfigParser()
11+
ini.read('config.ini')
12+
wecom_key = ini.get('wecom', 'key')
13+
14+
15+
def sql(sqlstr): # 定义一个执行SQL的函数
16+
conn = pymysql.connect(host=ini.get('db', 'host'), user=ini.get('db', 'username'),
17+
password=ini.get('db', 'password'), database=ini.get('db', 'database'))
18+
cursor = conn.cursor()
19+
cursor.execute(sqlstr)
20+
results = cursor.fetchall() # 获取查询的所有记录
21+
cursor.close()
22+
conn.close()
23+
return results
24+
25+
26+
def read_mysql_to_csv(filename):
27+
with codecs.open(filename=filename, mode='w', encoding='utf-8') as f:
28+
write = csv.writer(f, dialect='excel')
29+
results = sql(
30+
ini.get('message', 'sql')
31+
)
32+
for result in results:
33+
write.writerow(result)
34+
35+
36+
def upload_file_robots(filename):
37+
url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=%(key)s&type=file" % {"key": wecom_key}
38+
data = {'file': open(filename, 'rb')} # post jason
39+
response = requests.post(url=url, files=data) # post 请求上传文件
40+
json_res = response.json() # 返回转为json
41+
media_id = json_res['media_id'] # 提取返回ID
42+
return media_id # 返回请求状态
43+
44+
45+
def send_file_robots(media_id):
46+
wx_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%(key)s' % {"key": wecom_key}
47+
data = {"msgtype": "file",
48+
"file": {"media_id": media_id}} # post json
49+
r = requests.post(url=wx_url, json=data)
50+
return r
51+
52+
53+
if __name__ == '__main__':
54+
filename = ini.get('message', 'title') + time.strftime('%y%m%d') + '.csv'
55+
read_mysql_to_csv(filename)
56+
print(send_file_robots(upload_file_robots(filename)))
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
certifi==2021.10.8
2+
charset-normalizer==2.0.7
3+
configparser==5.0.2
4+
idna==3.3
5+
importlib-metadata==4.8.1
6+
Jinja2==3.0.2
7+
MarkupSafe==2.0.1
8+
prettytable==2.4.0
9+
pyecharts==1.9.0
10+
PyMySQL==1.0.2
11+
requests==2.26.0
12+
simplejson==3.17.5
13+
typing-extensions==3.10.0.2
14+
urllib3==1.26.7
15+
wcwidth==0.2.5
16+
xlwt==1.3.0
17+
zipp==3.6.0

0 commit comments

Comments
 (0)
Failed to load comments.