|
|
import pymysql
|
|
|
import xlwt as xlwt
|
|
|
|
|
|
|
|
|
def Toexcel(path, sql, title):
|
|
|
conn = pymysql.connect(host='47.106.183.36', port=3306,
|
|
|
user='fuchuang', password='fuchuang',
|
|
|
database='fuchuang', charset='utf8mb4')
|
|
|
curs = conn.cursor()
|
|
|
curs.execute(sql)
|
|
|
rows = curs.fetchall()
|
|
|
w = xlwt.Workbook(encoding='utf-8')
|
|
|
style = xlwt.XFStyle() # 初始化样式
|
|
|
font = xlwt.Font() # 为样式创建字体
|
|
|
font.name = "微软雅黑" # 如果是 python2 ,需要这样写 u"微软雅黑"
|
|
|
style.font = font # 为样式设置字体
|
|
|
ws = w.add_sheet("视频信息", cell_overwrite_ok=True)
|
|
|
# 将 title 作为 Excel 的列名
|
|
|
title = title.split(",")
|
|
|
for i in range(len(title)):
|
|
|
ws.write(0, i, title[i], style)
|
|
|
# 开始写入数据库查询到的数据
|
|
|
for i in range(len(rows)):
|
|
|
row = rows[i]
|
|
|
for j in range(len(row)):
|
|
|
if row[j]:
|
|
|
item = row[j]
|
|
|
ws.write(i + 1, j, item, style)
|
|
|
# 写文件完成,开始保存xls文件
|
|
|
w.save(path)
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
sql_1 = '''select * from video'''
|
|
|
Toexcel('视频信息.xls', sql_1, "id,标题,播放量,弹幕数,发布者")
|
|
|
sql_2 = '''select * from bangumi'''
|
|
|
Toexcel('番剧信息.xls', sql_2, "id,番名,播放量,评分,弹幕数")
|