Python Script Learning Notes (iv) customizing business quality reports

Source: Internet
Author: User
Tags imagemagick python script radar

First, using the Xlsxwriter module to generate Excel tables

Installing the xlsxwriter module

pip2.7 Install Xlsxwriter

Official website:http://xlsxwriter.readthedocs.org/


Common Method Description:

Workbook (filename [options]) used to create a workbook object

Create an Excel file

Workbook = Xlsxwriter. Workbook (' chart.xlsx ')


Add_worksheet (sheetname) to add a worksheet, you can specify the sheet name in the empty number, and the default is Sheet1

Create a Sheet object

Worksheet = Workbook.add_worksheet ()


Add_format ([properties]) used to create a new format object, the parameter [properties] is a dictionary that specifies a format property such as:

Bold = Workbook.add_format ({' Bold ': True})

Format methods can also be done by formatting such as:

Bold = Workbook.add_format ()

Bold.set_bold ()


worksheet.write (row,column, ' data ') cell Row,column for writing data to a specified location, with a starting position of 0 for the coordinate index

If the position is represented by coordinates, for example (2,0) is (A3);

This is the expression "Hello" in the A1 position.

Worksheet.write (0, 0 ' hello ')


Set_row (row,height,cell_format,options) to set the properties of the row cell, row for the specified position, height to set the row height in pixels, cell_format to specify a defined Format object invocation, options to set the line hidden (hidden), level (combo rating), Examples of collapsed (folding) operations are as follows:

Sets the cell height of row 1th to 40 pixels and calls the format Cell_format .

Worksheet.set_row (0,40,cell_format)

Hide the 2nd row of cells

Worksheet.set_row (1,none,none,{'hidden': True})


Set_column (first_col,last_col,width,cell_format,options) used to set one or more column cell properties


Bold = Workbook.add_format ({' Bold ': True})

Set 0 to 1 (that is, A to B) column cells are 10 pixels wide and call bold formatting.

Worksheet.set_column (0,1,bold)

Set C-to-D column cell width to 20 pixels

Worksheet.set_column (c:d,20)

Hide E to G column cells

Worksheet.set_column (e:g,none,none,{' hidden ': 1})


insert_image (row,col,image,[options]) used to insert a picture into a specified cell

Insert a picture to specify a hyperlink to a picture

Worksheet.insert_image (' B5 ', ' img/he.jpg ', {' url ': ' http://python.org '})


Add_chart (Type: chart type) used to create a chart object

Chart styles: Areas area style, bar bar style, column column style, line style, pie pie chart style, scatter scatter style, stock stock style, radar radar style;


Creating a Column Column chart

Chart = Workbook.add_chart ({type, ' column '})

Insert a chart in cell A5

Worksheet.insert_chart (' A5 ', chart)

chart.add_series () used to add a data series to a chart

Three commonly used options:categories as the Set Chart category label range, values for setting the icon data range, line for setting chart lines properties.

Chart.add_series ({

' Categories ': ' =sheet1! $A $ $A ',

' Values ': ' =sheet1! $B $ $B ',

' line ': {' color ': ' Red '},

})


Custom Automated Business Flow report weekly

Customized website 5 channels of Traffic report weekly, through the Xlsxwriter module to write traffic data to Excel documents, and automatically calculate the average weekly traffic, and then generate data reports.

#!/usr/local/python27/bin/python2.7#coding: utf-8import sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') )   #用于解决windows转linux出现的编码问题import  xlsxwriterworkbook = xlsxwriter. Workbook (' chart.xlsx ') worksheet = workbook.add_worksheet () #创建图表对象, defines the chart type. Chart = workbook.add_chart ({' type ': ' column '}) title = [' Business name ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ' , ' Saturday ', ' Sunday ', ' Average traffic ']buname = [' business official website ', ' News Center ', ' Shopping channel ', ' Sports channel ', ' Parent-child channel ']data = [     [150,152,158,149,155,145,148],    [89,88,95,93,98,100,99],     [201,200,198,175,170,198,195],    [75,77,78,78,74,70,79],    [ 88,85,87,90,93,88,84],]format = workbook.add_format ()   #定义格式 for use with Format.set_border (1) in part-unit style Format_title = workbook.add_format () Format_title.set_border (1) format_title.set_bg_color (' #cccccc ') Format_ave = workbook.add_format () Format_ave.set_border (1) format_avE.set_num_format (' 0.00 ') #使用行写入第一行业务名称及标题worksheet. Write_row (' A1 ', Title,format_title) # Write the channel title Worksheet.write_column (' A2 ', Buname,format) #从B2开始写入数据项worksheet. Write_row (' B2 ', data[0], starting from the second row of the first column with a column write 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): #这里是在求周平均值, The data of the current row  = b:h the value of the current row and then the average Worksheet.write_formula (' I ' +cur_row,  ' = average (B ' +cur_row+ ': H ' + cur_row+ ', Format_ave) chart.add_series ({#这里是在工作表Sheet1中指定该图表类别的标签范围, from B1 to H1, was taken from Monday to Sunday. ' Categories ':  ' =sheet1! $B $: $H $ ', #这里是设置图表数据的范围, from B current data row to H current data row. ' Values ':  ' =sheet1! $B $ ' +cur_row+ ': $H $ ' +cur_row, ' line '  : {' color ': ' Black '}, #引用了业务名称为图列项 ' name '  :  ' =sheet1! $A $ ' +cur_row,}) For row in range (2,7): Chart_series (str (ROW)) #定义图表样式, Each of these values is a different style. Chart.set_style (2) #定义图表的大小chart. Set_size ({' width ': 577, ' height ': 287}) #设置图表上方的大标题chart. Set_title ({' NamE ': ' Business Flow Weekly Chart '}) #设置图表y轴的小标题chart. Set_y_axis ({' name ': ' MB/s '}) #插入图表到A8的位置worksheet. Insert_chart (' A8 ', chart) Workbook.close ()


Second, the realization of TCP detection target server route path


Scapy modules require tcpdump support, and generating reports requires Graphviz,imagemagick image processing package support.

Yum Install tcpdump Graphviz imagemagick-y

Installing the Scapy module

pip2.7 Install Scapy

#!/usr/local/python27/bin/python2.7#coding: utf-8 import os,sys,time,subprocessimport  warnings,logging  #屏蔽无用的告警信息  warnings.filterwarnings ("Ignore", category=deprecationwarning) # Shield module IPV6 redundant alarm logging.getlogger ("Scapy.runtime"). SetLevel (Logging. ERROR) from scapy.all import traceroute domains = raw_input (" Please  input one or more ip/domain:  ") #切片空格, this is equivalent to putting domains content in the target list, which can be target[number] called; Target = domains.split ('   ')  dport = [80] if len (target)  >=  1 and target[0] != ': #启动路由跟踪         res,unans  = traceroute (target,dport=dport,retry=-2) #res保存的是路由跟踪抓的报文数据, generates an. svg file by using the Res.graph method;                 res.graph (target= ">  test.svg ")         time.sleep (1) #调用shell命To convert SVG to png,svg files can be opened in the browser;                 subprocess. Popen ("/usr/bin/convert test.svg test.png", Shell=true) else:    print  "IP/ Domain number of errors,exit "

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/73/6D/wKiom1X9WErD2qv1AAH9xMmStUo554.jpg "title=" 11111. PNG "alt=" wkiom1x9werd2qv1aah9xmmstuo554.jpg "/>

This article from "Breakthrough Comfort zone" blog, reproduced please contact the author!

Python Script Learning Notes (iv) customizing business quality reports

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.