import mysql.connector import datetime import xlwt dbh = None cur = None xls = None sheet = None try: dbh = mysql.connector.connect(user='statsuser', password='test123', host='192.168.3.22', database='statsdb') cur = dbh.cursor() query = """ SELECT d.description, d.address, cd.ts, cd.value, cd.id FROM device_t d, dataitem_t di, devicedataitem_t ddi, collecteddata_t cd WHERE ddi.device = d.id AND ddi.dataitem = di.id AND cd.devicedataitem = ddi.id AND di.description = 'totalPages' AND cd.ts BETWEEN %s AND %s ORDER BY cd.id """ cur.execute(query, (datetime.datetime(1970, 1, 1), datetime.datetime(2100, 12, 31))) printers = {} for (printerName, printerAddress, timestamp, totalPages, id) in cur: totalPages = int(totalPages) if not printers.has_key(printerAddress): printers[printerAddress] = {'name': printerName, 'address': printerAddress, 'first': {'timestamp': timestamp, 'value': totalPages}, 'last': {'timestamp': timestamp, 'value': totalPages}} else: printers[printerAddress]['last'] = {'timestamp': timestamp, 'value': totalPages} secondsPerMonth = datetime.timedelta(30).total_seconds() secondsPerWeek = datetime.timedelta(7).total_seconds() xls = xlwt.Workbook() sheet = xls.add_sheet('Sheet 1') rowCnt = 0 sheet.row(rowCnt).write(0, 'Name') sheet.row(rowCnt).write(1, 'Address') sheet.row(rowCnt).write(2, 'First Sample Date') sheet.row(rowCnt).write(3, 'First Sample Value') sheet.row(rowCnt).write(4, 'Last Sample Date') sheet.row(rowCnt).write(5, 'Last Sample Value') sheet.row(rowCnt).write(6, 'Diff in SampleBox') sheet.row(rowCnt).write(7, 'MonthFactor') sheet.row(rowCnt).write(8, 'WeekFactor') sheet.row(rowCnt).write(9, 'Month Average/Forecast') sheet.row(rowCnt).write(10, 'Week Average/Forecast') sheet.col(2).width = 256 * 20 sheet.col(4).width = 256 * 20 dateStyle = xlwt.XFStyle() dateStyle.num_format_str = 'DD.MM.YYYY hh:mm:ss' for printer in printers.values(): rowCnt += 1 print("Printer: %s, %s" % (printer['name'], printer['address'])) sheet.row(rowCnt).write(0, printer['name']) sheet.row(rowCnt).write(1, printer['address']) print(" First: %s, %s" % (printer['first']['timestamp'], printer['first']['value'])) sheet.row(rowCnt).write(2, printer['first']['timestamp'], dateStyle) sheet.row(rowCnt).write(3, printer['first']['value']) print(" Last: %s, %s" % (printer['last']['timestamp'], printer['last']['value'])) sheet.row(rowCnt).write(4, printer['last']['timestamp'], dateStyle) sheet.row(rowCnt).write(5, printer['last']['value']) deltaTime = printer['last']['timestamp'] - printer['first']['timestamp'] deltaValue = printer['last']['value'] - printer['first']['value'] print(" Diff: %s, %s" % (deltaTime, deltaValue)) sheet.row(rowCnt).write(6, deltaValue) factorMonth = secondsPerMonth / deltaTime.total_seconds() sheet.row(rowCnt).write(7, factorMonth) factorWeek = secondsPerWeek / deltaTime.total_seconds() sheet.row(rowCnt).write(8, factorWeek) print(" Month average/forecast: %s" % (int(deltaValue * factorMonth))) sheet.row(rowCnt).write(9, int(deltaValue * factorMonth)) print(" Week average/forecast: %s" % (int(deltaValue * factorWeek))) sheet.row(rowCnt).write(10, int(deltaValue * factorWeek)) except mysql.connector.Error as err: print("Failure when using database: %s" % (err)) finally: if xls is not None: xls.save('printerStats.xls') if cur is not None: cur.close() if dbh is not None: dbh.close()