1. 程式人生 > >Python導出sql語句結果到Excel

Python導出sql語句結果到Excel

isp cte 分享 content plain gb2 定期 shee attach

本文檔是因為每周需要統計線上數據庫中客戶新增資源,手動執行實在是麻煩,就寫了個腳本導出到Excel,順便發一封郵件。

(當然這不是線上的真實腳本,不過根據個人需求稍微修改下,還是可以直接用的。拿去不謝!!)

將想發出郵件的SQL語句寫到exec.sql:

 vim /tmp/exec.sql
select * from db;

編輯Python腳本:

root@localhost:/tmp# vim exportsql.py
#!/usr/bin/python
# coding: utf-8
import sys
import xlwt
import pymysql
import datetime
import subprocess
import 
time import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication import os.path host = localhost user = root pwd = jeqThs1qOVbHGRz0 port = 3306 db = mysql sql_file = exec.sql sheet_name =
vm + time.strftime("%Y-%m-%d") filename = vm_ + time.strftime("%Y-%m-%d") + .xls out_path = /tmp/vm_ + time.strftime("%Y-%m-%d") + .xls def export(): conn = pymysql.connect(host, user, pwd, db, charset=utf8) cursor = conn.cursor() with open(u%s % sql_file, r+) as f: sql_list
= f.read().split(;)[:-1] # sql文件最後一行加上; sql_list = [x.replace(\n, ) if \n in x else x for x in sql_list] # 將每段sql裏的換行符改成空格 ##執行sql語句,使用循環執行sql語句 for sql in sql_list: #print(sql) count = cursor.execute(sql) # print("查詢出" + str(count) + "條記錄") if count > 0: # 來重置遊標的位置 cursor.scroll(0, mode=absolute) # 搜取所有結果 results = cursor.fetchall() # 獲取MYSQL裏面的數據字段名稱 fields = cursor.description workbook = xlwt.Workbook(encoding=utf-8) # workbook是sheet賴以生存的載體。 sheet = workbook.add_sheet(sheet_name, cell_overwrite_ok=True) # 寫上字段信息 for field in range(0, len(fields)): sheet.write(0, field, fields[field][0]) # 獲取並寫入數據段信息 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(out_path) else: pass _user = "[email protected]" _pwd = "*********" areceiver = "[email protected]" #抄送人,可寫可不寫,多個用,隔開 acc = "*****@capitalonline.net" msg = MIMEMultipart() msg["Subject"] = udata_ + time.strftime("%Y-%m-%d") msg["From"] = _user msg["To"] = areceiver msg["Cc"] = acc def send_email(): content = ‘‘‘Hello, everyone,This is a test email! Have a nice day!‘‘‘ part = MIMEText(content, plain, utf-8) msg.attach(part) if filename: file_name = /tmp/ + filename part = MIMEText(open(file_name, rb).read(), base64, gb2312) part["Content-Type"] = application/octet-stream basename = os.path.basename(file_name) part["Content-Disposition"] = attachment; filename=%s % basename.encode(gb2312) msg.attach(part) s = smtplib.SMTP("smtp.exmail.qq.com", timeout=305) s.login(_user, _pwd) s.sendmail(_user, areceiver.split(,) + acc.split(,), msg.as_string()) s.close() else: pass if __name__ == "__main__": export() send_email()

測試:

python /tmp/exportsql.py

查看收件箱: (也可以去垃圾箱看看)

技術分享圖片

如果覺得手動執行比較麻煩,也可以寫個計劃任務,定期導出:

crontab -e
*/5 * * * * /usr/bin/python /tmp/exportsql.py

Python導出sql語句結果到Excel