1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| import openpyxl from datetime import datetime from lib.cmutils_io import CSVUtils from openpyxl.chart import ( LineChart, Reference, ) from openpyxl.chart.axis import DateAxis from openpyxl import Workbook DATE_TIME_FORMAT = "%Y/%m/%d %H:%M:%S" def test_monitor(): wb_tpl = Workbook() ws = wb_tpl.active ws.title = "Monitor" """ [ [2018/06/17 23:47:11,17.0,16.71], [2018/06/17 23:47:13,1.5,17.01], [2018/06/17 23:47:15,2.9,17.04], ... ] """ data_list = CSVUtils.readCSVRowsList("Monitor.csv") _max_row = len(data_list) _max_col = 3 for row_index, rows in enumerate(data_list): for col_index, value in enumerate(rows): if(row_index == 0): ws.cell(row=row_index+1, column=col_index+1).value = value else: if(col_index == 0): ws.cell(row=row_index+1, column=col_index+1).value = datetime.strptime(value, DATE_TIME_FORMAT) ws.cell(row=row_index+1, column=col_index+1).number_format = 'HH:mm:ss' else: ws.cell(row=row_index+1, column=col_index+1).value = float(value) chart1 = LineChart() chart1.title = "CPU/Memory Mornitor" chart1.style = 2 chart1.height = 15 chart1.width = 30 chart1.legend.position = "b" chart1.y_axis.scaling.min = 0 chart1.y_axis.scaling.max = 100
data = Reference(ws, min_col=2, min_row=1, max_col=_max_col, max_row=_max_row) chart1.add_data(data, titles_from_data=True)
cats = Reference(ws, min_col=1, min_row=1, max_row=_max_row) chart1.set_categories(cats)
s2 = chart1.series[0] s2.smooth = True
s2 = chart1.series[1] s2.smooth = True ws.add_chart(chart1, "F10") dest_filename = 'test2.xlsx' wb_tpl.save(filename = dest_filename) test_monitor()
|