#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.name = 'Arial' 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 = xlwt.Workbook() tree = ET.parse('C:\\Tokheim\\xml\\StationReport.xml') root = tree.getroot() tag = root.tag fn = 'C:\\Tokheim\\站报表' + root.get('attrtime') + '.xlsx' sectionname = 'DailyReport' 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) fnt = Font() fnt.name = 'Arial' borders = Borders() borders.left = Borders.THICK borders.right = Borders.THICK borders.top = Borders.THICK borders.bottom = Borders.THICK pattern = Pattern() pattern.pattern = Pattern.SOLID_PATTERN pattern.pattern_fore_colour = 0x0A style = XFStyle() style.font = fnt style.borders = borders headerstyle = getHeaderStyle() headerStyleFont = getHeaderStyleFont() titlestyle = gettitlestyle() contentstyle = getcontentstyle() for index,oilgun in enumerate(root): sheetoil = workbook.add_worksheet(oilgun.get('oilnum')+'号枪') # first_row = sheetoil.row(0) # first_row.set_style(headerstyle) # sheetoil.write_merge(0,0,0,25,"油气回收在线监测枪数据表",headerStyleFont) sheetoil.write(0, 0, "油气回收在线监测站报表") #col1 = sheetoil.col(1) #col1.width = 256 * 20 #first_col = sheetoil.col(0) #first_col.width = 256 * 20 #col2 = sheetoil.col(2) #col2.width = 256 * 15 sheetoil.set_column('A:A', len('2019-00-00 ') + 1) sheetoil.set_column('B:B', len('2019-00-00') + 1) sheetoil.set_column('C:C', len('2019-00-00 ') + 1) sheetoil.set_column('D:D', len('2019-00-00') + 1) sheetoil.write(2, 0, '枪号') sheetoil.write(2, 1, int(oilgun.get('oilnum'))) sheetoil.write(4, 0, '日期') sheetoil.write(4, 1, '有效计数') sheetoil.write(4, 2, 'A/L超标计数') sheetoil.write(4, 3, 'A/L') for index,guncol in enumerate(oilgun): alcounts = 0 avicounts = 0 for gunele in guncol: timetxt = gunele.find('time').text conuts = int(gunele.find('counts').text) al = float(gunele.find('al').text) alcounts = int(gunele.find('alcounts').text) sheetoil.write(index + 5, 1, conuts) sheetoil.write(index+5, 3, al) date_time = datetime.datetime.strptime(timetxt, '%Y%m%d') sheetoil.write(index+5, 0, date_time.strftime('%Y-%m-%d')) sheetoil.write(index+5, 2,alcounts ) 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"))