| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 |
- from openpyxl import load_workbook
- import pyodbc
- import time
- import sys
- from pprint import pprint
- import datetime
- from openpyxl.styles import NamedStyle, Font
- from copy import deepcopy
- from openpyxl.chart import LineChart, Reference, Series
- from openpyxl.chart.text import RichText
- from openpyxl.drawing.text import RichTextProperties,Paragraph,ParagraphProperties, CharacterProperties
- # Specifying the ODBC driver, server name, database, etc. directly
- #cnxn = None
- #cursor = None
- '''cursor.execute('select * from data_wb')
- rows = cursor.fetchall()
- for row in rows:
- print(row)'''
- wb = load_workbook(filename = './dctemplate2.xlsx')
- #wb.save(filename = './createfrompy.xlsx');
- def findCustomer(code):
- global cursor
- global env
- tempResult = []
- views = []
- if env == "prod":
- views = ["bel_master_view", "e_master_view", "mg_master_view", "v_master_view"]
- else:
- views = ["bel_master_view_dev", "e_master_view_dev", "mg_master_view_dev", "v_master_view_dev"]
- ts = []
- 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"
- where = "PRO1 = '{0}'".format(code)
- for v in views:
- sqlQuery = "select {0} from {1} where {2}".format(select, v, where)
- ts.append(sqlQuery)
- merged = []
- for t in ts:
- merged += cursor.execute(t)
- return merged
- def findInMasterView(select, where):
- global cursor
- global env
- tempResult = []
- views = []
- if env == "prod":
- views = ["bel_master_view", "e_master_view", "mg_master_view", "v_master_view"]
- else:
- views = ["bel_master_view_dev", "e_master_view_dev", "mg_master_view_dev", "v_master_view_dev"]
- ts = []
- for v in views:
- sqlQuery = "select {0} from {1} where {2}".format(select, v, where)
- ts.append(sqlQuery);
- qj = ';'.join(ts)
- #print(qj)
- merged = []
- for t in ts:
- merged += cursor.execute(t)
- return merged
- def updateLength(lotno):
- global cursor
- global wb
- q = "select * from data_wb where lot_no = '{0}' and judgement='OK' order by row_no asc".format(lotno)
- #print(q);
- return cursor.execute(q)
- def updateUb(lotno):
- global cursor
- global wb
- q = "select * from data_wb where lot_no = '{0}' and judgement='NG' order by row_no asc".format(lotno)
- #print(q);
- return cursor.execute(q)
- def getMachineName(machineId):
- global cursor
- q = "select * from machines_wb where id = {0}".format(machineId)
- #print(q)
- cursor.execute(q)
- rs = cursor.fetchall()
- return rs[0].name
- def getMachineDetail(machineId):
- global cursor
- q = "select * from machines_wb where id = {0}".format(machineId)
- #print(q)
- cursor.execute(q)
- rs = cursor.fetchall()
- return rs[0]
- def createDayChart(chartTitle):
- global wb
- ws = wb['Chart']
- wsd = wb['Daily Check']
- c1 = LineChart()
- c1.legend.position = "t";
- c1.title = chartTitle
- c1.title.font = Font(underline=Font.UNDERLINE_SINGLE)
- c1.y_axis.title = "Std. Weight(g)"
- c1.y_axis.number_format = "#,##0.00"
- c1.x_axis.title = "Date"
- c1.width = 30
- c1.height = 10
- #print(len(values))
- MINDATACOL = 2
- MAXDATACOL = 2 + 31
- lt = Reference(wsd, min_col=3, max_col=33, min_row=5, max_row=5)
- std = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=6, max_row=6)
- data = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=10, max_row=10)
- maxUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=15, max_row=15)
- minUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=21, max_row=21)
- avgUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=26, max_row=26)
- vsStd = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=32, max_row=32)
- vsStd2 = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=37, max_row=37)
- c1.add_data(data, from_rows=True, titles_from_data=True)
- c1.add_data(maxUB, from_rows=True, titles_from_data=True)
- c1.add_data(minUB, from_rows=True, titles_from_data=True)
- c1.add_data(avgUB, from_rows=True, titles_from_data=True)
- c1.add_data(vsStd, from_rows=True, titles_from_data=True)
- c1.add_data(vsStd2, from_rows=True, titles_from_data=True)
- c1.add_data(std, from_rows=True, titles_from_data=True)
- s1 = c1.series[0]
- s1.marker.symbol = "triangle"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[1]
- s1.marker.symbol = "circle"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[2]
- s1.marker.symbol = "square"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[3]
- s1.marker.symbol = "auto"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[4]
- s1.marker.symbol = "diamond"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[5]
- s1.marker.symbol = "auto"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[6]
- #s1.marker.symbol = "diamond"
- #s1.graphicalProperties.line.noFill = True
- c1.set_categories(lt)
- '''c1.x_axis.txPr = RichText(bodyPr=RichTextProperties(anchor="ctr",anchorCtr="1",rot="-2700000",
- spcFirstLastPara="1",vertOverflow="ellipsis",wrap="square"),
- p=[Paragraph(pPr=ParagraphProperties(defRPr=CharacterProperties()), endParaRPr=CharacterProperties())])'''
- ws.add_chart(c1, "b3")
- #pass
- def createNightChart(chartTitle):
- global wb
- ws = wb['Chart']
- wsd = wb['Daily Check']
- c1 = LineChart()
- c1.legend.position = "t";
- c1.title = chartTitle
- c1.title.font = Font(underline=Font.UNDERLINE_SINGLE)
- c1.y_axis.title = "Std. Weight(g)"
- c1.y_axis.number_format = "#,##0.00"
- c1.x_axis.title = "Date"
- c1.width = 30
- c1.height = 10
- #print(len(values))
- MINDATACOL = 2
- MAXDATACOL = 2 + 31
- lt = Reference(wsd, min_col=3, max_col=33, min_row=5, max_row=5)
- std = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=6, max_row=6)
- #begin data
- data = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=51, max_row=51)
- maxUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=56, max_row=56)
- minUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=62, max_row=62)
- avgUB = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=67, max_row=67)
- vsStd = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=73, max_row=73)
- vsStd2 = Reference(wsd, min_col=MINDATACOL, max_col=MAXDATACOL, min_row=78, max_row=78)
- c1.add_data(data, from_rows=True, titles_from_data=True)
- c1.add_data(maxUB, from_rows=True, titles_from_data=True)
- c1.add_data(minUB, from_rows=True, titles_from_data=True)
- c1.add_data(avgUB, from_rows=True, titles_from_data=True)
- c1.add_data(vsStd, from_rows=True, titles_from_data=True)
- c1.add_data(vsStd2, from_rows=True, titles_from_data=True)
- c1.add_data(std, from_rows=True, titles_from_data=True)
- s1 = c1.series[0]
- s1.marker.symbol = "triangle"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[1]
- s1.marker.symbol = "circle"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[2]
- s1.marker.symbol = "square"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[3]
- s1.marker.symbol = "auto"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[4]
- s1.marker.symbol = "diamond"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[5]
- s1.marker.symbol = "auto"
- s1.graphicalProperties.line.noFill = True
- s1 = c1.series[6]
- #s1.marker.symbol = "diamond"
- #s1.graphicalProperties.line.noFill = True
- c1.set_categories(lt)
- '''c1.x_axis.txPr = RichText(bodyPr=RichTextProperties(anchor="ctr",anchorCtr="1",rot="-2700000",
- spcFirstLastPara="1",vertOverflow="ellipsis",wrap="square"),
- p=[Paragraph(pPr=ParagraphProperties(defRPr=CharacterProperties()), endParaRPr=CharacterProperties())])'''
- ws.add_chart(c1, "b22")
- def createExcel(codeno, filters):
- global cursor
- global wb
- ws = wb["Daily Check"]
- machineName = getMachineName(codeno)
- machineDetail = getMachineDetail(codeno)
- ws["C2"].value = machineName
- ws["C3"].value = filters.replace("created_at between", "").replace("and", "to").replace("'", "")
- ws["C43"].value = machineName
- ws["C44"].value = filters.replace("created_at between", "").replace("and", "to").replace("'", "")
- ws["C4"].value = machineDetail.machineStd;
- if filters is not None:
- q = "select * from daily_checks_wb where machine_id = '{0}' and {1}".format(codeno, filters)
- else:
- q = "select * from daily_checks_wb where machine_id = '{0}'".format(codeno)
- #print(q)
- cursor.execute(q)
- rows = cursor.fetchall()
- #print(rows)
- a0740 = [];
- a1230 = []
- a1700 = []
- a0030 = []
- a1940 = []
- a0500 = []
- for i in range(31):
- a0740.append([])
- a1230.append([])
- a1700.append([])
- a0030.append([])
- a1940.append([])
- a0500.append([])
- for r in rows:
- i = r.created_at.day
- if r.shift == "07.40":
- a0740[i-1].append(r)
- if r.shift == "12.30":
- a1230[i-1].append(r)
- if r.shift == "17.00":
- #a1700.append(r)
- a1700[i-1].append(r)
- if r.shift == "00.30":
- a0030[i-1].append(r)
- if r.shift == "19.40":
- a1940[i-1].append(r)
- if r.shift == "05.00":
- a0500[i-1].append(r)
- #print(a0740)
- for idx, val in enumerate(a0740):
- j = 7
- k = 12
- users = []
- #print(val)
- ok = 0
- ng = 0
- for v in val:
- #print(j , idx + )
- #print(v.judgment)
- if v.judgement == "OK":
- ok += 1
- if ok > 3:
- continue
- ws.cell(row = j , column = idx + 3 , value = v.weight)
- j += 1
- if v.judgement == "NG":
- ng += 1
- if ng > 3:
- continue
- ws.cell(row = k , column = idx + 3 , value = v.weight)
- k += 1
- #print(v.empid)
- if v.empid not in users:
- #print("insert in users")
- users.append(v.empid)
- #print(val)
- if len(val) > 0:
- q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
- #print(q)
- cursor.execute(q)
- us = cursor.fetchall()
- names = [x.fname + " " + x.lname for x in us]
- ws.cell(row = 17, column = idx + 3, value = ",".join(names))
- for idx, val in enumerate(a1230):
- j = 18
- k = 23
- users = []
- ok = ng = 0
- for v in val:
- #print(j , idx)
- if v.judgement == "OK":
- ok += 1
- if ok > 3:
- continue
- ws.cell(row = j , column = idx + 3 , value = v.weight)
- j += 1
- if v.judgement == "NG":
- ng += 1
- if ng > 3:
- continue
- ws.cell(row = k , column = idx + 3 , value = v.weight)
- k += 1
- if v.empid not in users:
- users.append(v.empid)
- if len(val) > 0:
- q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
- #print(q)
- cursor.execute(q)
- us = cursor.fetchall()
- names = [x.fname + " " + x.lname for x in us]
- ws.cell(row = 28, column = idx + 3, value = ",".join(names))
- for idx, val in enumerate(a1700):
- j = 29
- k = 34
- users = []
- ok = ng = 0
- for v in val:
- #print(j , idx)
- if v.judgement == "OK":
- ok += 1
- if ok > 3:
- continue
- ws.cell(row = j , column = idx + 3 , value = v.weight)
- j += 1
- if v.judgement == "NG":
- ng += 1
- if ng > 3:
- continue
- ws.cell(row = k , column = idx + 3 , value = v.weight)
- k += 1
- if v.empid not in users:
- users.append(v.empid)
- if len(val) > 0:
- q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
- #print(q)
- cursor.execute(q)
- us = cursor.fetchall()
- names = [x.fname + " " + x.lname for x in us]
- ws.cell(row = 39, column = idx + 3, value = ",".join(names))
- for idx, val in enumerate(a1940):
- j = 47
- k = 53
- users = []
- ok = ng = 0
- for v in val:
- #print(j , idx)
- if v.judgement == "OK":
- ok += 1
- if ok > 3:
- continue
- ws.cell(row = j , column = idx + 3 , value = v.weight)
- j += 1
- if v.judgement == "NG":
- ng += 1
- if ng > 3:
- continue
- ws.cell(row = k , column = idx + 3 , value = v.weight)
- k += 1
- if v.empid not in users:
- users.append(v.empid)
- if len(val) > 0:
- q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
- #print(q)
- cursor.execute(q)
- us = cursor.fetchall()
- names = [x.fname + " " + x.lname for x in us]
- ws.cell(row = 58, column = idx + 3, value = ",".join(names))
- for idx, val in enumerate(a0030):
- j = 59
- k = 64
- users = []
- ok = ng = 0
- for v in val:
- #print(j , idx)
- if v.judgement == "OK":
- ok += 1
- if ok > 3:
- continue
- ws.cell(row = j , column = idx + 3 , value = v.weight)
- j += 1
- if v.judgement == "NG":
- ng += 1
- if ng > 3:
- continue
- ws.cell(row = k , column = idx + 3 , value = v.weight)
- k += 1
- if v.empid not in users:
- users.append(v.empid)
- if len(val) > 0:
- q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
- #print(q)
- cursor.execute(q)
- us = cursor.fetchall()
- names = [x.fname + " " + x.lname for x in us]
- ws.cell(row = 69, column = idx + 3, value = ",".join(names))
- for idx, val in enumerate(a0500):
- j = 70
- k = 75
- users = []
- ok = ng = 0
- for v in val:
- #print(j , idx)
- if v.judgement == "OK":
- ok += 1
- if ok > 3:
- continue
- ws.cell(row = j , column = idx + 3 , value = v.weight)
- j += 1
- if v.judgement == "NG":
- ng += 1
- if ng > 3:
- continue
- ws.cell(row = k , column = idx + 3 , value = v.weight)
- k += 1
- if v.empid not in users:
- users.append(v.empid)
- if len(val) > 0:
- q = "select * from users where empid in ({0})".format(",".join("'{0}'".format(w) for w in users))
- #print(q)
- cursor.execute(q)
- us = cursor.fetchall()
- names = [x.fname + " " + x.lname for x in us]
- ws.cell(row = 80, column = idx + 3, value = ",".join(names))
- createDayChart("Daily Check of Day Shift ({0})".format(machineName))
- createNightChart("Daily Check of Night Shift ({0})".format(machineName))
- ut = int(time.time())
- filename = "dc{0}.xlsx".format(str(ut))
- outfile = './public/excel/'+filename
- wb.save(filename = outfile)
- print(filename)
- if __name__ == "__main__":
- env = sys.argv[1]
- machine = sys.argv[2]
- #print(len(sys.argv))
- df = None
- if len(sys.argv) == 4:
- filter = sys.argv[3]
- #import ast
- #df = ast.literal_eval(filter)
- #print(df['date'])
- df = filter
- global cnxn
- global cursor
- if env == "prod":
- cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=192.168.0.253;DATABASE=OB2011DB;UID=user1;PWD=1234')
- cursor = cnxn.cursor()
- else:
- cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=OB2011DB;UID=admin;PWD=1234')
- cursor = cnxn.cursor()
- #print("code is "+code)
- createExcel(machine, df)
|