No Description

pressexcel.py 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. from openpyxl import load_workbook
  2. import pyodbc
  3. import time
  4. import sys
  5. from pprint import pprint
  6. import datetime
  7. from openpyxl.styles import NamedStyle, Font, colors, Border, Side
  8. import openpyxl.drawing.text as drawingText
  9. from copy import deepcopy
  10. from openpyxl.chart import LineChart, Reference, Series
  11. from openpyxl.chart.text import RichText
  12. from openpyxl.drawing.text import RichTextProperties,Paragraph,ParagraphProperties, CharacterProperties
  13. import openpyxl.utils as utils
  14. from openpyxl.chart.text import RichText
  15. dateStyle = NamedStyle(name='custom_datetime', number_format='DD/MM/YYYY HH:MM:MM')
  16. codeMap = {
  17. "001": "D",
  18. "002": "T",
  19. "003": "H",
  20. "02": "P",
  21. "03": "E",
  22. "39": "E",
  23. "04": "W",
  24. "05": "JK",
  25. "06": "F",
  26. "51": "F",
  27. "07": "G",
  28. "B2": "G",
  29. "08": "U",
  30. "09": "M",
  31. "10": "A",
  32. "11": "N",
  33. "12": "J",
  34. "13": "X",
  35. "14": "Flatness",
  36. "15": "K",
  37. "16": "Thickness",
  38. "17": "Q",
  39. "18": "S",
  40. "72": "X"
  41. }
  42. # Specifying the ODBC driver, server name, database, etc. directly
  43. #cnxn = None
  44. #cursor = None
  45. '''cursor.execute('select * from data_wb')
  46. rows = cursor.fetchall()
  47. for row in rows:
  48. print(row)'''
  49. #wb = load_workbook(filename = './measurement-temp.xlsx')
  50. wb = load_workbook(filename = './press-temp.xlsx')
  51. #wb.save(filename = './createfrompy.xlsx');
  52. def findCustomer(code):
  53. global cursor
  54. global env
  55. tempResult = []
  56. views = []
  57. if env == "prod":
  58. views = ["bel_master_view", "e_master_view", "mg_master_view", "v_master_view"]
  59. else:
  60. views = ["bel_master_view_dev", "e_master_view_dev", "mg_master_view_dev", "v_master_view_dev"]
  61. ts = []
  62. select = "distinct MI24 as unbStd, PRO1C as customer, cast(PRO8 as varchar) + ' ' + cast(PRO9 as varchar) + cast(PRO10 as varchar) + 'x' + cast(PRO11 as varchar ) + 'x' + cast(PRO12 as varchar) as sizestring, cast(PRO13 as varchar) + ' ' + cast(PRO14 as varchar) + ' ' + cast(PRO15 as varchar) + ' ' + cast(PRO16 as varchar) + ' ' + cast(PRO17 as varchar) + ' ' + cast(PRO18 as varchar) as spec"
  63. where = "PRO1 = '{0}'".format(code)
  64. for v in views:
  65. sqlQuery = "select {0} from {1} where {2}".format(select, v, where)
  66. ts.append(sqlQuery)
  67. merged = []
  68. for t in ts:
  69. merged += cursor.execute(t)
  70. return merged
  71. def createWS(name, values, codeno, filters):
  72. global wb
  73. #wb.create_sheet(name)
  74. wo = wb['Data Measurement']
  75. ws = wb.copy_worksheet(wo)
  76. ws.title = name
  77. cust = findCustomer(codeno.replace("-", ""))
  78. ws["B2"] = cust[0].customer
  79. ws["B3"] = codeno.replace("-", "")
  80. ws["B4"] = cust[0].sizestring
  81. ws["B5"] = cust[0].spec
  82. bcode = ord('B')
  83. ccode = 2
  84. for idx, val in enumerate(values):
  85. #print(idx)
  86. c = chr(bcode + idx)
  87. #print(val.lot_no)
  88. if ( bcode + idx) > ord('Z'):
  89. r = bcode + idx - ord('Z')
  90. c = 'A' + chr(ord('A') + (r - 1))
  91. #print("c = "+c)
  92. ws.cell(column= ccode, row=45, value = val.lot_no)
  93. ms = findInMasterView('*', "PRO2 = '{0}'".format(val.lot_no))
  94. #pressInfo = findPressInfo(codeno.replace("-", ""),val.lot_no, filters)
  95. #print("master view")
  96. #print(ms)
  97. if ms:
  98. ws.cell(column = ccode, row=35, value = ms[0].MI24)
  99. #ws[c+'38'] = ms[0].STD_Balance
  100. ws.cell(column = ccode , row=37, value = ms[0].PRO6)
  101. #ws[c+'44'] = ms[0].PRO5
  102. ws.cell(column = ccode, row = 43, value = ms[0].PRO10)
  103. ws.cell(column = ccode, row = 42, value = ms[0].PRO12)
  104. #if pressInfo:
  105. ws.cell(column = ccode , row=39, value = val.Press_UserName)
  106. ws.cell(column = ccode, row = 40, value = val.Press_McName)
  107. #print(type(pressInfo[0].Insp_Date))
  108. #ws[c+'46'].number_format = "Short Date"
  109. ws.cell(column = ccode, row = 46 , value = val.Insp_Date.date())
  110. #print(type(ws[c+'46'].value))
  111. #ws[c+'46'].style = dateStyle
  112. #ws[c+'46'].value = datetime.datetime(2010, 7, 21)
  113. #print(ws[c+'46'].number_format)
  114. #ws[c+'46']. = datetime.datetime
  115. ws.cell(column = ccode , row = 44, value = val.FG_Qty)
  116. dws = updateLength(val.lot_no)
  117. for j,v in enumerate(dws):
  118. ws.cell(column = ccode , row = 48+v.row_no , value = v.weight)
  119. dws = updateUb(val.lot_no)
  120. for j,v in enumerate(dws):
  121. ws.cell(column = ccode , row = 171+v.row_no , value = v.weight)
  122. ccode += 1
  123. c1 = LineChart()
  124. c1.legend.position = "t";
  125. c1.title = "Graph of Result Cp Unbalance"
  126. c1.title.font = Font(underline=Font.UNDERLINE_SINGLE)
  127. c1.y_axis.title = "Unb Weight"
  128. c1.y_axis.number_format = "#,##0.00"
  129. c1.x_axis.title = "Lot No"
  130. c1.width = 30
  131. c1.height = 10
  132. #print(len(values))
  133. lt = Reference(ws, min_col=2, max_col=len(values)+1, min_row=45, max_row=45)
  134. data = Reference(ws, min_col=1, max_col=len(values)+1, min_row=34, max_row=34)
  135. maxUB = Reference(ws, min_col=1, max_col=len(values)+1, min_row=29, max_row=29)
  136. minUB = Reference(ws, min_col=1, max_col=len(values)+1, min_row=30, max_row=30)
  137. avgUB = Reference(ws, min_col=1, max_col=len(values)+1, min_row=31, max_row=31)
  138. vsStd = Reference(ws, min_col=1, max_col=len(values)+1, min_row=36, max_row=36)
  139. c1.add_data(data, from_rows=True, titles_from_data=True)
  140. c1.add_data(minUB, from_rows=True, titles_from_data=True)
  141. c1.add_data(avgUB, from_rows=True, titles_from_data=True)
  142. c1.add_data(vsStd, from_rows=True, titles_from_data=True)
  143. c1.add_data(maxUB, from_rows=True, titles_from_data=True)
  144. s1 = c1.series[0]
  145. s1.marker.symbol = "triangle"
  146. s1.graphicalProperties.line.noFill = True
  147. s1 = c1.series[1]
  148. s1.marker.symbol = "circle"
  149. s1.graphicalProperties.line.noFill = True
  150. s1 = c1.series[2]
  151. s1.marker.symbol = "square"
  152. s1.graphicalProperties.line.noFill = True
  153. s1 = c1.series[4]
  154. s1.marker.symbol = "diamond"
  155. s1.graphicalProperties.line.noFill = True
  156. c1.set_categories(lt)
  157. c1.x_axis.txPr = RichText(bodyPr=RichTextProperties(anchor="ctr",anchorCtr="1",rot="-2700000",
  158. spcFirstLastPara="1",vertOverflow="ellipsis",wrap="square"),
  159. p=[Paragraph(pPr=ParagraphProperties(defRPr=CharacterProperties()), endParaRPr=CharacterProperties())])
  160. ws.add_chart(c1, "A7")
  161. #newws.title = name
  162. #copy data cp ws
  163. #insert values in the sheet
  164. def findInMasterView(select, where):
  165. global cursor
  166. global env
  167. tempResult = []
  168. views = []
  169. if env == "prod":
  170. views = ["bel_master_view", "e_master_view", "mg_master_view", "v_master_view"]
  171. else:
  172. views = ["bel_master_view_dev", "e_master_view_dev", "mg_master_view_dev", "v_master_view_dev"]
  173. ts = []
  174. for v in views:
  175. sqlQuery = "select {0} from {1} where {2}".format(select, v, where)
  176. ts.append(sqlQuery);
  177. qj = ';'.join(ts)
  178. #print(qj)
  179. merged = []
  180. for t in ts:
  181. merged += cursor.execute(t)
  182. if len(merged) > 0:
  183. return merged
  184. return merged
  185. def findPressInfo(code, lot, year):
  186. global cursor
  187. global env
  188. fyear = year.replace("created_at", "Insp_Date")
  189. q = "select * from tb_fg_pressinfo_lotlist where ProductCode = '{0}' and LotNo = '{1}' and {2}" \
  190. .format(code, lot, fyear)
  191. cursor.execute(q)
  192. return cursor.fetchall()
  193. def findProductDimension(codeno):
  194. stdSizes = """
  195. select * from AllProduct_Dimension_ForInsProcess where ProductCode='{0}'""".format(codeno.replace("-", ""))
  196. #print(stdSizes)
  197. return stdSizes
  198. def findProductPress(codeno):
  199. stdSizes = """
  200. select * from AllProduct_PressPosition_PressWeight where ProductCode='{0}'""".format(codeno.replace("-", ""))
  201. #print(stdSizes)
  202. return stdSizes
  203. def generateSQLDataMS(codeno, filters):
  204. if codeno.find('-') != -1:
  205. codeString = codeno
  206. else:
  207. codeString = codeno[0] + "-" + codeno[1:5] + "-" + codeno[5:9] + "-" + codeno[9:10]
  208. if filters is not None:
  209. qMsData = """
  210. select * from data_ms where code='{0}' and (size1 > 0 or size2 > 0) and {1} order by lot_no asc, row_no asc
  211. """.format(codeString, filters)
  212. else:
  213. qMsData = """
  214. select * from data_ms where code='{0}' and (size1 > 0 or size2 > 0 ) order by lot_no asc, row_no asc
  215. """.format(codeString)
  216. return qMsData
  217. def updateLength(lotno):
  218. global cursor
  219. global wb
  220. q = "select * from data_wb where lot_no = '{0}' and judgement='OK' order by row_no asc".format(lotno)
  221. #print(q);
  222. return cursor.execute(q)
  223. def updateUb(lotno):
  224. global cursor
  225. global wb
  226. q = "select * from data_wb where lot_no = '{0}' and judgement='NG' order by row_no asc".format(lotno)
  227. #print(q);
  228. return cursor.execute(q)
  229. def actualChart(ws, title, data, position_data, body_data, cat_pos, position):
  230. '''
  231. for row in rows:
  232. ws.append(row)
  233. '''
  234. if isinstance(position_data, str):
  235. rs = ws[position_data]
  236. else:
  237. rs = tuple(ws.iter_rows(**position_data))
  238. for i in range(len(rs)):
  239. for j in range(len(rs[i])):
  240. print(rs[i][j])
  241. rs[i][j].value = data[i][j]
  242. #ws["A1"].value = rows
  243. #print(cells)
  244. print(rs)
  245. #return
  246. c1 = LineChart()
  247. c1.width = 10
  248. #c1.height = 10
  249. c1.title = title
  250. c1.style = 13
  251. c1.y_axis.title = 'Size'
  252. c1.x_axis.title = 'Lot'
  253. #print(ws.title+"?"+position_data)
  254. if isinstance(body_data, str):
  255. rdata = Reference(range_string=ws.title+"!"+body_data)
  256. else:
  257. rdata = Reference(ws, **body_data)
  258. c1.add_data(rdata, titles_from_data=True)
  259. if isinstance(cat_pos, str):
  260. lots = Reference(range_string=ws.title+"!"+cat_pos)
  261. else:
  262. lots = Reference(ws, **cat_pos)
  263. c1.set_categories(lots)
  264. # Style the lines
  265. s1 = c1.series[0]
  266. s1.marker.symbol = "triangle"
  267. s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
  268. s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline
  269. s1.graphicalProperties.line.noFill = True
  270. s2 = c1.series[1]
  271. s2.graphicalProperties.line.solidFill = "00AAAA"
  272. s2.graphicalProperties.line.dashStyle = "sysDot"
  273. s2.graphicalProperties.line.width = 100050 # width in EMUs
  274. s2 = c1.series[2]
  275. s2.smooth = True # Make the line smooth
  276. if isinstance(position,str):
  277. ws.add_chart(c1, position)
  278. else:
  279. ptemp = get_column_letter(position[1])
  280. print("ptemp = ", ptemp)
  281. ws.add_chart(c1, ptemp + str(position[0]))
  282. def createXBarChart(codeno, filters):
  283. global cursor
  284. global wb
  285. global dateStyle
  286. global codeMap
  287. ws = wb.create_sheet("X Bar,R Chart")
  288. #find lot in a codeno
  289. sql = generateSQLDataMS(codeno, filters)
  290. sql2 = findProductPress(codeno)
  291. cursor.execute(sql)
  292. row = cursor.fetchall()
  293. cursor.execute(sql2)
  294. row2 = cursor.fetchall()
  295. #print(list(row2))
  296. temp = {}
  297. #map data into lotno list
  298. #find max, min
  299. sizes = {}
  300. stdSizes = {}
  301. pressType1 = pressType2 = pressType3 = ""
  302. for k in row2:
  303. if k.PressType_1 != "" and k.PressType_1 is not None:
  304. sizes["size1"] = []
  305. stdSizes["size1"] = k
  306. pressType1 = k.PressType_1
  307. if k.PressType_2 != "" and k.PressType_2 is not None:
  308. sizes["size2"] = []
  309. stdSizes["size2"] = k
  310. pressType2 = k.PressType_2
  311. if k.Press_T != "" and k.Press_T is not None:
  312. sizes["tpresssize"] = []
  313. stdSizes["tpresssize"] = k
  314. pressType3 = "Thickness Press"
  315. num = 0
  316. for r in row:
  317. #print(r)
  318. if r.lot_no not in temp:
  319. temp[r.lot_no] = []
  320. temp[r.lot_no].append(r)
  321. for k,v in sizes.items():
  322. sizes[k].append(getattr(r, k, None))
  323. #print("temp = ", temp)
  324. #print("temp keys = ", temp.keys());
  325. for k,v in temp.items():
  326. #print("k = ", k)
  327. num = len(v)
  328. #break
  329. #print("num = ", num)
  330. if num > 5:
  331. num = 5
  332. #filter only dimensions in master
  333. #print("TEMP == \n")
  334. #print(sizes)
  335. avgSize = {}
  336. minSize = {}
  337. maxSize = {}
  338. data2 = []
  339. data2.append(["Lot No"])
  340. for k,v in temp.items():
  341. data2[0].append(k)
  342. #print("data 2 = ", data2)
  343. #create maxtrix in form
  344. '''
  345. Lot, 1, 2, 3, 4, 5, max, min
  346. xxx, v, v, v, v, v, v, v
  347. '''
  348. bp = 1
  349. for keySize, keyValue in sizes.items():
  350. l = []
  351. n0 = 0
  352. beginRow = 2
  353. #print("key size = %s\n" %(keySize) )
  354. for k,v in temp.items():
  355. a0 = []
  356. a0.append(k)
  357. sv = len(v)
  358. if sv > 5:
  359. sv = 5
  360. n0 = sv
  361. for i in range(sv):
  362. a0.append(getattr(v[i], keySize, 0))
  363. avg = sum(a0[1:]) / len(a0[1:])
  364. a0.append(avg)
  365. r = max(a0[1:]) - min(a0[1:])
  366. a0.append(r)
  367. #print("std size ", stdSizes[keySize])
  368. max_min = 0
  369. if k == "size1":
  370. max_min = stdSizes[k].PressWeight_1
  371. if k == "size2":
  372. max_min = stdSizes[k].PressWeight_2
  373. if k == "tpresssize":
  374. maxMaster = stdSizes[k].PressT + stdSizes[k].Press_T_Tol
  375. minMaster = stdSizes[k].PressT - stdSizes[k].Press_T_Tol
  376. else:
  377. maxMaster = minMaster = float(max_min)
  378. a0.append(maxMaster)
  379. a0.append(minMaster)
  380. l.append(a0)
  381. #print("l = ", l)
  382. header = ["Lot No"] + [str(i+1) for i in range(n0)] + ["avg", "r", "max", "min"]
  383. #print("header =", header)
  384. l = [header] + l;
  385. #print("l = ", l)
  386. '''
  387. tranpose above matrix into form:
  388. Lot,xxx, xxx, xxx,
  389. 1, v, v, v
  390. 2, v, v, v
  391. ...
  392. '''
  393. m, n = len(l), len(l[0])
  394. #print("mxn = ", m, n)
  395. ta = [ [0 for j in range(m)] for i in range(n)]
  396. #print("ta = ", ta)
  397. for i in range(len(l)):
  398. for j in range(len(l[i])):
  399. try:
  400. ta[j][i] = l[i][j]
  401. except Exception as e:
  402. return
  403. #print("TA ", ta)
  404. #print(len(ta), len(ta[0]))
  405. #print("===\n")
  406. #print(beginRow, beginRow + len(ta) - 1, bp, (bp-1) + len(ta[0]))
  407. wsRows = tuple(ws.iter_rows(min_row=beginRow, max_row=beginRow + len(ta) - 1, min_col=bp, max_col=(bp-1)+len(ta[0])))
  408. for i in range(len(wsRows)):
  409. for j in range(len(wsRows[i])):
  410. #print(i,j)
  411. wsRows[i][j].value = ta[i][j]
  412. wsRows[i][j].number_format = '0.00'
  413. bd = Side(style='medium', color="000000")
  414. wsRows[i][j].border = Border(left=bd, top=bd, right=bd, bottom=bd)
  415. if keySize == "size1":
  416. barTitle = pressType1
  417. if keySize == "size2":
  418. barTitle = pressType2
  419. if keySize == "tpresssize":
  420. barTitle = pressType3
  421. ws.cell(1, bp, value = "%s (bar)" % (barTitle))
  422. #bp += len(ta[0]) + 2
  423. #continue
  424. cpos = utils.get_column_letter(bp) + "15"
  425. font_test = drawingText.Font(typeface='Calibri')
  426. cp = CharacterProperties(latin=font_test, sz=1000)
  427. pp = ParagraphProperties(defRPr=cp)
  428. c1 = LineChart()
  429. cp1 = CharacterProperties(latin=font_test, sz=1400)
  430. pp1 = ParagraphProperties(defRPr=cp1)
  431. minValues = Reference(ws, min_row=beginRow+9, min_col=bp+1, max_row=beginRow+9, max_col=(bp-1)+len(ta[0]) )
  432. minSerie = Series(minValues, title="Min")
  433. c1.append(minSerie)
  434. maxValues = Reference(ws, min_row=beginRow+8, min_col=bp+1, max_row=beginRow+8, max_col=(bp-1)+len(ta[0]) )
  435. maxSerie = Series(maxValues, title="Max")
  436. c1.append(maxSerie)
  437. avgValues = Reference(ws, min_row=beginRow+6, min_col=bp+1, max_row=beginRow+6, max_col=(bp-1)+len(ta[0]) )
  438. avgSerie = Series(avgValues, title="Avg")
  439. avgSerie.marker.symbol = "circle"
  440. avgSerie.marker.graphicalProperties.solidFill = "4f81bd" # Marker filling
  441. avgSerie.graphicalProperties.line.solidFill = "4f81bd"
  442. c1.append(avgSerie)
  443. lots = Reference(ws,min_row=beginRow, min_col=bp+1, max_row=beginRow, max_col=(bp-1)+len(ta[0]))
  444. c1.set_categories(lots)
  445. c1.width = len(ta[0])*3.5
  446. if keySize == "size1":
  447. c1.title = "%s (X Bar) " % (pressType1)
  448. if keySize == "size2":
  449. c1.title = "%s (X Bar) " % (pressType2)
  450. if keySize == "tpresssize":
  451. c1.title = "%s (X Bar) " % (pressType3)
  452. #c1.title.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
  453. if keySize == "tpresssize":
  454. c1.y_axis.title = "Value(mm.)"
  455. else:
  456. c1.y_axis.title = "Value(mg.)"
  457. c1.x_axis.title = "Lot No."
  458. c1.x_axis.title.tx.rich.p[0].pPr = pp# Works!
  459. if c1.y_axis.title != None:
  460. c1.y_axis.title.tx.rich.p[0].pPr = pp# Works!
  461. c1.title.tx.rich.p[0].pPr = pp1# Works!
  462. c1.legend.position = "b"
  463. ws.add_chart(c1, cpos)
  464. cpos = utils.get_column_letter(bp) + "30"
  465. c2 = LineChart()
  466. #c2.title.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
  467. rValues = Reference(ws, min_row=beginRow+7, min_col=bp+1, max_row=beginRow+7, max_col=(bp-1)+len(ta[0]) )
  468. rSerie = Series(rValues, title="R")
  469. c2.append(rSerie)
  470. lots = Reference(ws,min_row=beginRow, min_col=bp+1, max_row=beginRow, max_col=(bp-1)+len(ta[0]))
  471. c2.set_categories(lots)
  472. c2.width = len(ta[0])*3.5
  473. if keySize == "size1":
  474. c2.title = "%s (R) " % (pressType1)
  475. if keySize == "size2":
  476. c2.title = "%s (R) " % (pressType2)
  477. if keySize == "tpresssize":
  478. c2.title = "%s (R) " % (pressType3)
  479. c2.y_axis.title = "Value(mm.)"
  480. c2.x_axis.title = "Lot No."
  481. c2.x_axis.title.tx.rich.p[0].pPr = pp# Works!
  482. c2.y_axis.title.tx.rich.p[0].pPr = pp# Works!
  483. c2.title.tx.rich.p[0].pPr = pp1# Works!
  484. c2.legend.position = "b"
  485. ws.add_chart(c2, cpos)
  486. #move column cursor
  487. bp += len(ta[0]) + 5
  488. #create each size table and chart types
  489. # actual chart
  490. def createPressCP(codeno, filters):
  491. global cursor
  492. global wb
  493. global dateStyle
  494. global codeMap
  495. ws = wb.create_sheet("Press CP")
  496. #find lot in a codeno
  497. sql = generateSQLDataMS(codeno, filters)
  498. sql2 = findProductPress(codeno)
  499. cursor.execute(sql)
  500. row = cursor.fetchall()
  501. cursor.execute(sql2)
  502. row2 = cursor.fetchall()
  503. temp = {}
  504. #map data into lotno list
  505. #find max, min
  506. sizes = {}
  507. stdSizes = {}
  508. #select distinct lot no
  509. bq = '''select ds.code,\
  510. ds.lot_no, max(ds.size1) maxsize1, min(ds.size1) minsize1, max(ds.size2) maxsize2, min(ds.size2) minsize2 ,\
  511. avg(ds.size1) avgSize1 , avg(ds.size2) avgSize2, stdev(ds.size1) sdsize1, stdev(ds.size2) sdsize2, \
  512. empids =
  513. STUFF((SELECT ',' + str(emp_id)
  514. FROM data_ms b
  515. WHERE b.lot_no = ds.lot_no
  516. group by emp_id
  517. FOR XML PATH('')), 1, 2, ''), \
  518. min(ds.created_at) minca,\
  519. pp.Mold_D, pp.Center_D, pp.PressWeight_1, pp.PressWeight_2, pp.PressType_1, pp.PressType_2, pp.UWeight, pp.Press_Ton, pp.PO_Qty \
  520. from data_ms ds \
  521. inner join AllProduct_PressPosition_PressWeight pp on pp.ProductCode = Replace('{0}', '-', '') \
  522. and pp.Lot_No = ds.lot_no \
  523. where \
  524. ds.code = '{0}' and ( (ds.size1 > 0) or (ds.size2 > 0) ) \
  525. group by ds.code, ds.lot_no, pp.Press_Ton, pp.PO_Qty, pp.Mold_D, pp.Center_D, pp.PressWeight_1, pp.PressWeight_2, pp.PressType_1, pp.PressType_2, pp.UWeight'''.format(codeno)
  526. #print("bq ", bq)
  527. cursor.execute(bq)
  528. cp_rows = cursor.fetchall()
  529. #print(cp_rows)
  530. max1Ub = ["Max1 Weight."]
  531. min1Ub = ["Min1 Weight."]
  532. max2Ub = ["Max2 Weight."]
  533. min2Ub = ["Min2 Weight."]
  534. avg1Ub = ["Avg1 Weight."]
  535. avg2Ub = ["Avg2 Weight."]
  536. r1Ub = ["R1"]
  537. r2Ub = ["R2"]
  538. sd1 = ["sd1"]
  539. sd2 = ["sd2"]
  540. w1 = ["Weight(1) std"]
  541. w2 = ["Weight(2) std"]
  542. unitW = ["Unit Weight"]
  543. op = ["Operator"]
  544. press = ["Press M/C"]
  545. holes = ["Hole (mm)"]
  546. dias = ["Dia (mm)"]
  547. qtys = ["QTY"]
  548. lotnos = ["Lot no."]
  549. inspecs = ["Inspection Date"]
  550. for r in cp_rows:
  551. max1Ub.append(r.maxsize1)
  552. min1Ub.append(r.minsize1)
  553. max2Ub.append(r.maxsize2)
  554. min2Ub.append(r.minsize2)
  555. avg1Ub.append(r.avgSize1)
  556. avg2Ub.append(r.avgSize2)
  557. r1Ub.append(r.maxsize1 - r.minsize1)
  558. r2Ub.append(r.maxsize2 - r.minsize2)
  559. sd1.append(r.sdsize1)
  560. sd2.append(r.sdsize2)
  561. w1.append(r.PressWeight_1)
  562. w2.append(r.PressWeight_2)
  563. unitW.append(r.UWeight)
  564. op.append(r.empids)
  565. press.append(r.Press_Ton)
  566. holes.append(r.Mold_D)
  567. dias.append(r.Center_D)
  568. qtys.append(r.PO_Qty)
  569. lotnos.append(r.lot_no)
  570. #print("xdfdsf = "+r.minca.strftime("%m/%d/%Y %H:%M:%S"))
  571. inspecs.append(r.minca.strftime("%m/%d/%Y %H:%M:%S"))
  572. dataArray = [
  573. max1Ub, #0
  574. min1Ub, #1
  575. max2Ub, #2
  576. min2Ub, #3
  577. avg1Ub, #4
  578. avg2Ub,#5
  579. r1Ub, #6
  580. r2Ub,
  581. sd1,
  582. sd2,#9
  583. w1,
  584. w2,
  585. unitW, #12
  586. op,
  587. press,
  588. holes,
  589. dias,
  590. qtys,
  591. lotnos,
  592. inspecs
  593. ]
  594. #print(dataArray)
  595. startRow = 20
  596. startCol = 2
  597. for i, row in enumerate(dataArray):
  598. for j, o in enumerate(row):
  599. cell = ws.cell(startRow + i, startCol +j)
  600. cell.value = o
  601. applyStyleName(cell, "allborder")
  602. lc = LineChart()
  603. lc.title = "Result CP Press Chart"
  604. lc.y_axis.title = "Press.weight (g)"
  605. lc.x_axis.title = "Lot No."
  606. lc.width = len(dataArray[0]) * 2.5
  607. #print("width ", lc.width)
  608. if lc.width <= 5:
  609. lc.width = 10
  610. enables = [0, 1,2,3,4,5, 10,11 ]
  611. for r in enables:
  612. data = Reference(ws, min_row=startRow+r, max_row=startRow+r,
  613. min_col=startCol, max_col=startCol + len(dataArray[0]))
  614. lc.add_data(data, from_rows=True, titles_from_data=True)
  615. lots = Reference(ws, min_row=startRow + 18, max_row=startRow+18, min_col=startCol+1, max_col=startCol+len(dataArray[0]) )
  616. lc.set_categories(lots)
  617. for se in lc.series:
  618. se.graphicalProperties.line.noFill = True
  619. se.marker.symbol = "auto"
  620. lc.series[6].graphicalProperties.line.noFill = False
  621. lc.series[7].graphicalProperties.line.noFill = False
  622. font_test = drawingText.Font(typeface='Calibri')
  623. cp = CharacterProperties(latin=font_test, sz=1000)
  624. pp = ParagraphProperties(defRPr=cp)
  625. cp1 = CharacterProperties(latin=font_test, sz=1400)
  626. pp1 = ParagraphProperties(defRPr=cp1)
  627. lc.x_axis.title.tx.rich.p[0].pPr = pp# Works!
  628. lc.y_axis.title.tx.rich.p[0].pPr = pp# Works
  629. lc.title.tx.rich.p[0].pPr = pp1# Works!
  630. lc.legend.position = "t"
  631. lc.marker = True
  632. ws.add_chart(lc, "B2")
  633. #create press thickness
  634. #find max, min
  635. sizes = {}
  636. stdSizes = {}
  637. #select distinct lot no
  638. bq = '''select ds.code,\
  639. ds.lot_no, max(ds.tpresssize) maxsize1, min(ds.tpresssize) minsize1,\
  640. avg(ds.tpresssize) avgSize1 , stdev(ds.tpresssize) sdsize1, \
  641. empids =
  642. STUFF((SELECT ',' + str(emp_id)
  643. FROM data_ms b
  644. WHERE b.lot_no = ds.lot_no
  645. group by emp_id
  646. FOR XML PATH('')), 1, 2, ''), \
  647. min(ds.created_at) minca,\
  648. pp.Mold_D, pp.Center_D, pp.Press_Ton, pp.PO_Qty, pp.Press_T \
  649. from data_ms ds \
  650. inner join AllProduct_PressPosition_PressWeight pp on pp.ProductCode = Replace('{0}', '-', '') \
  651. and pp.Lot_No = ds.lot_no \
  652. where \
  653. ds.code = '{0}' and ( (ds.size1 > 0) or (ds.size2 > 0) or (ds.tpresssize > 0)) \
  654. group by ds.code, ds.lot_no, pp.Press_Ton, pp.PO_Qty, pp.Mold_D, pp.Center_D, pp.Press_T'''.format(codeno)
  655. #print("bq ", bq)
  656. cursor.execute(bq)
  657. cp_rows = cursor.fetchall()
  658. #print(cp_rows)
  659. max1Ub = ["Max1 Weight."]
  660. min1Ub = ["Min1 Weight."]
  661. avg1Ub = ["Avg1 Weight."]
  662. r1Ub = ["R1"]
  663. sd1 = ["sd1"]
  664. w1 = ["Weight(1) std"]
  665. unitW = ["Unit Weight"]
  666. press_t = ["T Press"]
  667. op = ["Operator"]
  668. press = ["Press M/C"]
  669. holes = ["Hole (mm)"]
  670. dias = ["Dia (mm)"]
  671. qtys = ["QTY"]
  672. lotnos = ["Lot no."]
  673. inspecs = ["Inspection Date"]
  674. for r in cp_rows:
  675. max1Ub.append(r.maxsize1)
  676. min1Ub.append(r.minsize1)
  677. avg1Ub.append(r.avgSize1)
  678. r1Ub.append(r.maxsize1 - r.minsize1)
  679. sd1.append(r.sdsize1)
  680. press_t.append(r.Press_T)
  681. op.append(r.empids)
  682. press.append(r.Press_Ton)
  683. holes.append(r.Mold_D)
  684. dias.append(r.Center_D)
  685. qtys.append(r.PO_Qty)
  686. lotnos.append(r.lot_no)
  687. #print("xdfdsf = "+r.minca.strftime("%m/%d/%Y %H:%M:%S"))
  688. inspecs.append(r.minca.strftime("%m/%d/%Y %H:%M:%S"))
  689. dataArray = [
  690. max1Ub, #0
  691. min1Ub, #1
  692. avg1Ub, #2
  693. r1Ub, #3
  694. sd1, #4
  695. press_t, #5
  696. op,
  697. press,
  698. holes,
  699. dias,
  700. qtys,
  701. lotnos,
  702. inspecs
  703. ]
  704. #print(dataArray)
  705. startRow = 20
  706. startCol = 15
  707. for i, row in enumerate(dataArray):
  708. for j, o in enumerate(row):
  709. cell = ws.cell(startRow + i, startCol +j)
  710. cell.value = o
  711. applyStyleName(cell, "allborder")
  712. lc = LineChart()
  713. lc.title = "Result CP Thickness press Chart"
  714. lc.y_axis.title = "TPress (mm)"
  715. lc.x_axis.title = "Lot No."
  716. lc.width = len(dataArray[0]) * 2.5
  717. #print("width ", lc.width)
  718. if lc.width <= 5:
  719. lc.width = 10
  720. enables = [0, 1,2,3,4,5 ]
  721. for r in enables:
  722. data = Reference(ws, min_row=startRow+r, max_row=startRow+r,
  723. min_col=startCol, max_col=startCol + len(dataArray[0]))
  724. lc.add_data(data, from_rows=True, titles_from_data=True)
  725. lots = Reference(ws, min_row=startRow + 11, max_row=startRow+11, min_col=startCol+1, max_col=startCol+len(dataArray[0]) )
  726. lc.set_categories(lots)
  727. for se in lc.series:
  728. se.graphicalProperties.line.noFill = True
  729. se.marker.symbol = "auto"
  730. lc.series[5].graphicalProperties.line.noFill = False
  731. #lc.series[7].graphicalProperties.line.noFill = False
  732. font_test = drawingText.Font(typeface='Calibri')
  733. cp = CharacterProperties(latin=font_test, sz=1000)
  734. pp = ParagraphProperties(defRPr=cp)
  735. cp1 = CharacterProperties(latin=font_test, sz=1400)
  736. pp1 = ParagraphProperties(defRPr=cp1)
  737. lc.x_axis.title.tx.rich.p[0].pPr = pp# Works!
  738. lc.y_axis.title.tx.rich.p[0].pPr = pp# Works
  739. lc.title.tx.rich.p[0].pPr = pp1# Works!
  740. lc.legend.position = "t"
  741. lc.marker = True
  742. ws.add_chart(lc, "O2")
  743. def applyStyleName(cell, styleName, format=None):
  744. cell.style = styleName
  745. if format is not None:
  746. #print(format)
  747. cell.number_format = format
  748. def createChart(codeno, filters):
  749. global cursor
  750. global wb
  751. global dateStyle
  752. global codeMap
  753. #ws = wb.create_sheet("chart2")
  754. #find lot in a codeno
  755. sql = generateSQLDataMS(codeno, filters)
  756. sql2 = findProductPress(codeno)
  757. #print(filters)
  758. #print(sql)
  759. cursor.execute(sql)
  760. row = cursor.fetchall()
  761. cursor.execute(sql2)
  762. row2 = cursor.fetchall()
  763. temp = {}
  764. #map data into lotno list
  765. #find max, min
  766. sizes = {}
  767. stdSizes = {}
  768. pressType1 = pressType2 = ""
  769. for k in row2:
  770. if k.PressType_1 != "" and k.PressType_1 is not None:
  771. sizes["size1"] = []
  772. stdSizes["size1"] = k
  773. pressType1 = k.PressType_1
  774. if k.PressType_2 != "" and k.PressType_2 is not None:
  775. sizes["size2"] = []
  776. stdSizes["size2"] = k
  777. pressType2 = k.PressType_2
  778. if k.Press_T != "" and k.Press_T is not None:
  779. sizes["tpresssize"] = []
  780. stdSizes["tpresssize"] = k
  781. pressType3 = k.Press_T
  782. for r in row:
  783. #print(r)
  784. if r.lot_no not in temp:
  785. temp[r.lot_no] = []
  786. temp[r.lot_no].append(r)
  787. for k,v in sizes.items():
  788. sizes[k].append(getattr(r, k, None))
  789. #filter only dimensions in master
  790. #print("TEMP == \n")
  791. #print(sizes)
  792. avgSize = {}
  793. minSize = {}
  794. maxSize = {}
  795. for k, v in sizes.items():
  796. if len(v) > 0 and v[0] is not None:
  797. avgSize[k] = sum(v)/len(v)
  798. minSize[k] = min(v)
  799. maxSize[k] = max(v)
  800. #print("avg size \n",avgSize)
  801. #print("min size \n",minSize)
  802. #print("max size \n",maxSize)
  803. for k1,v1 in temp.items():
  804. #print("\nlot %s \n" %( k1))
  805. ws = wb.create_sheet("lot %s actual" %(k1))
  806. i = 0
  807. bp = 1
  808. for k,v in sizes.items():
  809. #print("%s actual" % (k))
  810. data = [
  811. ["Max (%s) size" % (k) ],
  812. ['Min'],
  813. ['Actual'],
  814. ['No.'],
  815. ['Lot No.']
  816. ]
  817. nrow = len(data)
  818. for v2 in v1:
  819. #print("std size ", stdSizes[keySize])
  820. max_min = 0
  821. if k == "size1":
  822. max_min = stdSizes[k].PressWeight_1
  823. if k == "size2":
  824. max_min = stdSizes[k].PressWeight_2
  825. if k == "tpresssize":
  826. max_min = stdSizes[k].Press_T
  827. if max_min is None:
  828. continue
  829. if k == "size1" or k == "size2":
  830. maxMaster = minMaster = float(max_min)
  831. else:
  832. maxMaster = stdSizes[k].Press_T + stdSizes[k].Press_T_Tol
  833. minMaster = stdSizes[k].Press_T - stdSizes[k].Press_T_Tol
  834. data[0].append(maxMaster)
  835. data[1].append(minMaster)
  836. data[2].append(getattr(v2, k))
  837. data[3].append(v2.row_no)
  838. data[4].append(v2.lot_no)
  839. #print(data)
  840. #gen chart
  841. #print("min col ", bp)
  842. wsRows = tuple(ws.iter_rows(min_row=1, max_row=len(data), min_col=bp, max_col= (bp - 1)+len(data[0])))
  843. for i in range(len(wsRows)):
  844. for j in range(len(wsRows[i])):
  845. #print(i,j)
  846. #data[i][j])
  847. #print(wsRows[i][j])
  848. wsRows[i][j].value = data[i][j]
  849. if i != 3:
  850. wsRows[i][j].number_format = '0.00'
  851. bd = Side(style='medium', color="000000")
  852. wsRows[i][j].border = Border(left=bd, top=bd, right=bd, bottom=bd)
  853. cpos = utils.get_column_letter(bp) + "10"
  854. font_test = drawingText.Font(typeface='Calibri')
  855. cp = CharacterProperties(latin=font_test, sz=1000)
  856. pp = ParagraphProperties(defRPr=cp)
  857. cp1 = CharacterProperties(latin=font_test, sz=1400)
  858. pp1 = ParagraphProperties(defRPr=cp1)
  859. c1 = LineChart()
  860. maxValues = Reference(ws, min_row=1, min_col=bp+1, max_row=1, max_col=(bp-1)+len(data[0]) )
  861. maxSerie = Series(maxValues, title="Max")
  862. maxSerie.graphicalProperties.line.solidFill = "FF0000"
  863. c1.append(maxSerie)
  864. minValues = Reference(ws, min_row=2, min_col=bp+1, max_row=2, max_col=(bp-1)+len(data[0]) )
  865. minSerie = Series(minValues, title="Min")
  866. minSerie.graphicalProperties.line.solidFill = "000000"
  867. c1.append(minSerie)
  868. acValues = Reference(ws, min_row=3, min_col=bp+1, max_row=3, max_col=(bp-1)+len(data[0]) )
  869. acSerie = Series(acValues, title="Actual")
  870. acSerie.marker.graphicalProperties.solidFill = "4f81bd" # Marker filling
  871. acSerie.graphicalProperties.line.solidFill = "4f81bd"
  872. acSerie.marker.symbol = "circle"
  873. c1.append(acSerie)
  874. lots = Reference(ws,min_row=4, min_col=bp+1, max_row=4, max_col=(bp-1)+len(data[0]))
  875. c1.set_categories(lots)
  876. c1.width = len(data[0])*1.8
  877. if k == "size1":
  878. c1.title = "%s (X Bar) " % (pressType1)
  879. if k == "size2":
  880. c1.title = "%s (X Bar) " % (pressType2)
  881. if k == "tpresssize":
  882. c1.title = "%s (X Bar) " % ("Thickness press")
  883. c1.y_axis.title = "Value(mm.)"
  884. c1.x_axis.title = "Item No."
  885. c1.x_axis.title.tx.rich.p[0].pPr = pp# Works!
  886. c1.y_axis.title.tx.rich.p[0].pPr = pp# Works!
  887. c1.title.tx.rich.p[0].pPr = pp1# Works!
  888. c1.legend.position = "b"
  889. ws.add_chart(c1, cpos)
  890. bp += len(data[0]) + 2
  891. #print()
  892. #print("M"+str(bp))
  893. #bp += coEndCell[1]+1+10
  894. #print(ws.calculate_dimension())
  895. #create each size table and chart types
  896. # actual chart
  897. def createExcel(codeno, filters):
  898. global cursor
  899. global wb
  900. global dateStyle
  901. wb["Styles"].sheet_state = 'hidden'
  902. ws = wb["Data Measurement"]
  903. codeString = ""
  904. if codeno.find('-') != -1:
  905. codeString = codeno
  906. else:
  907. codeString = codeno[0] + "-" + codeno[1:5] + "-" + codeno[5:9] + "-" + codeno[9:10]
  908. # std sizes
  909. stdSizes = """
  910. select * from AllProduct_PressPosition_PressWeight where ProductCode='{0}'""".format(codeString.replace("-", ""))
  911. #print(stdSizes)
  912. cursor.execute(stdSizes)
  913. rows = cursor.fetchall()
  914. stdSizeMapColumn = {
  915. "D": "F",
  916. "T": "G",
  917. "H": "H",
  918. "A": "I",
  919. "B": "J",
  920. "C": "K",
  921. "E": "L",
  922. "F": "M",
  923. "G": "N",
  924. "J": "O",
  925. "K": "P",
  926. "L": "Q",
  927. "M": "R",
  928. "N": "S",
  929. "O": "T",
  930. "P": "U",
  931. "U": "V",
  932. "Q": "W",
  933. "R": "X",
  934. "S": "Y",
  935. "W": "Z",
  936. "Y": "AA",
  937. "Z": "AB",
  938. "X1": "AC",
  939. "X2": "AD",
  940. "Thickness": "AE",
  941. "Flatness": "AF",
  942. "Cen": "AG",
  943. "Weight": "AH",
  944. "Result": "AI",
  945. }
  946. '''
  947. for k,v in stdSizeMapColumn.items():
  948. if k not in ["Weight", "Result"]:
  949. ws.column_dimensions[v].hidden = True
  950. '''
  951. enableCols = []
  952. for r in rows:
  953. if r.PressType_1 != "" and r.PressType_1 is not None:
  954. ws["F8"] = r.PressWeight_1
  955. ws["F10"] = r.PressType_1
  956. else:
  957. ws.column_dimensions["F"].hidden = True
  958. if r.PressType_2 != "" and r.PressType_2 is not None:
  959. ws["G8"] = r.PressWeight_1
  960. ws["G10"] = r.PressType_1
  961. else:
  962. ws.column_dimensions["G"].hidden = True
  963. if r.Press_T != "" and r.Press_T is not None:
  964. ws["I8"] = r.Press_T
  965. else:
  966. ws.column_dimensions["I"].hidden = True
  967. if filters is not None:
  968. qMsData = """
  969. select a.*, b.v1, b.v2, b.ac1, b.ac2, b.ac3, b.ac4, b.ac5, b.ac6, b.c0 from data_ms a
  970. left join press_cal b
  971. on a.lot_no = b.lot_no
  972. where code='{0}' and ((size1 > 0) or (size2 > 0 ) or (tpresssize > 0)) and {1}
  973. order by lot_no asc, row_no asc
  974. """.format(codeString, filters.replace("created_at", "a.created_at"))
  975. else:
  976. qMsData = """
  977. select a.*, b.v1, b.v2, b.ac1, b.ac2, b.ac3, b.ac4, b.ac5, b.ac6, b.c0 from data_ms a
  978. left join press_cal b
  979. on a.lot_no = b.lot_no
  980. where code='{0}' and ((size1 > 0) or (size2 > 0 ) or (tpresssize > 0))
  981. order by lot_no asc, row_no asc
  982. """.format(codeString)
  983. cursor.execute(qMsData)
  984. rows = cursor.fetchall()
  985. startRow = 11
  986. for r in rows:
  987. isNG = False
  988. if int(r.row_no) == 1:
  989. ws.cell(column = 1, row = startRow, value = r.created_at).number_format = "m/d/yy"
  990. applyStyleName(ws.cell(column=1, row=startRow),"allborder", "m/d/yy hh:mm")
  991. ws.cell(column = 2, row = startRow, value = r.code)
  992. applyStyleName(ws.cell(column=2, row=startRow), "allborder")
  993. ws.cell(column = 3, row = startRow, value=r.lot_no)
  994. applyStyleName(ws.cell(column=3, row=startRow), "allborder")
  995. countLot = """
  996. select count(*) as c from data_ms where lot_no = '{0}'
  997. """.format(r.lot_no)
  998. cursor.execute(countLot)
  999. countResult = cursor.fetchall()
  1000. #print(countResult)
  1001. ws.cell(column = 4, row = startRow, value = countResult[0].c).number_format = "0"
  1002. applyStyleName(ws.cell(column=4, row=startRow), "allborder")
  1003. c = ws.cell(column=5, row = startRow, value = int(r.row_no))
  1004. applyStyleName(ws.cell(column=5, row=startRow), "allborder")
  1005. c.number_format = "0"
  1006. colstr = "F"
  1007. #print("x1", r.x1size)
  1008. ws[colstr + str(startRow)] = r.size1
  1009. applyStyleName(ws[colstr + str(startRow)], "allborder")
  1010. if r.size1Ok == "NG":
  1011. isNG = True
  1012. ws[colstr + str(startRow)].font = Font(color = colors.RED)
  1013. colstr = "G"
  1014. ws[colstr + str(startRow)] = r.size2
  1015. applyStyleName(ws[colstr + str(startRow)], "allborder")
  1016. if r.size2Ok == "NG":
  1017. isNG = True
  1018. ws[colstr + str(startRow)].font = Font(color = colors.RED)
  1019. colstr = "I"
  1020. ws[colstr + str(startRow)] = r.tpresssize
  1021. applyStyleName(ws[colstr + str(startRow)], "allborder")
  1022. if r.tpresssizeOk == "NG":
  1023. isNG = True
  1024. ws[colstr + str(startRow)].font = Font(color = colors.RED)
  1025. colstr = "J"
  1026. ws[colstr + str(startRow)] = r.tpresssizeOk
  1027. applyStyleName(ws[colstr + str(startRow)], "allborder")
  1028. if r.tpresssizeOk == "NG":
  1029. ws[colstr + str(startRow)].font = Font(color = colors.RED)
  1030. colstr = "K"
  1031. r.v1 = r.v1 or 0
  1032. r.v2 = r.v2 or 0
  1033. ws[colstr + str(startRow)] = r.v1 + r.v2
  1034. ws[colstr + str(startRow)].number_format = "#,##0.00"
  1035. applyStyleName(ws[colstr + str(startRow)], "allborder")
  1036. colstr = "H"
  1037. if isNG == True:
  1038. ws[colstr+ str(startRow)] = "NG"
  1039. ws[colstr+ str(startRow)].font = Font(color = colors.RED)
  1040. else:
  1041. ws[colstr+ str(startRow)] = "OK"
  1042. ws["N"+ str(startRow)] = r.ac1 or 0
  1043. applyStyleName(ws["N" + str(startRow)], "allborder")
  1044. ws["O"+ str(startRow)] = r.ac2 or 0
  1045. applyStyleName(ws["O" + str(startRow)], "allborder")
  1046. ws["P"+ str(startRow)] = r.ac3 or 0
  1047. applyStyleName(ws["P" + str(startRow)], "allborder")
  1048. ws["Q"+ str(startRow)] = r.ac4 or 0
  1049. applyStyleName(ws["Q" + str(startRow)], "allborder")
  1050. ws["R"+ str(startRow)] = r.ac5 or 0
  1051. applyStyleName(ws["R" + str(startRow)], "allborder")
  1052. ws["S"+ str(startRow)] = r.ac6 or 0
  1053. applyStyleName(ws["S" + str(startRow)], "allborder")
  1054. ws["T"+ str(startRow)] = r.c0 or 0
  1055. applyStyleName(ws["T" + str(startRow)], "allborder")
  1056. '''
  1057. colstr = stdSizeMapColumn.get("X")
  1058. ws[colstr+str(startRow)].value = r.xsize
  1059. if r.xsizeOk == "NG":
  1060. ws[colstr+ str(startRow)].font = Font(color = colors.RED)
  1061. '''
  1062. #ws['U'+str(startRow)] = r.ysize or 0
  1063. #ysize.font= Font(color = colors.Red)
  1064. #ws['V'+str(startRow)] = r.zsize
  1065. startRow += 1
  1066. #print(ws._charts)
  1067. cust = findCustomer(codeno.replace("-", ""))
  1068. ws["B3"] = codeno.replace("-", "")
  1069. if cust:
  1070. ws["B2"] = cust[0].customer
  1071. ws["B4"] = cust[0].sizestring
  1072. ws["B5"] = cust[0].spec
  1073. if __name__ == "__main__":
  1074. env = sys.argv[1]
  1075. code = sys.argv[2]
  1076. #print(len(sys.argv))
  1077. df = None
  1078. if len(sys.argv) == 4:
  1079. filter = sys.argv[3]
  1080. #import ast
  1081. #df = ast.literal_eval(filter)
  1082. #print(df['date'])
  1083. df = filter
  1084. global cnxn
  1085. global cursor
  1086. if env == "prod":
  1087. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.0.253;DATABASE=OB2011DB;UID=user1;PWD=1234')
  1088. cursor = cnxn.cursor()
  1089. else:
  1090. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=OB2011DB;UID=admin;PWD=1234')
  1091. cursor = cnxn.cursor()
  1092. #print("code is "+code)
  1093. createExcel(code, df)
  1094. createChart(code, df)
  1095. createXBarChart(code, df)
  1096. createPressCP(code, df)
  1097. #createXRangeChart(code, df)
  1098. ut = int(time.time())
  1099. filename = "press{0}_{1}.xlsx".format(code.replace("-", ""), ut)
  1100. outfile = './public/excel/'+filename
  1101. wb.save(filename = outfile)
  1102. print(filename)