AlarmHandler.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #coding=utf-8
  2. import os
  3. import xml.etree.ElementTree as ET
  4. import xlwt
  5. #from xlwt import Workbook, XFStyle, Borders, Pattern, Font
  6. import datetime
  7. import time
  8. import xlsxwriter
  9. import sys
  10. sys.path.append("C:\Python\Python36\Lib")
  11. start = time.time()
  12. import configparser
  13. def getHeaderStyle():
  14. return xlwt.easyxf('font:height 720;') # 36pt,类型小初的字号
  15. def getHeaderStyleFont():
  16. fnt = Font()
  17. fnt.height = 400
  18. fnt.bold = True
  19. style = XFStyle()
  20. style.font = fnt
  21. return style
  22. def gettitlestyle():
  23. fnt = Font()
  24. fnt.bold = True
  25. alignment = xlwt.Alignment() # Create Alignment
  26. 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
  27. alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
  28. style = XFStyle()
  29. style.alignment = alignment # Add Alignment to Style
  30. style.font = fnt
  31. return style
  32. def getcontentstyle():
  33. fnt = Font()
  34. alignment = xlwt.Alignment() # Create Alignment
  35. 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
  36. alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
  37. style = XFStyle()
  38. style.alignment = alignment # Add Alignment to Style
  39. style.font = fnt
  40. return style
  41. #workbook = xlsxwriter.Workbook('demo.xlsx')
  42. f = open('C:\\Tokheim\\xml\\AlarmReport.xml')
  43. xml_text = f.read()
  44. root = ET.fromstring(xml_text)
  45. f.close()
  46. tag = root.tag
  47. #workbook = xlsxwriter.Workbook('c:\\tokheim\\'+'预报警数据报表'+root.get('attrtime')+'.xlsx')
  48. fn = 'C:\\Tokheim\\预报警数据报表' + root.get('attrtime') + '.xlsx'
  49. sectionname = 'AlarmReport'
  50. cfgpath = "C:\\Tokheim\\plugs\\" + sectionname + "cfg.ini"
  51. workbook = xlsxwriter.Workbook(fn)
  52. try:
  53. workbook.close()
  54. except:
  55. state = "2"
  56. conf = configparser.ConfigParser()
  57. conf.add_section(sectionname)
  58. conf.set(sectionname, "State", state)
  59. conf.write(open(cfgpath, "w"))
  60. exit(1)
  61. workbook = xlsxwriter.Workbook(fn)
  62. for index,oilgun in enumerate(root):
  63. sheetoil = workbook.add_worksheet('预报警数据报表')
  64. sheetoil.write(0,0,"油气回收在线监测油预报警数据表")
  65. sheetoil.write(4, 0, '计数')
  66. sheetoil.write(4, 1, '时间')
  67. sheetoil.write(4, 2, '零压')
  68. sheetoil.write(4, 3, 'PV阀')
  69. sheetoil.write(4, 4, 'PV阀临界')
  70. sheetoil.write(4, 5, '后处理压力')
  71. sheetoil.write(4, 6, '卸油区浓度')
  72. sheetoil.write(4, 7, '三次回收浓度')
  73. sheetoil.write(4, 8, '人孔井浓度')
  74. sheetoil.write(4, 9, '加油区浓度')
  75. sheetoil.set_column('B:B', 20)
  76. sheetoil.set_column('C:Q', 14)
  77. count=0
  78. for guncol in oilgun:
  79. alcounts = 0
  80. avicounts = 0
  81. count=0
  82. for index,gunele in enumerate(guncol):
  83. timetxt = gunele.find('time').text
  84. sheetoil.write(index + 5, 0, int(gunele.find('count').text))
  85. date_time = datetime.datetime.strptime(timetxt, '%Y%m%d%H%M%S')
  86. sheetoil.write(index+5, 1, date_time.strftime('%Y-%m-%d %H:%M:%S'))
  87. sheetoil.write(index+5, 2, gunele.find('zero').text)
  88. sheetoil.write(index+5, 3, gunele.find('pv').text)
  89. sheetoil.write(index+5, 4, gunele.find('pvcrisis').text)
  90. sheetoil.write(index+5, 5, gunele.find('postprocess').text)
  91. sheetoil.write(index+5, 6, gunele.find('gas').text)
  92. sheetoil.write(index+5, 7, gunele.find('postgas').text)
  93. sheetoil.write(index+5, 8, gunele.find('well').text)
  94. sheetoil.write(index+5, 9, gunele.find('oil').text)
  95. count = count + 1
  96. state = "0"
  97. try:
  98. workbook.close()
  99. state = "1"
  100. except:
  101. state = "2"
  102. end = time.time()
  103. stime = end - start
  104. print(stime)
  105. print(state)
  106. conf = configparser.ConfigParser()
  107. conf.add_section(sectionname)
  108. conf.set(sectionname, "State", state)
  109. conf.set(sectionname, "time", str(stime))
  110. conf.write(open(cfgpath, "w"))