python接口自动化生成excel报告

文章来源:一氧化碳中毒   发布时间:2021-6-18 10:30:52   点击数:
  

管理后台

排序优先级生成状态,生成时间

使用.order_by("record_status","-record_reporttime")

点击生成报告按钮生成html报告跟excel报告

之前html报告好像分享过了吧,反正套用的过程就是不断优化优化就是自己的了

生成excel报告源码:

#生成excel报告

defrunexcelplan(report_id,uname):

nowtime=time.strftime("%Y-%m-%d%H:%M:%S")

reportinfo=testrecord.objects.get(id=report_id)

report_excel=xlwt.Workbook()

summary_worksheet=report_excel.add_sheet("测试总结",cell_overwrite_ok=True)

#case_worksheet=report_excel.add_sheet("测试用例",cell_overwrite_ok=True)

#设置宽高

summary_worksheet.col(0).width=#设置第一列的宽

summary_worksheet.col(1).width=

summary_worksheet.col(2).width=

summary_worksheet.col(3).width=

summary_worksheet.col(4).width=

summary_worksheet.col(5).width=

summary_worksheet.row(0).set_style(xlwt.easyxf(font:height))#设置第一行的高

summary_worksheet.row(1).set_style(xlwt.easyxf(font:height))

summary_worksheet.row(2).set_style(xlwt.easyxf(font:height))

summary_worksheet.row(3).set_style(xlwt.easyxf(font:height))

style=xlwt.XFStyle()#创建一个样式对象,初始化样式

al=xlwt.Alignment()

al.horz=0x02#设置水平居中

al.vert=0x01#设置垂直居中

a2=xlwt.Borders()

a2.left=1

a2.right=1

a2.top=1

a2.bottom=1

style.alignment=al

style.borders=a2

#合并第1行到第1行的第1列到第6列

summary_worksheet.write_merge(0,0,0,5,reportinfo.record_name+测试报告,style)

summary_worksheet.write_merge(1,3,0,0,概括,style)

#B列单元格

summary_worksheet.write(1,1,测试描述,style)

summary_worksheet.write(2,1,测试人员,style)

summary_worksheet.write(3,1,测试日期,style)

#C列单元格

summary_worksheet.write(1,2,reportinfo.record_name,style)

summary_worksheet.write(2,2,uname,style)

summary_worksheet.write(3,2,nowtime,style)

#D列单元格

summary_worksheet.write(1,3,"用例总数",style)

summary_worksheet.write(2,3,"用例成功数",style)

summary_worksheet.write(3,3,"用例失败数",style)

#E列单元格

summary_worksheet.write(1,4,reportinfo.record_casecount,style)

summary_worksheet.write(2,4,reportinfo.record_success,style)

summary_worksheet.write(3,4,reportinfo.record_fail,style)

#F列单元格

summary_worksheet.write(1,5,"测试用例通过率",style)

summary_worksheet.write_merge(2,3,5,5,reportinfo.record_successrate,style)

#生成饼图

#app=xlwings.App()

#wb=app.books.active

#sht=wb.sheets.active

#chart=sht.charts.add(,10)#,10为图表放置的位置坐标。以像素为单位。

##生成图表的数据

#sht.range(A1).value=[[用例成功数,用例失败数数],[reportinfo.record_success,reportinfo.record_fail]]

#chart.set_source_data(sht.range(A1).expand())#参数为表格中的数据区域。

#chart.chart_type=5#用来设置图表类型,具体参数件下面详细说明。

#chart.api[1].ChartTitle.Text=5#用来设置图表的标题。

#wb.save(reportinfo.record_name+测试统计图)

#wb.close()

#app.quit()

reportdate=time.strftime(%F)

reporttime=time.strftime("%H点%M分%S秒")

filedir=settings.MEDIA_ROOT+"/reportexcel/"+uname

ifnotos.path.exists(filedir):

os.makedirs(filedir)

report_excel.save(filedir+"/"+reportinfo.record_name+""+reportdate+""+reporttime+".xlsx")

reportinfo.record_reporttime=nowtime#更新生成报告时间

reportinfo.record_excelreporturl="media/reportexcel/"+uname+"/"+reportinfo.record_name+""+reportdate+""+reporttime+".xlsx"

reportinfo.record_status=1

reportinfo.save()

调用excel自带的饼图还是有问题的,其实可以插入html中的饼图图片也行的。

效果如下

python对excel处理有两个xlwt跟xlwtwriter

对应的实现还是有点差异,比如设置宽高、单元格,背景色,边框,柱状图,折线图,饼图等等

预览时标签不可点收录于话题#个上一篇下一篇
转载请注明:http://www.lwblm.com/bzbk/12012.html
  • 上一篇文章:
  • 下一篇文章: