DailyHandler.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.name = 'Arial'
  18. fnt.height = 400
  19. fnt.bold = True
  20. style = XFStyle()
  21. style.font = fnt
  22. return style
  23. def gettitlestyle():
  24. fnt = Font()
  25. fnt.bold = True
  26. alignment = xlwt.Alignment() # Create Alignment
  27. 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
  28. alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
  29. style = XFStyle()
  30. style.alignment = alignment # Add Alignment to Style
  31. style.font = fnt
  32. return style
  33. def getcontentstyle():
  34. fnt = Font()
  35. alignment = xlwt.Alignment() # Create Alignment
  36. 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
  37. alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
  38. style = XFStyle()
  39. style.alignment = alignment # Add Alignment to Style
  40. style.font = fnt
  41. return style
  42. workbook = xlwt.Workbook()
  43. tree = ET.parse('C:\\Tokheim\\xml\\StationReport.xml')
  44. root = tree.getroot()
  45. tag = root.tag
  46. fn = 'C:\\Tokheim\\站报表' + root.get('attrtime') + '.xlsx'
  47. sectionname = 'DailyReport'
  48. cfgpath = "C:\\Tokheim\\plugs\\" + sectionname + "cfg.ini"
  49. workbook = xlsxwriter.Workbook(fn)
  50. try:
  51. workbook.close()
  52. except:
  53. state = "2"
  54. conf = configparser.ConfigParser()
  55. conf.add_section(sectionname)
  56. conf.set(sectionname, "State", state)
  57. conf.write(open(cfgpath, "w"))
  58. exit(1)
  59. workbook = xlsxwriter.Workbook(fn)
  60. fnt = Font()
  61. fnt.name = 'Arial'
  62. borders = Borders()
  63. borders.left = Borders.THICK
  64. borders.right = Borders.THICK
  65. borders.top = Borders.THICK
  66. borders.bottom = Borders.THICK
  67. pattern = Pattern()
  68. pattern.pattern = Pattern.SOLID_PATTERN
  69. pattern.pattern_fore_colour = 0x0A
  70. style = XFStyle()
  71. style.font = fnt
  72. style.borders = borders
  73. headerstyle = getHeaderStyle()
  74. headerStyleFont = getHeaderStyleFont()
  75. titlestyle = gettitlestyle()
  76. contentstyle = getcontentstyle()
  77. for index,oilgun in enumerate(root):
  78. sheetoil = workbook.add_worksheet(oilgun.get('oilnum')+'号枪')
  79. # first_row = sheetoil.row(0)
  80. # first_row.set_style(headerstyle)
  81. # sheetoil.write_merge(0,0,0,25,"油气回收在线监测枪数据表",headerStyleFont)
  82. sheetoil.write(0, 0, "油气回收在线监测站报表")
  83. #col1 = sheetoil.col(1)
  84. #col1.width = 256 * 20
  85. #first_col = sheetoil.col(0)
  86. #first_col.width = 256 * 20
  87. #col2 = sheetoil.col(2)
  88. #col2.width = 256 * 15
  89. sheetoil.set_column('A:A', len('2019-00-00 ') + 1)
  90. sheetoil.set_column('B:B', len('2019-00-00') + 1)
  91. sheetoil.set_column('C:C', len('2019-00-00 ') + 1)
  92. sheetoil.set_column('D:D', len('2019-00-00') + 1)
  93. sheetoil.write(2, 0, '枪号')
  94. sheetoil.write(2, 1, int(oilgun.get('oilnum')))
  95. sheetoil.write(4, 0, '日期')
  96. sheetoil.write(4, 1, '有效计数')
  97. sheetoil.write(4, 2, 'A/L超标计数')
  98. sheetoil.write(4, 3, 'A/L')
  99. for index,guncol in enumerate(oilgun):
  100. alcounts = 0
  101. avicounts = 0
  102. for gunele in guncol:
  103. timetxt = gunele.find('time').text
  104. conuts = int(gunele.find('counts').text)
  105. al = float(gunele.find('al').text)
  106. alcounts = int(gunele.find('alcounts').text)
  107. sheetoil.write(index + 5, 1, conuts)
  108. sheetoil.write(index+5, 3, al)
  109. date_time = datetime.datetime.strptime(timetxt, '%Y%m%d')
  110. sheetoil.write(index+5, 0, date_time.strftime('%Y-%m-%d'))
  111. sheetoil.write(index+5, 2,alcounts )
  112. state = "0"
  113. try:
  114. workbook.close()
  115. state = "1"
  116. except:
  117. state = "2"
  118. end = time.time()
  119. stime = end -start
  120. print(stime)
  121. print(state)
  122. conf = configparser.ConfigParser()
  123. conf.add_section(sectionname)
  124. conf.set(sectionname, "State", state)
  125. conf.set(sectionname, "time", str(stime))
  126. conf.write(open(cfgpath, "w"))