#coding=utf-8 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 sys.path.append("C:\Python\Python36\Lib") start = time.time() import configparser def getHeaderStyle(): return xlwt.easyxf('font:height 720;') # 36pt,类型小初的字号 def getHeaderStyleFont(): fnt = Font() fnt.height = 400 fnt.bold = True style = XFStyle() style.font = fnt return style def gettitlestyle(): fnt = Font() fnt.bold = True alignment = xlwt.Alignment() # Create Alignment alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED style = XFStyle() style.alignment = alignment # Add Alignment to Style style.font = fnt return style def getcontentstyle(): fnt = Font() alignment = xlwt.Alignment() # Create Alignment alignment.horz = xlwt.Alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED style = XFStyle() style.alignment = alignment # Add Alignment to Style style.font = fnt return style #workbook = xlsxwriter.Workbook('demo.xlsx') f = open('C:\\Tokheim\\xml\\AlarmReport.xml') xml_text = f.read() root = ET.fromstring(xml_text) f.close() tag = root.tag #workbook = xlsxwriter.Workbook('c:\\tokheim\\'+'预报警数据报表'+root.get('attrtime')+'.xlsx') fn = 'C:\\Tokheim\\预报警数据报表' + root.get('attrtime') + '.xlsx' sectionname = 'AlarmReport' cfgpath = "C:\\Tokheim\\plugs\\" + sectionname + "cfg.ini" workbook = xlsxwriter.Workbook(fn) try: workbook.close() except: state = "2" conf = configparser.ConfigParser() conf.add_section(sectionname) conf.set(sectionname, "State", state) conf.write(open(cfgpath, "w")) exit(1) workbook = xlsxwriter.Workbook(fn) for index,oilgun in enumerate(root): sheetoil = workbook.add_worksheet('预报警数据报表') sheetoil.write(0,0,"油气回收在线监测油预报警数据表") sheetoil.write(4, 0, '计数') sheetoil.write(4, 1, '时间') sheetoil.write(4, 2, '零压') sheetoil.write(4, 3, 'PV阀') sheetoil.write(4, 4, 'PV阀临界') sheetoil.write(4, 5, '后处理压力') sheetoil.write(4, 6, '卸油区浓度') sheetoil.write(4, 7, '三次回收浓度') sheetoil.write(4, 8, '人孔井浓度') sheetoil.write(4, 9, '加油区浓度') sheetoil.set_column('B:B', 20) sheetoil.set_column('C:Q', 14) count=0 for guncol in oilgun: alcounts = 0 avicounts = 0 count=0 for index,gunele in enumerate(guncol): timetxt = gunele.find('time').text sheetoil.write(index + 5, 0, int(gunele.find('count').text)) date_time = datetime.datetime.strptime(timetxt, '%Y%m%d%H%M%S') sheetoil.write(index+5, 1, date_time.strftime('%Y-%m-%d %H:%M:%S')) sheetoil.write(index+5, 2, gunele.find('zero').text) sheetoil.write(index+5, 3, gunele.find('pv').text) sheetoil.write(index+5, 4, gunele.find('pvcrisis').text) sheetoil.write(index+5, 5, gunele.find('postprocess').text) sheetoil.write(index+5, 6, gunele.find('gas').text) sheetoil.write(index+5, 7, gunele.find('postgas').text) sheetoil.write(index+5, 8, gunele.find('well').text) sheetoil.write(index+5, 9, gunele.find('oil').text) count = count + 1 state = "0" try: workbook.close() state = "1" except: state = "2" end = time.time() stime = end - start print(stime) print(state) conf = configparser.ConfigParser() conf.add_section(sectionname) conf.set(sectionname, "State", state) conf.set(sectionname, "time", str(stime)) conf.write(open(cfgpath, "w"))