用mysql+python自动生成统计报表

import xlrd
import xlwt
from xlutils.copy import copy
import pymysql

database=pymysql.connect("ip","username","pwd","sqlname",charset='utf8')

#初始化一个指针
cursor=database.cursor()
#需要查询的内容   GROUP BY company表示以公司名进行分组
sql="SELECT company,COUNT(company),SUM(weight),SUM(weight*price)FROM  data  GROUP BY company"
#运行sql语句
cursor.execute(sql)
#结果存储在result里面
result=cursor.fetchall()

#读取数据
for i in result:
	if i[0]=='张三粮配':
		a_num=i[1]
		a_weight=i[2]
		a_total_price=i[3]
	elif i[0]=='李四粮食':
		b_num=i[1]
		b_weight=i[2]
		b_total_price=i[3]
	elif i[0]=='王五小麦':
		c_num=i[1]
		c_weight=i[2]
		c_total_price=i[3]
	elif i[0]=='赵六麦子专营':
		d_num=i[1]
		d_weight=i[2]
		d_total_price=i[3]

tem_excel=xlrd.open_workbook('path'.formatting_info=True)
tem_sheet=tem_excel.sheet_by_index(0)

new_excel=copy(tem_excel)
new_sheet=new_excel.get_sheet(0)

style=xlwt.XFStyle()

font=xlwt.Font()
font.name='微软雅黑'
font.bold=True
#字体的高度等于  字号*20
font.height=360

style.font=font
borders=xlwt.Borders()
borders.top=xlwt.Borders.THIN
borders.bottom=xlwt.Borders.THIN
borders.left=xlwt.Borders.THIN
borders.right=xlwt.Borders.THIN
style.borders=borders

#对齐
alignment=xlwt.Alignment()
#水平
alignment.horz=xlwt.Alignment.HORZ_CENTER
#竖直
alignment.vert=xlwt.Alignment.VERT_CENTER
style.alignment=alignment


new_sheet.write(2,1,len(a_weight),style)
new_sheet.write(2,2,round(sum(a_weight),2),style)
new_sheet.write(2,2,round(sum(a_total_price),2),style)
new_sheet.write(2,1,len(b_weight),style)
new_sheet.write(2,2,round(sum(b_weight),2),style)
new_sheet.write(2,2,round(sum(b_total_price),2),style)
new_sheet.write(2,1,len(c_weight),style)
new_sheet.write(2,2,round(sum(c_weight),2),style)
new_sheet.write(2,2,round(sum(c_total_price),2),style)
new_sheet.write(2,1,len(d_weight),style)
new_sheet.write(2,2,round(sum(d_weight),2),style)
new_sheet.write(2,2,round(sum(d_total_price),2),style)

new_excel.save('path')
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值