output_file.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import app
  2. import xlwt
  3. import log
  4. import threading
  5. import tkinter.messagebox
  6. import sql
  7. import time
  8. from reportlab.pdfbase import pdfmetrics
  9. from reportlab.pdfbase.ttfonts import TTFont
  10. from reportlab.platypus import Table, SimpleDocTemplate, Paragraph
  11. from reportlab.lib.pagesizes import A3, landscape
  12. from reportlab.lib.styles import getSampleStyleSheet
  13. from reportlab.lib import colors
  14. from reportlab.graphics.charts.barcharts import VerticalBarChart
  15. from reportlab.graphics.charts.legends import Legend
  16. from reportlab.graphics.shapes import Drawing
  17. pdfmetrics.registerFont(TTFont('SimSun', 'SIMYOU.TTF'))
  18. class output_file(threading.Thread):
  19. def __init__(self, threadID, name, counter, type, datastr='', filepath='', content='', master=None):
  20. threading.Thread.__init__(self)
  21. self.threadID = threadID
  22. self.name = name
  23. self.counter = counter
  24. self.filepath = filepath
  25. self.content = content
  26. self.type = type
  27. self.datastr = datastr
  28. self.user_var = app.user_var
  29. self.sqlclient = sql.sqlclient
  30. self.log_file = log.log_file
  31. self.log_print = log.log_file
  32. def get_product_param(self):
  33. self.user_var.product_data.clear()
  34. idx = 0
  35. for product_name in self.user_var.product_list:
  36. if product_name == '':
  37. continue
  38. sqlstr = "SELECT product_idx,product_name,param_type,startuptime,airtime,presstime, testtime, exhaust, \
  39. maxpress, minpress, setpress, capacity, posthreshold, pnegthreshold \
  40. FROM product_param_tbl WHERE product_name='{0}' ORDER by product_idx ASC".format(product_name)
  41. results = self.sqlclient.sql_inquire_all(sqlstr)
  42. if results != None and len(results) > 0:
  43. for row in results:
  44. if row[2] == 17700:
  45. value = (
  46. row[0]+1, self.user_var.product_17700_list[idx], row[3], row[4], row[5], row[6], row[7], row[8],
  47. row[9], row[10], row[11], row[12], row[13])
  48. else:
  49. value = (
  50. row[0]+1, self.user_var.product_17720_list[idx], row[3], row[4], row[5], row[6], row[7], row[8],
  51. row[9], row[10], row[11], row[12], row[13])
  52. self.user_var.product_data.append(value)
  53. else:
  54. value = ('0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')
  55. self.user_var.product_data.append(value)
  56. self.user_var.listview_stat = True
  57. def get_product_data(self):
  58. self.user_var.product_data.clear()
  59. sqlstr = 'SELECT batch,serial, product_name, testok, data_17700, data_17720, team, begin_time, end_time\
  60. FROM product_data_tbl {0}'.format(
  61. self.datastr)
  62. results = self.sqlclient.sql_inquire_all(sqlstr)
  63. if results != None and len(results) > 0:
  64. for row in results:
  65. tt = time.localtime(row[8])
  66. if row[3] == 1:
  67. testok = 'OK'
  68. else:
  69. testok = 'NG'
  70. sqlstr = "SELECT 17700_name,17720_name FROM product_tbl WHERE stat=True and product_name='{0}'".format(
  71. row[2]
  72. )
  73. paramresult = self.sqlclient.sql_inquire_all(sqlstr)
  74. if paramresult != None and len(paramresult) > 0:
  75. for paramrow in paramresult:
  76. name_17700 = paramrow[0]
  77. name_17720 = paramrow[1]
  78. else:
  79. name_17700 = row[2]
  80. name_17720 = row[2]
  81. datalist17700 = [time.strftime("%Y-%m-%d", tt), time.strftime("%H:%M:%S", tt), row[1], row[0], testok,
  82. row[4], row[6], name_17700]
  83. sqlstr = "SELECT startuptime,airtime,presstime, testtime, exhaust, maxpress, minpress, setpress, capacity, \
  84. posthreshold, pnegthreshold FROM product_param_tbl WHERE product_name='{0}' and \
  85. param_type = 17700".format(row[2])
  86. paramresult = self.sqlclient.sql_inquire_all(sqlstr)
  87. if paramresult != None and len(paramresult) > 0:
  88. for paramrow in paramresult:
  89. datalist17700.append(paramrow[0])
  90. datalist17700.append(paramrow[1])
  91. datalist17700.append(paramrow[2])
  92. datalist17700.append(paramrow[3])
  93. datalist17700.append(paramrow[4])
  94. datalist17700.append(paramrow[5])
  95. datalist17700.append(paramrow[6])
  96. datalist17700.append(paramrow[7])
  97. datalist17700.append(paramrow[8])
  98. datalist17700.append(paramrow[9])
  99. datalist17700.append(paramrow[10])
  100. else:
  101. datalist17700.append(0)
  102. datalist17700.append(0)
  103. datalist17700.append(0)
  104. datalist17700.append(0)
  105. datalist17700.append(0)
  106. datalist17700.append(0)
  107. datalist17700.append(0)
  108. datalist17700.append(0)
  109. datalist17700.append(0)
  110. datalist17700.append(0)
  111. datalist17700.append(0)
  112. self.user_var.product_data.append(tuple(datalist17700))
  113. datalist17720 = [time.strftime("%Y-%m-%d", tt), time.strftime("%H:%M:%S", tt), row[1], row[0], testok,
  114. row[5], row[6], name_17720]
  115. sqlstr = "SELECT startuptime, airtime,presstime, testtime, exhaust, maxpress, minpress, setpress, capacity, \
  116. posthreshold, pnegthreshold FROM product_param_tbl WHERE product_name='{0}' and \
  117. param_type = 17720".format(row[2])
  118. paramresult = self.sqlclient.sql_inquire_all(sqlstr)
  119. if paramresult != None and len(paramresult) > 0:
  120. for paramrow in paramresult:
  121. datalist17720.append(paramrow[0])
  122. datalist17720.append(paramrow[1])
  123. datalist17720.append(paramrow[2])
  124. datalist17720.append(paramrow[3])
  125. datalist17720.append(paramrow[4])
  126. datalist17720.append(paramrow[5])
  127. datalist17720.append(paramrow[6])
  128. datalist17720.append(paramrow[7])
  129. datalist17720.append(paramrow[8])
  130. datalist17720.append(paramrow[9])
  131. datalist17720.append(paramrow[10])
  132. else:
  133. datalist17720.append(0)
  134. datalist17720.append(0)
  135. datalist17720.append(0)
  136. datalist17720.append(0)
  137. datalist17720.append(0)
  138. datalist17720.append(0)
  139. datalist17720.append(0)
  140. datalist17720.append(0)
  141. datalist17720.append(0)
  142. datalist17720.append(0)
  143. datalist17720.append(0)
  144. self.user_var.product_data.append(tuple(datalist17720))
  145. self.user_var.listview_stat = True
  146. def save_excel_file(self):
  147. try:
  148. xls = xlwt.Workbook()
  149. sht1 = xls.add_sheet("Sheet1")
  150. for row, row_content in enumerate(self.content):
  151. for column, column_content in enumerate(row_content):
  152. sht1.write(row, column, column_content)
  153. xls.save(self.filepath)
  154. return True
  155. except Exception as e:
  156. self.log_file.logger.error('写入excel文件({0})异常: {1}'.format(self.filepath, e),
  157. exc_info=True, stack_info=True)
  158. return False
  159. def draw_title(self, title):
  160. style = getSampleStyleSheet()
  161. ct = style['Normal']
  162. ct.fontName = 'SimSun'
  163. ct.fontSize = 18
  164. # 设置行距
  165. ct.leading = 50
  166. # 颜色
  167. ct.textColor = colors.green
  168. # 居中
  169. ct.alignment = 1
  170. # 添加标题并居中
  171. title = Paragraph(title, ct)
  172. return title
  173. def draw_table(self, *args):
  174. style = [
  175. ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体
  176. ('BACKGROUND', (0, 0), (-1, 0), '#d5dae6'), # 设置第一行背景颜色
  177. ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 对齐
  178. ('VALIGN', (-1, 0), (-2, 0), 'MIDDLE'), # 对齐
  179. ('GRID', (0, 0), (-1, -1), 0.5, colors.grey), # 设置表格框线为grey色,线宽为0.5
  180. ]
  181. table = Table(args, style=style)
  182. return table
  183. def save_pdf_file(self):
  184. try:
  185. content = list()
  186. content.append(self.draw_title('气密数据表'))
  187. content.append(self.draw_table(*self.content))
  188. pdf = SimpleDocTemplate(self.filepath, pagesize=landscape(A3))
  189. pdf.build(content)
  190. return True
  191. except Exception as e:
  192. self.log_file.logger.error('写入pdf文件({0})异常: {1}'.format(self.filepath, e),
  193. exc_info=True, stack_info=True)
  194. return False
  195. def run(self):
  196. if self.type == 'save_excel':
  197. stat = self.save_excel_file()
  198. if stat == True:
  199. tkinter.messagebox.showinfo(title='提示', message='文件保存成功!')
  200. else:
  201. tkinter.messagebox.showwarning(title='提示', message='文件保存失败!')
  202. self.user_var.file_save_stat = True
  203. print("{0}保存完成".format(self.filepath))
  204. elif self.type == 'save_pdf':
  205. stat = self.save_pdf_file()
  206. if stat == True:
  207. tkinter.messagebox.showinfo(title='提示', message='文件保存成功!')
  208. else:
  209. tkinter.messagebox.showwarning(title='提示', message='文件保存失败!')
  210. self.user_var.file_save_stat = True
  211. print("{0}保存完成".format(self.filepath))
  212. elif self.type == 'inquire_param':
  213. self.get_product_param()
  214. elif self.type == 'inquire_data':
  215. self.get_product_data()