#!/usr/bin/python3
# coding:utf-8
import pymysql
import random
cnn=pymysql.connect(host="localhost",user="root",password="mysql",database="test")
cur=cnn.cursor()
cur.execute("select * from sc")
data=cur.fetchall()
cur.close()
cnn.close()
from datetime import datetime
today=datetime.today()
today_str=today.strftime("%Y%m%d")
print(type(today_str))
assert type(today_str)==str
import pandas as pd
sc=list()
for i in data:
Sid=i[0]
Cid=i[1]
score=i[2]
row={"Sid":Sid,"Cid":Cid,"score":score}
sc.append(row)
assert type(sc)==type(list())
SC=pd.DataFrame(sc)
sheet=pd.DataFrame(SC.groupby("Sid")["score"].sum())
sheet.reset_index(level=0,inplace=True)
writer=pd.ExcelWriter(r"/home/python/Desktop/python_script/xml_dir/%s_%d.xls"%(today_str,random.randint(1,1000)*random.randint(1,1000)),engine="xlsxwriter")
sheet.to_excel(writer,sheet_name="Sheet1",startrow=1,header=False,index=False)
workbook=writer.book
worksheet=writer.sheets["Sheet1"]
header_format=workbook.add_format({"bold":True,"text_wrap":True,"align":"center","valign":"vcenter","fg_color":"#96DCCD","border":1})
for col_num,value in enumerate(sheet.columns.values):
worksheet.write(0,col_num,value,header_format)
writer.save()
自动连接MySQL数据库抓取数据并生成一定格式的Excel报表(Python实现)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...