import os import xml.etree.ElementTree as ET import xlwt # from xlwt import Workbook, XFStyle, Borders, Pattern, Font import datetime import time import xlsxwriter import sys import socket # python3 import configparser def app_path(): """Returns the base application path.""" if hasattr(sys, 'frozen'): # Handles PyInstaller return os.path.dirname(sys.executable) #使用pyinstaller打包后的exe目录 return os.path.dirname(__file__) #没打包前的py目录 current_directory = os.path.dirname(os.path.abspath(__file__)) #tree = ET.parse(current_directory + '\\xml\\AuthCodeReport.xml') tree = ET.parse(app_path() + '\\xml\\AuthCodeReport.xml') root = tree.getroot() #fn = current_directory+ '\\report\\授权码数据记录表' + '.xlsx' fn = app_path() + '\\report\\授权码数据记录表' + '.xlsx' workbook = xlsxwriter.Workbook(fn) #=========样式=============== formatValue = workbook.add_format() formatValue.set_align('center') formatValue.set_font_name('等线') formatValueHead = workbook.add_format() formatValueHead.set_bold() formatValueHead.set_font_name('等线') formatHead = workbook.add_format() formatHead.set_font_size(20) formatHead.set_bold() formatHead.set_font_name('等线') #=============================== sheetAuth = workbook.add_worksheet('授权码报表') sheetAuth.write(0, 0, "授权码数据记录表",formatHead) formatValueHead.set_align('center') sheetAuth.write(2, 0, '时间',formatValueHead) sheetAuth.write(2, 1, '用户',formatValueHead) sheetAuth.write(2, 2, '油站名称',formatValueHead) sheetAuth.write(2, 3, '油机机型',formatValueHead) sheetAuth.write(2, 4, '油机ID',formatValueHead) sheetAuth.write(2, 5, '功能码',formatValueHead) sheetAuth.write(2, 6, '授权码',formatValueHead) sheetAuth.write(2, 7, '授权码计数',formatValueHead) sheetAuth.write(2, 8, '油机类型',formatValueHead) sheetAuth.set_column('A:A', len('2019-00-00 00:00:00') + 1) # sheetAuth.set_column('D:D', len('111111111111AAAB') + 1) # sheetAuth.set_column('E:E', len('111111111111AAAB') + 1) # sheetAuth.set_column('F:F', len('111111111111AAAB') + 1) sheetAuth.set_column('B:I', len('111111111111AAAB') + 1) sheetAuth.set_column('E:E',20 + 1) sheetAuth.set_column(13, 13, None, None, {'hidden': 1}) for index,authvalue in enumerate(root): createTime = authvalue.find('createTime').text user = authvalue.find('user').text gasStation = authvalue.find('gasStation').text cpuID = authvalue.find('cpuID').text functionCode = authvalue.find('functionCode').text AuthCode = authvalue.find('AuthCode').text machineType = authvalue.find('machineType').text authCodeCount = authvalue.find('authCodeCount').text type = authvalue.find('type').text tmpformat = formatValue #===time=== date_time = datetime.datetime.strptime(createTime, '%Y%m%d%H%M%S') sheetAuth.write(index + 3, 0, date_time.strftime('%Y-%m-%d %H:%M:%S'),tmpformat) #=========== sheetAuth.write(index + 3, 1, user, tmpformat) sheetAuth.write(index + 3, 2, gasStation, tmpformat) sheetAuth.write(index + 3, 3, machineType, tmpformat) sheetAuth.write(index + 3, 4, cpuID, tmpformat) sheetAuth.write(index + 3, 5, functionCode, tmpformat) sheetAuth.write(index + 3, 6, AuthCode, tmpformat) sheetAuth.write(index + 3, 7, authCodeCount, tmpformat) sheetAuth.write(index + 3, 8, type, tmpformat) workbook.close()