Python 使用xlsxwriter绘制Excel表格
最近在统计资产,正好看到了xlsxwriter这个表格生成模块,借此机会,熟悉一下,写点有趣的小案例,一开始想使用C++ QT图形化开发一套自动化运维平台,但后来发现不仅消耗时间而且需要解决QT Qssh远程模块的一些问题,后来没有使用QT做,xlsxwriter模块来做非常的简单,所以使用它。
上班不能摸鱼,我要努力摸鱼,不不不,学习,不然今天我摸鱼,明天鱼摸我。。
绘制磁盘统计(封装表格)
封装前需要先来找出规律: 先找到表格生成坐标与大小之间的比值关系,这是第一步。
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_line.xlsx')
worksheet3 = workbook.add_worksheet("系统磁盘统计")
merge_format = workbook.add_format({
'bold': True,
'border': 1,
'align': 'center',
'valign': 'vcenter',
'fg_color': '#EEAEEE',
})
worksheet3.merge_range('A9:B12', '192.168.1.1', merge_format)
# -------------------------------------------------------------------------
head = ["IP地址","IP地址","路径","总容量","剩余容量","利用率"]
merge_format1 = workbook.add_format({
'bold': True,
'border': 1,
'align': 'center',#水平居中
'valign': 'vcenter',#垂直居中
'fg_color': '#AEEEEE',#颜色填充
})
worksheet3.write_row("A8:B12",head,merge_format1) #显示表头
worksheet3.merge_range('A8:B8',"IP地址",merge_format1) # 合并表头(合并第一个元素)
data1 = ["/etc/system/winsss/aaa","/proc/","/sys","/abc/lyshark"]
merge_format2 = workbook.add_format\
({'bold': True,'border': 1,'valign': 'vcenter','fg_color': '#D7E4BC','align': 'center'})
worksheet3.write_column("C9",data1,merge_format2)
worksheet3.set_column("C9:C9",30)
data2 = ["1024GG","2048GB","111GB","1111GB"]
merge_format3 = workbook.add_format\
({'bold': True,'border': 1,'valign': 'vcenter','fg_color': '#D7E4BC','align': 'center'})
worksheet3.write_column("D9",data2,merge_format3)
worksheet3.set_column("D9:D9",20)
data3 = ["1024GG","2048GB","111GB","22GB"]
merge_format4 = workbook.add_format\
({'bold': True,'border': 1,'valign': 'vcenter','fg_color': '#D7E4BC','align': 'center'})
worksheet3.write_column("E9",data3,merge_format4)
worksheet3.set_column("E9:E9",20)
data4= ["10%","50%","20%","33%"]
merge_format5 = workbook.add_format\
({'bold': True,'border': 1,'valign': 'vcenter','fg_color': '#D7E4BC','align': 'center'})
worksheet3.write_column("F9",data4,merge_format5)
worksheet3.set_column("F9:F9",20)
'''
val =
[
["IP地址","IP地址","路径","总容量","剩余容量","利用率"],
["/etc/","/proc","/user","/test","lyshark"],
["1GB","2GB","3GB","4GB","5GB"],
["1GB","2GB","3GB","4GB","5GB"],
["10%","10%","10%","10%","10%"]
]
SetDiskTable(address,val)
'''
workbook.close()
接着对其进行封装:找到规律后就要对其进行封装,封装成一个可直接调用的方法。
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_line.xlsx')
worksheet = workbook.add_worksheet("系统磁盘统计")
def CreateDiskTable(address,data,section):
merge_format = workbook.add_format(
{'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#EEAEEE'})
header_count = len(data[1])
merge_format1 = workbook.add_format(
{'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#AEEEEE'})
# 根据磁盘路径计算出表格大小
header_range = "A{}:B{}".format(section,section+header_count)
worksheet.write_row(header_range, data[0], merge_format1) # 显示表头
# 计算合并表头偏移
header_merge_range = "A{}:B{}".format(section,section)
worksheet.merge_range(header_merge_range, "IP地址", merge_format1) # 合并表头(合并第一个元素)
# 计算出地址所占总单元格大小
address_merge_range = "A{}:B{}".format(section+1,section+header_count)
worksheet.merge_range(address_merge_range, address , merge_format) #需要计算出来,根据传入分区数量
# 通过计算得到磁盘路径所对应到表中的位置
merge_format2 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "C{}".format(section+1)
worksheet.write_column(index_range, data[1], merge_format2)
index_range = "C{}:C{}".format(section+1,section+1)
worksheet.set_column(index_range, 30)
# 计算出总容量对应到表中的位置
merge_format3 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "D{}".format(section + 1)
worksheet.write_column(index_range, data[2], merge_format3)
index_range = "D{}:D{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 计算出剩余容量对应到表中的位置
merge_format4 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "E{}".format(section + 1)
worksheet.write_column(index_range, data[3], merge_format4)
index_range = "E{}:E{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 计算出利用率对应到表中的位置
merge_format5 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "F{}".format(section + 1)
worksheet.write_column(index_range, data[4], merge_format5)
index_range = "F{}:F{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 返回计算后的表格的下两个单元的实际偏移位置
return section + header_count + 3
if __name__ == "__main__":
val = \
[
["IP地址", "IP地址", "磁盘路径", "总容量", "剩余容量", "利用率"],
["/etc/system/winsss/aaa", "/proc/", "/sys", "/abc/lyshark"],
["1024GG", "2048GB", "111GB", "1111GB"],
["1024GG", "2048GB", "111GB", "22GB"],
["10%", "50%", "20%", "33%"]
]
ref = CreateDiskTable("192.168.1.1",val,8)
print(ref)
ref = CreateDiskTable("192.168.1.1", val, ref)
CreateDiskTable("192.168.1.1", val, ref)
workbook.close()
继续更新一下
import xlsxwriter
import json,os,sys,math
import argparse
import paramiko
workbook = xlsxwriter.Workbook('disk.xlsx')
worksheet = workbook.add_worksheet("系统磁盘统计")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def BatchCMD(address,username,password,port,command):
try:
ssh.connect(hostname=address,username=username,password=password,port=port,timeout=2)
stdin , stdout , stderr = ssh.exec_command(command)
result = stdout.read()
if len(result) != 0:
result = str(result).replace("\\n", "\n")
result = result.replace("b'", "").replace("'", "")
return result
else:
return None
except Exception:
return None
def GetAllDiskSpace(address,username,password,port):
ref = []
cmd_dict = {"Linux\n" : "df | grep -v 'Filesystem' | awk '{print $6 \":\" $3 \":\" $4 \":\" $5}'",
"AIX\n" : "df | grep -v 'Filesystem' | awk '{print $4 \":\" $7}'"
}
os_version = BatchCMD(address,username,password,port,"uname")
for version,run_cmd in cmd_dict.items():
if(version == os_version):
os_ref = BatchCMD(address,username,password,port,run_cmd)
ref_list= os_ref.split("\n")
for each in ref_list:
if each != "":
ref_list = []
ref_list = str(each.split(":"))
ref.append(eval(ref_list))
return ref
def CreateDiskTable(address,data,section):
merge_format = workbook.add_format(
{'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#EEAEEE'})
header_count = len(data[1])
merge_format1 = workbook.add_format(
{'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#AEEEEE'})
# 根据磁盘路径计算出表格大小
header_range = "A{}:B{}".format(section,section+header_count)
worksheet.write_row(header_range, data[0], merge_format1) # 显示表头
# 计算合并表头偏移
header_merge_range = "A{}:B{}".format(section,section)
worksheet.merge_range(header_merge_range, "IP地址", merge_format1) # 合并表头(合并第一个元素)
# 计算出地址所占总单元格大小
address_merge_range = "A{}:B{}".format(section+1,section+header_count)
worksheet.merge_range(address_merge_range, address , merge_format) #需要计算出来,根据传入分区数量
# 通过计算得到磁盘路径所对应到表中的位置
merge_format2 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "C{}".format(section+1)
worksheet.write_column(index_range, data[1], merge_format2)
index_range = "C{}:C{}".format(section+1,section+1)
worksheet.set_column(index_range, 30)
# 计算出总容量对应到表中的位置
merge_format3 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "D{}".format(section + 1)
worksheet.write_column(index_range, data[2], merge_format3)
index_range = "D{}:D{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 计算出剩余容量对应到表中的位置
merge_format4 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "E{}".format(section + 1)
worksheet.write_column(index_range, data[3], merge_format4)
index_range = "E{}:E{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 计算出利用率对应到表中的位置
merge_format5 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "F{}".format(section + 1)
worksheet.write_column(index_range, data[4], merge_format5)
index_range = "F{}:F{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 返回计算后的表格的下两个单元的实际偏移位置
return section + header_count + 3
if __name__ == "__main__":
'''
val = \
[
["IP地址", "IP地址", "磁盘路径", "总容量", "剩余容量", "利用率"],
["/etc/system/winsss/aaa", "/proc/", "/sys", "/abc/lyshark"],
["1024GG", "2048GB", "111GB", "1111GB"],
["1024GG", "2048GB", "111GB", "22GB"],
["10%", "50%", "20%", "33%"]
]
ref = CreateDiskTable("192.168.1.1",val,3)
ref = CreateDiskTable("192.168.1.2", val, ref)
CreateDiskTable("192.168.1.3", val, ref)
'''
ref = GetAllDiskSpace("192.168.191.4","root","1233",22)
# 恒矩阵转树矩阵
ref_xor = list ( map(list,zip(*ref)) )
header = ["IP地址", "IP地址", "磁盘路径", "已使用", "剩余容量", "利用率"]
ref_xor.insert(0,header)
print(ref_xor)
CreateDiskTable("192.168.191.4", ref_xor, 2)
workbook.close()
增加SSH功能遵循调用规范:
import xlsxwriter
import paramiko
import os,sys,datetime
disk_name = "disk_" + str(datetime.datetime.now()).split(" ")[0] + ".xlsx"
workbook = xlsxwriter.Workbook(disk_name)
worksheet = workbook.add_worksheet("系统磁盘统计")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 封装远程命令执行
def BatchCMD(address,username,password,port,command):
try:
ssh.connect(hostname=address,username=username,password=password,port=port,timeout=2)
stdin , stdout , stderr = ssh.exec_command(command)
result = stdout.read()
if len(result) != 0:
result = str(result).replace("\\n", "\n")
result = result.replace("b'", "").replace("'", "")
return result
else:
return None
except Exception:
return None
# 获取磁盘数据
def GetAllDiskSpace(address,username,password,port):
ref = []
cmd_dict = {"Linux\n" : "df | grep -v 'Filesystem' | awk '{print $6 \":\" $3 \":\" $4 \":\" $5}'",
"AIX\n" : "df | grep -v 'Filesystem' | awk '{print $7 \":\" $5 \":\" $3 \":\" $4}'"
}
os_version = BatchCMD(address,username,password,port,"uname")
for version,run_cmd in cmd_dict.items():
if(version == os_version):
os_ref = BatchCMD(address,username,password,port,run_cmd)
ref_list= os_ref.split("\n")
for each in ref_list:
if each != "":
ref_list = str(each.split(":"))
ref.append(eval(ref_list))
return ref
# 封装的绘制主机表格方法
def CreateDiskTable(address,data,section):
merge_format = workbook.add_format(
{'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#EEAEEE'})
header_count = len(data[1])
merge_format1 = workbook.add_format(
{'bold': True, 'border': 1, 'align': 'center', 'valign': 'vcenter', 'fg_color': '#AEEEEE'})
# 根据磁盘路径计算出表格大小
header_range = "A{}:B{}".format(section,section+header_count)
worksheet.write_row(header_range, data[0], merge_format1) # 显示表头
# 计算合并表头偏移
header_merge_range = "A{}:B{}".format(section,section)
worksheet.merge_range(header_merge_range, "巡检IP地址", merge_format1) # 合并表头(合并第一个元素)
# 计算出地址所占总单元格大小
address_merge_range = "A{}:B{}".format(section+1,section+header_count)
worksheet.merge_range(address_merge_range, address , merge_format) #需要计算出来,根据传入分区数量
# 通过计算得到磁盘路径所对应到表中的位置
merge_format2 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC'})
index_range = "C{}".format(section+1)
worksheet.write_column(index_range, data[1], merge_format2)
index_range = "C{}:C{}".format(section+1,section+1)
worksheet.set_column(index_range, 30)
# 计算出总容量对应到表中的位置
merge_format3 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "D{}".format(section + 1)
worksheet.write_column(index_range, data[2], merge_format3)
index_range = "D{}:D{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 计算出剩余容量对应到表中的位置
merge_format4 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "E{}".format(section + 1)
worksheet.write_column(index_range, data[3], merge_format4)
index_range = "E{}:E{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 计算出利用率对应到表中的位置
merge_format5 = workbook.add_format(
{'bold': True, 'border': 1, 'valign': 'vcenter', 'fg_color': '#D7E4BC', 'align': 'center'})
index_range = "F{}".format(section + 1)
worksheet.write_column(index_range, data[4], merge_format5)
index_range = "F{}:F{}".format(section + 1, section + 1)
worksheet.set_column(index_range, 20)
# 返回计算后的表格的下两个单元的实际偏移位置
return section + header_count + 3
if __name__ == "__main__":
with open("./unix_disk.db","r") as fp:
fp_file = fp.readlines()
ref_index = 2
for each in fp_file:
data = eval(each)
ref = GetAllDiskSpace(data[0], data[2], data[3], 22)
try:
print("[+] 正在绘制 {} 表格数据,这是一台: {} 主机".format(data[0],data[1]))
# 恒矩阵转树矩阵
ref_xor = list(map(list, zip(*ref)))
header = ["IP地址", "IP地址", "磁盘路径", "已使用", "剩余容量", "利用率"]
# 将表头追加到矩阵头部
ref_xor.insert(0, header)
# 绘制表格,ref_index 递归自身,每次定位到下一个需要更新的表
ref_index = CreateDiskTable(data[0], ref_xor, ref_index)
except Exception:
pass
workbook.close()
绘制内存统计图(柱状图)
简单的案例: 主要统计前十计数信息,先远程拉取数据,然后内部矩阵排序,关联柱状图前十个元素,即可。
import xlsxwriter
workbook = xlsxwriter.Workbook("xxx.xlsx")
worksheet = workbook.add_worksheet()
headings = ["主机地址", "CPU利用率", "内存利用率","交换分区"]
data = [["192.168.1.100", 88, 36,66], ["192.168.1.200", 98, 89,66], ["192.168.1.220", 88, 100,32]]
# 循环添加模拟数据
for i in range(1,100):
s = ["192.168.1.{}".format(i),i,i,i]
data.append(s)
# 定义表格样式
head_style = workbook.add_format({"bold": True, "align": "center", "font": 13})
# 逐条写入数据
worksheet.write_row("A1", headings, head_style)
for i in range(0, len(data)):
worksheet.write_row("A{}".format(i + 2), data[i])
# 添加柱状图
chart = workbook.add_chart({"type": "column"})
chart.add_series({
"name": "=Sheet1!$B$1", # 图例项
"categories": "=Sheet1!$A$2:$A$10", # X轴 Item名称
"values": "=Sheet1!$B$2:$B$10" # X轴Item值
})
chart.add_series({
"name": "=Sheet1!$C$1",
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$C$2:$C$10"
})
chart.add_series({
"name": "=Sheet1!$D$1",
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$D$2:$D$10"
})
# 添加柱状图标题
chart.set_title({"name": "性能统计柱状图"})
chart.set_style(12)
# 在G2处绘制
worksheet.insert_chart("G2", chart)
workbook.close()
先来获取到CPU数据: 通过SSH拉取CPU数据,然后按照最后一个值进行排序,由大到小以此放入二维数组中。
import xlsxwriter
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def BatchCMD(address,username,password,port,command):
try:
ssh.connect(hostname=address,username=username,password=password,port=port,timeout=2)
stdin , stdout , stderr = ssh.exec_command(command)
result = stdout.read()
if len(result) != 0:
result = str(result).replace("\\n", "\n")
result = result.replace("b'", "").replace("'", "")
return result
else:
return None
except Exception:
return None
def GetMemLoad(address,username,password,port):
ref = []
cmd_dict = {"Linux\n" : "free | grep \"Mem\" | awk '{print $2 \":\" $4}'",
"AIX\n" : "svmon | grep \"memory\" | awk '{print $2 \":\" $4}'"
}
# aix 总大小、剩余大小 需要x4
os_version = BatchCMD(address,username,password,port,"uname")
for version,run_cmd in cmd_dict.items():
if(version == os_version):
os_ref = BatchCMD(address,username,password,port,run_cmd)
ref_list= os_ref.split("\n")
for each in ref_list:
if each != "":
print(each)
return ref
def GetCPULoad(address,username,password,port):
ref = []
# 用户占用,系统占用,空闲
cmd_dict = {"Linux\n" : "vmstat | tail -n 1 | awk '{print $13 \":\" $14 \":\" $15}'",
"AIX\n" : "vmstat | tail -n 1 | awk '{print $14 \":\" $15 \":\" $16}'"
}
os_version = BatchCMD(address,username,password,port,"uname")
for version,run_cmd in cmd_dict.items():
if(version == os_version):
os_ref = BatchCMD(address,username,password,port,run_cmd)
ref_list= os_ref.split("\n")
for each in ref_list:
if each != "":
ref = each.split(":")
ref[2] = str(100 - int(ref[2]))
ref.insert(0,address)
return ref
# 排序函数,以第三列为条件排列
def takeSecond(elem):
return int(elem[3])
if __name__ == "__main__":
workbook = xlsxwriter.Workbook("test1111.xlsx")
worksheet = workbook.add_worksheet()
# 设置头部数据
headings = ["主机地址", "CPU内核态", "CPU用户态", "总利用率"]
head_style = workbook.add_format({"bold": True, "align": "center" ,"fg_color":"#D7E4BC"})
worksheet.write_row("A1", headings, head_style)
# 设置头部列宽
worksheet.set_column("A1:D1",15)
# 统计数据并排序
cpu_data = []
with open("./unix_disk.db","r") as fp:
fp_file = fp.readlines()
for each in fp_file:
try:
data = eval(each)
ref = GetCPULoad(data[0], data[2], data[3], 22)
if(len(ref) !=0):
cpu_data.append(ref)
else:
continue
except Exception:
pass
# 排序,统计第三列数据,将最大的放在最前面,以此向下
cpu_data.sort(key=takeSecond, reverse=True)
print(cpu_data)
for i in range(0, len(cpu_data)):
worksheet.write_row("A{}".format(i + 2), cpu_data[i])
workbook.close()
将绘图方法与SSH结合: 运行后即可统计出机器的所有CPU数据,并将排名前十的机器使用情况列出来。
import xlsxwriter
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
workbook = xlsxwriter.Workbook("test333.xlsx")
worksheet = workbook.add_worksheet()
def BatchCMD(address,username,password,port,command):
try:
ssh.connect(hostname=address,username=username,password=password,port=port,timeout=2)
stdin , stdout , stderr = ssh.exec_command(command)
result = stdout.read()
if len(result) != 0:
result = str(result).replace("\\n", "\n")
result = result.replace("b'", "").replace("'", "")
return result
else:
return None
except Exception:
return None
def GetCPULoad(address,username,password,port):
ref = []
# 用户占用,系统占用,空闲
cmd_dict = {"Linux\n" : "vmstat | tail -n 1 | awk '{print $13 \":\" $14 \":\" $15}'",
"AIX\n" : "vmstat | tail -n 1 | awk '{print $14 \":\" $15 \":\" $16}'"
}
os_version = BatchCMD(address,username,password,port,"uname")
for version,run_cmd in cmd_dict.items():
if(version == os_version):
os_ref = BatchCMD(address,username,password,port,run_cmd)
ref_list= os_ref.split("\n")
for each in ref_list:
if each != "":
ref = each.split(":")
ref[2] = str(100 - int(ref[2]))
ref.insert(0,address)
ref[1] = int(ref[1])
ref[2] = int(ref[2])
ref[3] = int(ref[3])
return ref
# 排序函数,以第三列为条件排列
def takeSecond(elem):
return int(elem[3])
# 添加柱状统计图
def AddGraphical():
# 添加柱状图
chart = workbook.add_chart({"type": "column"})
chart.add_series({
"name": "=Sheet1!$B$1", # 图例项(也就是CPU内核态)
"categories": "=Sheet1!$A$2:$A$10", # X轴 Item名称
"values": "=Sheet1!$B$2:$B$10" # X轴Item值
})
chart.add_series({
"name": "=Sheet1!$C$1",
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$C$2:$C$10"
})
chart.add_series({
"name": "=Sheet1!$D$1",
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$D$2:$D$10"
})
# 添加柱状图标题
chart.set_title({"name": "CPU 性能统计柱状图"})
# chart.set_style(8)
chart.set_x_axis({
'major_gridlines': {
'visible': True,
'line': {'width': 1.25, 'dash_type': 'dash'}
},
})
chart.set_size({'width': 900, 'height': 500})
chart.set_legend({'position': 'top'})
chart.set_table({'show_keys': True})
# 在F2处绘制
worksheet.insert_chart("F2", chart)
if __name__ == "__main__":
# 设置头部数据
headings = ["主机地址", "CPU内核态", "CPU用户态", "总利用率"]
head_style = workbook.add_format({"bold": True, "align": "center" ,"fg_color":"#D7E4BC"})
worksheet.write_row("A1", headings, head_style)
# 设置头部列宽
worksheet.set_column("A1:D1",15)
# 统计数据并排序
cpu_data = []
with open("./unix_disk.db","r") as fp:
fp_file = fp.readlines()
for each in fp_file:
try:
data = eval(each)
ref = GetCPULoad(data[0], data[2], data[3], 22)
if(len(ref) !=0):
print(ref)
cpu_data.append(ref)
else:
continue
except Exception:
pass
# 排序,统计第三列数据,将最大的放在最前面,以此向下
cpu_data.sort(key=takeSecond, reverse=True)
print(cpu_data)
# 将数据批量添加到表格中
for i in range(0, len(cpu_data)):
worksheet.write_row("A{}".format(i + 2), cpu_data[i])
# 开始绘图
AddGraphical()
workbook.close()
收集主机负载(条形图)
制作Excel绘图:
import xlsxwriter
workbook = xlsxwriter.Workbook("xxx.xlsx")
worksheet = workbook.add_worksheet()
headings = ["获取时间", "1分钟负载", "5分钟负载","15分钟负载"]
data = [["12:01", 0.05, 0.7,0.006], ["12:02", 0.5, 0.08,0.06], ["12:03", 0.7, 1,2.1]]
# 定义表格样式
head_style = workbook.add_format({"bold": True, "align": "center" ,"fg_color":"#D7E4BC"})
worksheet.set_column("A1:D1",15)
# 逐条写入数据
worksheet.write_row("A1", headings, head_style)
for i in range(0, len(data)):
worksheet.write_row("A{}".format(i + 2), data[i])
# 添加条形图,显示前十个元素
chart = workbook.add_chart({"type": "line"})
chart.add_series({
"name": "=Sheet1!$B$1", # 图例项
"categories": "=Sheet1!$A$2:$A$10", # X轴 Item名称
"values": "=Sheet1!$B$2:$B$10" # X轴Item值
})
chart.add_series({
"name": "=Sheet1!$C$1",
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$C$2:$C$10"
})
chart.add_series({
"name": "=Sheet1!$D$1",
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$D$2:$D$10"
})
# 添加柱状图标题
chart.set_title({"name": "负载统计条形图"})
# chart.set_style(8)
chart.set_size({'width': 1000, 'height': 500})
chart.set_legend({'position': 'top'})
# 在F2处绘制
worksheet.insert_chart("F2", chart)
workbook.close()
封装SSH执行模块: 封装上方的绘图模块,与SSH模块实现图形绘制,收集十分钟负载。
import xlsxwriter
import paramiko
import datetime
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
workbook = xlsxwriter.Workbook("load_avg.xlsx")
worksheet = workbook.add_worksheet()
def BatchCMD(address,username,password,port,command):
try:
ssh.connect(hostname=address,username=username,password=password,port=port,timeout=2)
stdin , stdout , stderr = ssh.exec_command(command)
result = stdout.read()
if len(result) != 0:
result = str(result).replace("\\n", "\n")
result = result.replace("b'", "").replace("'", "")
return result
else:
return None
except Exception:
return None
def GetCPUAvg(address,username,password,port):
ref = []
cmd_dict = {"Linux\n" : "cat /proc/loadavg | awk '{print $1 \":\" $2 \":\" $3}'",
"AIX\n" : "vmstat | tail -n 1 | awk '{print $14 \":\" $15 \":\" $16}'"
}
os_version = BatchCMD(address,username,password,port,"uname")
for version,run_cmd in cmd_dict.items():
if(version == os_version):
os_ref = BatchCMD(address,username,password,port,run_cmd)
ref_list= os_ref.split("\n")
for each in ref_list:
if each != "":
ref = each.split(":")
str_to_date = datetime.datetime.now()
now_date = str(str_to_date.minute) + ":" + str(str_to_date.second)
ref.insert(0,now_date)
ref[1] = float(ref[1])
ref[2] = float(ref[2])
ref[3] = float(ref[3])
return ref
# 绘制聚合图形
def AddGraphical(data):
#data = [["12:01", 0.05, 0.7, 0.006], ["12:02", 0.5, 0.08, 0.06], ["12:03", 0.7, 1, 2.1]]
# 定义表格样式
head_style = workbook.add_format({"bold": True, "align": "center", "fg_color": "#D7E4BC"})
worksheet.set_column("A1:D1", 15)
# 逐条写入数据
headings = ["获取时间", "1分钟负载", "5分钟负载", "15分钟负载"]
worksheet.write_row("A1", headings, head_style)
for i in range(0, len(data)):
worksheet.write_row("A{}".format(i + 2), data[i])
# 添加条形图,显示前十个元素
chart = workbook.add_chart({"type": "line"})
chart.add_series({
"name": "=Sheet1!$B$1", # 图例项
"categories": "=Sheet1!$A$2:$A$10", # X轴 Item名称
"values": "=Sheet1!$B$2:$B$10" # X轴Item值
})
chart.add_series({
"name": "=Sheet1!$C$1", # 第一个线条(图例)
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$C$2:$C$10"
})
chart.add_series({
"name": "=Sheet1!$D$1", # 第二个线条(图例)
"categories": "=Sheet1!$A$2:$A$10",
"values": "=Sheet1!$D$2:$D$10"
})
# 添加柱状图标题
chart.set_title({"name": "负载统计条形图"})
# chart.set_style(8)
chart.set_size({'width': 900, 'height': 500})
chart.set_legend({'position': 'top'})
# 在F2处绘制
worksheet.insert_chart("F2", chart)
# 收集10分钟的负载数据
def GetLoad():
ref_date = []
for item in range(10):
ret = GetCPUAvg("192.168.1.11", "root", "123123", "22")
ref_date.append(ret)
time.sleep(60)
print(ret)
return ref_date
if __name__ == "__main__":
date = []
date = GetLoad()
AddGraphical(date)
workbook.close()
绘制设备种类(饼状图)
制作Excel绘图: 先来实现绘图。
import xlsxwriter
workbook = xlsxwriter.Workbook("pie.xlsx")
worksheet = workbook.add_worksheet()
data = [["Suse", 30 ], ["Centos",25], ["AIX",15]]
# 定义表格样式
head_style = workbook.add_format({"bold": True, "align": "center" ,"fg_color":"#D7E4BC"})
worksheet.set_column("A1:D1",15)
# 逐条写入数据
headings = ["系统版本", "数量"]
worksheet.write_row("A1", headings, head_style)
for i in range(0, len(data)):
worksheet.write_row("A{}".format(i + 2), data[i])
# 添加条形图,显示前十个元素
chart = workbook.add_chart({"type": "pie"})
chart.add_series({
"name": "=Sheet1!$B$1", # 图例项
"categories": "=Sheet1!$A$2:$A$4", # X轴 Item名称
"values": "=Sheet1!$B$2:$B$4" # X轴Item值
})
# 添加饼状图
chart.set_title({"name": "系统版本分布"})
chart.set_size({'width': 600, 'height': 300})
chart.set_legend({'position': 'right'})
# 在D2处绘制
worksheet.insert_chart("D2", chart)
workbook.close()
实现收集数据并绘图: 收集数据就不写了,主要说说统计个数,我们可以每次收集到数据后放入一个列表中,最后使用set集合将其自动去重,去重后在对特定的值进行计数统计即可。
data = ["Windows 10","Windows 10","Windows 7","Windows 7","Suse","AIX","AIX"]
dic = {}
for each in set(data):
dic[each] = data.count(each)
print(dic)
继续实现绘图即可。
import xlsxwriter
workbook = xlsxwriter.Workbook("pie.xlsx")
worksheet = workbook.add_worksheet()
# 去重后的数据在dic
data = ["Windows 10","Windows 10","Windows 7","Windows 7","Suse","AIX","AIX"]
dic = {}
for each in set(data):
dic[each] = data.count(each)
print("构建字典: {}".format(dic))
print("字典数: {}".format(len(dic)))
# 字典转列表
new_data = []
for k,v in dic.items():
new_list = [k,v]
new_data.append(new_list)
print("字典转为列表:{}".format(new_data))
#data = [["Suse", 30 ], ["Centos",25], ["AIX",15]]
# 定义表格样式
head_style = workbook.add_format({"bold": True, "align": "center" ,"fg_color":"#D7E4BC"})
worksheet.set_column("A1:D1",15)
# 逐条写入数据
headings = ["系统版本", "数量"]
worksheet.write_row("A1", headings, head_style)
for i in range(0, len(new_data)):
worksheet.write_row("A{}".format(i + 2), new_data[i])
# 添加饼状图图例
chart = workbook.add_chart({"type": "pie"})
chart.add_series({
"name": "=Sheet1!$B$1", # 图例项
"categories": "=Sheet1!$A$2:$A${}".format(len(dic)+1), # X轴 Item名称
"values": "=Sheet1!$B$2:$B${}".format(len(dic)+1) # X轴Item值
})
# 添加饼状图
chart.set_title({"name": "系统版本分布"})
chart.set_size({'width': 600, 'height': 300})
chart.set_legend({'position': 'right'})
# 在D2处绘制
worksheet.insert_chart("D2", chart)
workbook.close()
Python 使用xlsxwriter绘制Excel表格的更多相关文章
- Python将多个excel表格合并为一个表格
Python将多个excel表格合并为一个表格 生活中经常会碰到多个excel表格汇总成一个表格的情况,比如你发放了一份表格让班级所有同学填写,而你负责将大家的结果合并成一个.诸如此类的问题有很多.除 ...
- Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented
在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...
- Python 操作csv和excel表格
1. 操作csv表格 使用的库 csv 1. csv文件里读取数据 代码 1. 以列表形式打开 import csv f = open('csv_test.csv', 'r') # 打开csv文件 c ...
- Python三个处理excel表格的库
三个向excel表格写入数的库:xlwt,xlsxwriter,openpyxl,代码如下: 1 #第一个库,xlwt,不能写超过256列的表格 2 import xlwt 3 4 #新建workbo ...
- Python 使用openpyxl导出Excel表格的时候,使用save()保存到指定路径
在使用openpyxl导出Excel表格的使用,如何指定导出的路径呢. 使用sava(filename),会保存到当前执行文件的路径下. 使用sava("/tmp/{}.xlsx" ...
- Python脚本:实现excel表格导入到数据库,支持mysql,postgresql,MongoDB
import xlrd,re from datetime import datetime from xlrd import xldate_as_tuple # 判断上传表格是否与模板要求一致 def ...
- Python利用xlutils统计excel表格数据
假设有像上这样一个表格,里面装满了各式各样的数据,现在要利用模板对它进行统计每个销售商的一些数据的总和.模板如下: 代码开始: 1 #!usr/bin/python3 2 # -*-coding=ut ...
- python XlsxWriter创建Excel 表格
文档(英文) https://xlsxwriter.readthedocs.io/index.html 常用模块说明(中文) https://blog.csdn.net/sinat_35930259/ ...
- Python使用openpyxl操作excel表格
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 ht ...
随机推荐
- AntDesign Pro + .NET Core 实现基于JWT的登录认证
很多同学说AgileConfig的UI实在是太丑了.我想想也是的,本来这个项目是我自己使用的,一开始甚至连UI都没有,全靠手动在数据库里修改数据.后来加上了UI也是使用了老掉牙的bootstrap3做 ...
- 剑指 Offer 26. 树的子结构
剑指 Offer 26. 树的子结构 Offer 26 题目详情: 题解分析 解法一: 第一种比较容易想到的解法就是查看这两棵树的前序遍历和中序遍历序列是否都匹配. 因为前序遍历和中序遍历可以唯一确定 ...
- PAT-1018(Public Bike Management)最短路+额外条件+所有最短路中找出满足条件的路径+dijkstra算法
Public Bike Management PAT-1018 使用一个vector来存储所有最短路的前驱结点,再通过使用dfs和一个额外的vector记录每一条路径 #include<iost ...
- pip命令安装python包到指定目录
pip install wxpython --target=D:\Server\Python38\Lib\site-packages
- JVM-对象及对象内存布局
目录 前言 类与对象 对象类二分模型 对象 对象内存布局 JOL工具 对象头 Mark Word 类型句柄 对象头与锁膨胀 无锁 偏向锁 轻量级锁 重量级锁 重量级锁降级 实例数据 填充 对象生命周期 ...
- 分布式session实现方式
一.背景 在搭建完集群环境后,不得不考虑的一个问题就是用户访问产生的session如何处理. 如果不做任何处理的话,用户将出现频繁登录的现象,比如集群中存在A.B两台服务器,用户在第一次访问网站时,N ...
- 七种join的书写规范
在mysql中的两表进行连接时,总共有7种连接情况,具体可见下图 由图的从左到右的顺序 图1.左连接(left join):返回左表中的所有记录和右表中的连接字符字段相等的记录,若右表没有匹配值则补N ...
- Linux+mysql混杂
一.linux 1.linux中给某一文件中批量新增一个内容 先vim进入文件,然后先按ctrl+v 然后选中需要的行数, 在shift+i 写你要添加的东西 然后在按下esc 二,mysql 1.l ...
- [倍增]luogu P4155 [SCOI2015]国旗计划
题面 https://www.luogu.com.cn/problem/P4155 问在环上最少取多少个区间能完全覆盖环 分析 首先发现是环,先把端点变为2n方便处理,注意离散化 其次要删去贡献不如其 ...
- iNeuOS工业互联平台,生产过程业务联动控制
1.概述 工业物联网也好.工业互联网也好或是其他生产系统,反向控制始终无法回避.搞工业最直接.最体现效果的两个方面是采集各种数据和生产过程业务控制,所谓大数据预测和分析,那是仁者见仁.智者见智,下一篇 ...