-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsticker-generator.py
481 lines (390 loc) · 18.6 KB
/
sticker-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/usr/bin/env python
###################################################
# sticker-generator.py
# Author: nathan
# Date: 26/04/2018
# Brief: a python program to scrape a jobs txt file, save
# all the parts into arrays and then generate a pdf file
# that is to be printed on the AVERY *(L7157REV)* sticker labels
###################################################
import labels
from reportlab.graphics import shapes
from collections import namedtuple
import os.path
import sys
import re
import shutil
print
print " N4tH4N's "
print ".__ ___. .__ "
print "| | _____ \_ |__ ____ | | "
print "| | \__ \ | __ \_/ __ \| | "
print "| |__/ __ \| \_\ \ ___/| |__ "
print "|____(____ /___ /\___ >____/ "
print " \/ \/ __ \/ "
print " _____ _____ | | __ ___________ "
print " / \\\\__ \ | |/ // __ \_ __ \\"
print " | Y Y \/ __ \| <\ ___/| | \/"
print " |__|_| (____ /__|_ \\\\___ >__| "
print " \/ \/ \/ \/ "
print
###########################################################################
############ Defining and opening the .txt file to work on ##########
###########################################################################
# prompting the user for the job number
user_input = raw_input("## Enter the Job Number: ")
# combining the 'user_input' string with the extension '.txt'
input_file = user_input + ".txt"
if os.path.isfile(input_file):
txt_file = open(input_file, "r")
contents = txt_file.read()
txt_file_lines = contents.splitlines() # have to do this because python adds extra line breaks
txt_file.close()
else:
print
print " WhAt??!?!?"
print " That file doesn't seem to exist..."
print " Are you sure you exported the job from iTMS?"
print
sys.exit(-1)
print
print
#########################################################################
####### Defining whether to print labels or run "test mode" ########
#########################################################################
print
print " +-------------------------------+"
print " | (1) Generate Labels |"
print " | (2) Test Mode |"
print " +-------------------------------+"
print
mode = raw_input("# Please enter an option number: ")
print_labels = False
test_mode = False
if mode == "1":
print_labels = True
elif mode == "2":
test_mode = True
##################################################################
####### Finding all the lines that have * - number * ########
##################################################################
ticket_line_number_array = []
customer = ""
job_number = ""
counter = 1
for line in txt_file_lines:
# finding the start of each job ticket
# using regex to find lines that contain for e.g. *88931-1*
if re.match("^\*.*-[0-9]*\*", line) is not None:
ticket_line_number_array.append(counter)
# the customers name is always on line 2
if counter == 2:
customer = line
# the fist job ticket start on line 3
elif counter == 3:
job_number = line
# removing the '*' from the job number
job_number = job_number.replace("*", "")
# removing everything after the '-' to leave only the job number left
job_number = re.sub("-.*$", "", job_number)
counter += 1
array_length = len(ticket_line_number_array)
print "Customer =", customer
print "Job Number =", job_number
print "The number of parts in the job =", array_length
###########################################################
############ Defining the customer variable #############
###########################################################
if customer == "G H VARLEY - TOMAGO DEFENCE":
customer = "VARLEY_TOMAGO_DEFENCE"
elif customer == "G H VARLEY - TOMAGO (SCHOOL DRIVE)" or customer == "G H VARLEY - BNE" or customer == "G H VARLEY (SYDNEY)":
customer = "VARLEY"
elif customer == "TRITIUM PTY LTD":
customer = "TRITIUM"
elif customer == "BELL ENVIRONMENTAL":
customer = "BELL"
##################################################################
############ Creating the Client Part Number Array #############
##################################################################
client_part_number_array = []
for i in ticket_line_number_array:
lines_ahead_array = []
for counter, line in enumerate(txt_file_lines, 1):
if counter < ( i + 12 ) and counter > ( i + 2 ):
lines_ahead_array.append(line)
if customer == "VARLEY" or customer == "BELL":
for j, item in enumerate(lines_ahead_array):
if j < 3 and item.find("Issue Date") >= 0:
# going back by one line and splitting the string by {TAB}
client_part_number_array.append(lines_ahead_array[j-1].split("\t")[1])
elif j > 4 and item.find("Issue Date") >= 0:
# going back by two lines
client_part_number_array.append(lines_ahead_array[j-2])
# TRITIUM and VARLEY TOMAGO DEFENCE get the client part number from the line below 'Part Description'
# once it finds 'Part Description', it will look one line ahead, then split the line at the first space " "
# and keep the first string that is there
if customer == "TRITIUM" or customer == "VARLEY_TOMAGO_DEFENCE":
for j, item in enumerate(lines_ahead_array):
# tritium has their client part number in the part description field
# looking for "Part Description" then jumping one line ahead
if item.find("Part Description") >= 0:
# adding "CUSTOMER LABELS" if the next line contains "CUSTOMER"
if lines_ahead_array[j+1].split(" ")[0] == "CUSTOMER":
client_part_number_array.append("CUSTOMER-LABELS")
# ADDING "DESPATCH" if the next line contains "DESPATCH"
elif lines_ahead_array[j+1].split("\t")[0] == "DESPATCH":
client_part_number_array.append("DESPATCH")
# one line down from "Part Description" and splitting the string with the first white space
else:
client_part_number_array.append(lines_ahead_array[j+1].split(" ")[0])
###################################################
############ Creating the Qty Array #############
###################################################
qty_array = []
for i in ticket_line_number_array:
lines_ahead_array = []
for counter, line in enumerate(txt_file_lines, 1):
if counter < ( i + 15 ) and counter > ( i + 6 ):
lines_ahead_array.append(line)
for j, item in enumerate(lines_ahead_array):
if item.find("Order Qty") >= 0:
qty_array.append(lines_ahead_array[j+1].split("\t")[0])
########################################################
############ Creating the Revision Array #############
########################################################
revision_array = []
for i in ticket_line_number_array:
lines_ahead_array = []
for counter, line in enumerate(txt_file_lines, 1):
if counter < ( i + 14 ) and counter > ( i + 3 ):
lines_ahead_array.append(line)
for j, item in enumerate(lines_ahead_array):
if item.find("Revision") >= 0:
# added the 'try' because you will get an IndexError if there is no revision in iTMS
try:
# the revision will be one line down and the 5th tab over
revision_array.append(lines_ahead_array[j+1].split("\t")[4])
except IndexError:
# adding an empty string to the array if there is no revision
revision_array.append(" ")
#####################################################
############ Finding the Order Number #############
#####################################################
# starting to find the Order Number
for i in enumerate(ticket_line_number_array):
# only searching the first element in the ticket_line_number_array
if i < 1:
lines_ahead_array = []
for counter, line in enumerate(txt_file_lines, 1):
# only reading in lines 5 to 14 from the txt file into the array
if counter > 4 and counter < 15:
lines_ahead_array.append(line)
# searching each line for the word "Order No"
for j, item in enumerate(lines_ahead_array):
if item.find("Order No") >= 0:
# the Order Number is one line below, and the third tab over
order_no = lines_ahead_array[j+1].split("\t")[2]
print "Order Number =", order_no
print
#################################################################
##### Arrays and Variables for VARLEY TOMAGO DEFENCE only #####
#################################################################
if customer == "VARLEY_TOMAGO_DEFENCE":
# Getting the Kit Number from the USER, this changes with each order
print "* VARLEY - TOMAGO DEFENCE, require a kit number to be printed on each label."
print "* The kit number should be written on the 'CUSTOMER-LABELS' ticket."
print "* If there is no kit number on that ticket, see Jamie."
print
kit_number = raw_input("## Please enter the Kit Number for this job: ")
# Creating the Revision Array
revision_array = []
for i in ticket_line_number_array:
lines_ahead_array = []
for counter, line in enumerate(txt_file_lines, 1):
if counter < ( i + 14 ) and counter > ( i + 3 ):
lines_ahead_array.append(line)
for j, item in enumerate(lines_ahead_array):
if item.find("Revision") >= 0:
# added the 'try' because you will get an IndexError if there is no revision in iTMS
try:
# the revision will be one line down and the 5th tab over
revision_array.append(lines_ahead_array[j+1].split("\t")[4])
except IndexError:
# adding an empty string to the array if there is no revision
revision_array.append(" ")
###############################################################
############## Starting to print the labels ###############
###############################################################
if print_labels == True:
# Create an A4 portrait (210mm x 297mm) sheet with 3 columns and 11 rows of labels.
# Each label is 63.6mm x 24.1mm with a 2mm rounded corner. The margins are automatically calculated.
# left_margin is 7mm, top_margin is 14mm, column_gap is 2.7mm
specs = labels.Specification(210, 297, 3, 11, 63.6, 24.1, corner_radius=2,
left_padding=3, bottom_padding=1.5, left_margin=6.5, top_margin=14,
row_gap=0.5, column_gap=2.7)
# setting up labels for VARLEY_TOMAGO_DEFENCE
if customer == "VARLEY_TOMAGO_DEFENCE":
Part = namedtuple(
'Part',
['gci_group', 'customer', 'division_kit_number', 'part_number', 'rev_qty'])
def draw_part(label, width, height, part):
lines = [
part.rev_qty,
part.part_number,
part.division_kit_number,
part.customer,
part.gci_group
]
group = shapes.Group()
x, y = 0, 0
for line in lines:
if not line:
continue
shape = shapes.String(x, y, line, textAnchor="start", fontName="Helvetica", fontSize=6)
y += 11
group.add(shape)
label.add(group)
# setting up the label for VARLEY
if customer == "VARLEY":
Part = namedtuple(
'Part',
['gci_group', 'customer', 'order_number', 'part_number', 'rev_qty']
)
def draw_part(label, width, height, part):
lines = [
part.rev_qty,
part.part_number,
part.order_number,
part.customer,
part.gci_group
]
group = shapes.Group()
x, y = 0, 0
for line in lines:
if not line:
continue
shape = shapes.String(x, y, line, textAnchor="start", fontName="Helvetica", fontSize=6)
y += 11
group.add(shape)
label.add(group)
# setting up the label for TRITIUM and BELL ENVIRONMENTAL
if customer == "TRITIUM" or "BELL":
Part = namedtuple(
'Part',
['gci_group', 'customer', 'order_number', 'part_number', 'rev_qty']
)
def draw_part(label, width, height, part):
lines = [
part.rev_qty,
part.part_number,
part.order_number,
part.customer,
part.gci_group
]
group = shapes.Group()
x, y = 0, 0
for line in lines:
if not line:
continue
shape = shapes.String(x, y, line, textAnchor="start", fontName="Helvetica", fontSize=6)
y += 11
group.add(shape)
label.add(group)
sheet = labels.Sheet(specs, draw_part, border=False)
# starting a label counter to display how many labels the program creates
label_counter = 1
# creating the labels for VARLEY_TOMAGO_DEFENCE
if customer == "VARLEY_TOMAGO_DEFENCE":
for i, item in enumerate(ticket_line_number_array):
counter = 1
if client_part_number_array[i] != "CUSTOMER-LABELS":
while counter <= int(qty_array[i]):
# print "Ticket Number: " + str(job_number) + "-" + str(i + 1)
# print "Part Number: " + str(client_part_number_array[i])
# print "Revision: " + str(revision_array[i])
# print "Quantity: " + str(counter) + " of " + str(qty_array[i])
# print
part = Part("GCI GROUP" + (" "*55) + str(job_number) + "-" + str(i + 1),
"CUSTOMER: VARLEY - TOMAGO",
"DIVISION: DEFENCE & AERO" + (" " * 8) + "KIT NO. " + kit_number,
"PART NUMBER: " + str(client_part_number_array[i]),
"REV: " + str(revision_array[i]) + (" "*25) + "QTY: " + str(counter) + " of " + str(qty_array[i])
)
print "* generating label number: " + str(label_counter) + " *"
sheet.add_label(part)
counter += 1
label_counter +=1
# creating the labels for VARLEY
if customer == "VARLEY":
for i, item in enumerate(ticket_line_number_array):
part = Part(
"GCI GROUP" + (" "*55) + str(job_number) + "-" + str(i + 1),
"CUSTOMER: VARLEY",
"ORDER NUMBER: " + str(order_no),
"PART NUMBER: " + str(client_part_number_array[i]),
"REV: " + str(revision_array[i]) + (" "*25) + "QTY: " + str(qty_array[i])
)
sheet.add_label(part)
print "* generating label number: " + str(label_counter) + " *"
label_counter += 1
# creating the labels for TRITIUM
if customer == "TRITIUM":
for i, item in enumerate(ticket_line_number_array):
# it will not generate a label if the client part is "CUSTOMER-LABELS" or "DESPATCH"
if client_part_number_array[i] == "CUSTOMER-LABELS" or client_part_number_array[i] == "DESPATCH":
print client_part_number_array[i]
else:
part = Part(
"GCI GROUP" + (" "*55) + str(job_number) + "-" + str(i + 1),
"CUSTOMER: TRITIUM",
"ORDER NUMBER: " + str(order_no),
"PART NUMBER: " + str(client_part_number_array[i]),
"REV: " + str(revision_array[i]) + (" "*25) + "QTY: " + str(qty_array[i])
)
sheet.add_label(part)
print "* generating label number: " + str(label_counter) + " *"
label_counter += 1
# creating the labels for BELL ENVIRONMENTAL
if customer == "BELL":
for i, item in enumerate(ticket_line_number_array):
if client_part_number_array[i] != "CUSTOMER-LABELS":
part = Part(
"GCI GROUP" + (" "*55) + str(job_number) + "-" + str(i + 1),
"CUSTOMER: BELL ENVIRONMENTAL",
"ORDER NUMBER: " + str(order_no),
"PART NUMBER: " + str(client_part_number_array[i]),
"REV: " + str(revision_array[i]) + (" "*25) + "QTY: " + str(qty_array[i])
)
sheet.add_label(part)
print "* generating label number: " + str(label_counter) + " *"
label_counter += 1
print
print "Saving the pdf as " + str(job_number) + ".pdf"
print
pdf_name = job_number + '.pdf'
sheet.save(pdf_name)
# Moving the txt file to the 'Already-Processed' folder
shutil.move(input_file, './Already-Processed/')
#################################
########## Test Mode ##########
#################################
if test_mode == True:
print
print "**** Test Mode ****"
print "Looping through and testing all of the arrays."
print
for i, item in enumerate(ticket_line_number_array):
if client_part_number_array[i] != "CUSTOMER-LABELS":
print "Ticket Number:", job_number+"-"+str(i+1)
print "Part Number: ", client_part_number_array[i]
if customer == "VARLEY_TOMAGO_DEFENCE" or customer == "VARLEY":
print "Revision:", revision_array[i]
print "Quantity:", qty_array[i]
print
# print this to stdout only if the user chose to print labels
if print_labels == True:
print "Generating the labels is complete."
print "Open the PDF generated and print this onto the AVERY 'L7175REV' Labels"
print "Make sure you printer is printing at 'Actual Size' and not scaling the pdf :D"
print "If in doubt ask Nathan"
print