#!/usr/bin/env python #coding: Utf-8 Import Xlsxwriter Workbook = Xlsxwriter. Workbook (' wugui.xlsx ') #创建一个Excel文件 Worksheet = Workbook.add_worksheet () #创建一个工作表对象 chart = Workbook.add_chart ({' type ':' column '}) #创建一个图表对象 #定义数据表开头列表 title = [u ' Business name ', U ' Monday ', U ' Tuesday ', U ' Wednesday ', U ' Thursday ', U ' Friday ', U ' Saturday ', U ' Sunday ', U ' average flow '] Buname = [u ' Turtle Ops official website ', U ' turtle 1 ', U ' turtle 2 ', U ' turtle 3 ', U ' Turtle 4 '] #定义频道名称 #定义5频道一周七天数据列表 data = [ [134,432,348,349,565,435,158], [176,168,94,53,68,700,91], [251,265,154,165,540,154,135], [77,76,75,74,73,73], [81,82,83,84,91,83,82], ] Format=workbook.add_format () #定义format格式对象 Format.set_border (1) format #定义format对象单元格边框加粗 (1 pixels) Format_title=workbook.add_format () #定义format_title格式对象 Format_title.set_border (1) format #定义format_title对象单元格边框加粗 (1 pixels) Format_title.set_bg_color (' #cccccc ') #定义format_title对象单元格背景颜色为 format of ' CCCCCC ' Format_title.set_align (' center ') #定义format_title对象单元格剧中对齐的格式 Format_title.set_bold () #定义format_title对象单元格内容加粗的格式 Format_ave=workbook.add_format () #定义format_ave格式对象 Format_ave.set_border (1) format #定义format_ave对象单元格边框加粗 (1 pixels) Format_ave.set_num_format (' 0.00 ') #定义format_ave对象单元格数字类别显示格式 #下面分别以行或列写入方式将标题, business name, traffic data written to the original cell, referencing different format objects Worksheet.write_row (' A1 ', title,format_title) Worksheet.write_column (' A2 ', Buname,format) Worksheet.write_row (' B2 ', Data[0],format) Worksheet.write_row (' B3 ', Data[1],format) Worksheet.write_row (' B4 ', Data[2],format) Worksheet.write_row (' B5 ', Data[3],format) Worksheet.write_row (' B6 ', Data[4],format) #定义图表数据系列函数 def chart_series (Cur_row): Worksheet.write_formula (' I ' +cur_row, ' =average (B ' +cur_row+ ': H ' +cur_row+ ') ', Format_ave) #计算 (AVERAGE function) #频道周平均流量 chart.add_series ({ ' categories ': ' =sheet1! $B $: $H ', #将 ' Monday to Sunday ' as an icon data label (x-axis) ' Values ': ' =sheet1! $B $ ' +cur_row+ ': $H $ ' +cur_row, #频道一周所有数据操作 for the data region ' line ':{' Color ':' Black '}, #线条颜色定义为black(Black) ' name ' : ' sheet1! $A $ ' +cur_row, }) For row in range (2,7): #数据域以第2 Line chart data series function call Chart_series (str (ROW)) #chart. Set_table () #设置X轴表格格式, this example does not enable #chart. Set_style (#设置图表样式), this example does not enable chart.set_size ( ' width ' : 577, ' height ' : 287 } ) #设置图表大小 Chart.set_title ({' name ': u ' Business Traffic Weekly Report '}) #设置图表 ( Top) Big title Chart.set_y_axis ({' name ': ' MB/s '}) #设置Y轴(left)Small Icons ( ' A8 ' Span class= "crayon-i" style= "Font-family:inherit;height:inherit;" >, chart Span class= "crayon-h" style= "Font-family:inherit;height:inherit;color:rgb (0,111,224);" > #在A8单元格插入图表 Workbook.close () #关闭Excel文档 |