-.bak 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. def getHeaderStyle():
  10. return xlwt.easyxf('font:height 720;') # 36pt,类型小初的字号
  11. def getHeaderStyleFont():
  12. fnt = Font()
  13. fnt.height = 400
  14. fnt.bold = True
  15. style = XFStyle()
  16. style.font = fnt
  17. return style
  18. def gettitlestyle():
  19. fnt = Font()
  20. fnt.bold = True
  21. alignment = xlwt.Alignment() # Create Alignment
  22. 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
  23. alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
  24. style = XFStyle()
  25. style.alignment = alignment # Add Alignment to Style
  26. style.font = fnt
  27. return style
  28. def getcontentstyle():
  29. fnt = Font()
  30. alignment = xlwt.Alignment() # Create Alignment
  31. 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
  32. alignment.vert = xlwt.Alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
  33. style = XFStyle()
  34. style.alignment = alignment # Add Alignment to Style
  35. style.font = fnt
  36. return style
  37. workbook = xlsxwriter.Workbook('demo.xlsx')
  38. tree = ET.parse('C:\\Tokheim\\xml\\GunReport.xml')
  39. root = tree.getroot()
  40. tag = root.tag
  41. borders = Borders()
  42. borders.left = Borders.THICK
  43. borders.right = Borders.THICK
  44. borders.top = Borders.THICK
  45. borders.bottom = Borders.THICK
  46. pattern = Pattern()
  47. pattern.pattern = Pattern.SOLID_PATTERN
  48. pattern.pattern_fore_colour = 0x0A
  49. style = XFStyle()
  50. style.borders = borders
  51. headerstyle = getHeaderStyle()
  52. headerStyleFont = getHeaderStyleFont()
  53. titlestyle = gettitlestyle()
  54. contentstyle = getcontentstyle()
  55. for index,oilgun in enumerate(root):
  56. sheetoil = workbook.add_worksheet(oilgun.get('oilnum')+'号枪报表')
  57. first_row = sheetoil.row(0)
  58. first_row.set_style(headerstyle)
  59. sheetoil.write_merge(0,0,0,25,"油气回收在线监测枪数据表",headerStyleFont)
  60. col1 = sheetoil.col(1)
  61. col1.width = 256 * 20
  62. first_col = sheetoil.col(0)
  63. first_col.width = 256 * 20
  64. col2 = sheetoil.col(2)
  65. col2.width = 256 * 15
  66. sheetoil.write(2, 0, '加油机编号',titlestyle)
  67. sheetoil.write(2, 1, oilgun.get('oilnum'))
  68. sheetoil.write(4, 0, '时间',titlestyle)
  69. sheetoil.write(4, 1, '有效计数',titlestyle)
  70. sheetoil.write(4, 2, 'A/L超标计数',titlestyle)
  71. sheetoil.write(4, 3, 'A/L',titlestyle)
  72. sheetoil.write(4, 4, '油体积', titlestyle)
  73. sheetoil.write(4, 5, '气体积', titlestyle)
  74. for guncol in oilgun:
  75. alcounts = 0
  76. avicounts = 0
  77. for index,gunele in enumerate(guncol):
  78. timetxt = gunele.find('time').text
  79. isfalse = gunele.find('isfalse').text
  80. al = gunele.find('al').text
  81. liquidvl = gunele.find('liquidvl').text
  82. vaporvl = gunele.find('vaporvl').text
  83. sheetoil.write(index + 5, 1, index+1, contentstyle)
  84. sheetoil.write(index+5, 3, al,contentstyle)
  85. sheetoil.write(index + 5, 4, liquidvl,contentstyle )
  86. sheetoil.write(index + 5, 5, vaporvl, contentstyle)
  87. date_time = datetime.datetime.strptime(timetxt, '%Y%m%d%H%M%S')
  88. sheetoil.write(index+5, 0, date_time.strftime('%Y-%m-%d %H:%M:%S'))
  89. if isfalse == 'Y':
  90. avicounts = avicounts+1
  91. sheetoil.write(index+5, 2,avicounts ,contentstyle)
  92. else:
  93. sheetoil.write(index + 5, 2,0, contentstyle)
  94. workbook.save('c:\\tokheim\\'+'枪报表'+root.get('attrtime')+'.xls')