Jul-26-2021, 09:11 AM
Hello,
I am trying to make a Python program that takes user input and generates labels as a pdf based on what was put in. The proof of concept is done however I am struggling to debug the more intensive version.
Currently the program will not generate the correct amount of labels for one distinct item, and if there are multiple distinct items then the whole thing gets confused. I have a while loop in there currently as you can see and the alternative for loop which was being used is commented. I understand the thing looks messy currently however I must emphasise this is a prototype!
Lastly if someone can help me with the label formatting on the pdf that would be smashing. I need 4 labels to a page, such as:
|1 | 2|
|3 | 4|
whilst i'm currently getting
|1 |
|2 |
*new page*
|1 |
|2 |
and so on.
CODE****************************
I am trying to make a Python program that takes user input and generates labels as a pdf based on what was put in. The proof of concept is done however I am struggling to debug the more intensive version.
Currently the program will not generate the correct amount of labels for one distinct item, and if there are multiple distinct items then the whole thing gets confused. I have a while loop in there currently as you can see and the alternative for loop which was being used is commented. I understand the thing looks messy currently however I must emphasise this is a prototype!
Lastly if someone can help me with the label formatting on the pdf that would be smashing. I need 4 labels to a page, such as:
|1 | 2|
|3 | 4|
whilst i'm currently getting
|1 |
|2 |
*new page*
|1 |
|2 |
and so on.
CODE****************************
#Prepare the global variables and import the PDF maker module. from fpdf import FPDF #label list is the list storing the details of each distinct item. This way we have no upper limit to the number of distinct items we can process. label_list = [] order = "0" send_line1 = "0" send_line2 = "0" send_city = "0" send_postcode = "0" rec_line1 = "0" rec_line2 = "0" rec_city = "0" rec_postcode = "0" #Total labels is the total number of labels we need for each distinct item. The first value (0) would be the number of labels required for all of the first (0) distinct item, the second value (1) for the next distinct item and so on. total_labels = [] #x acts as a placemark later for knowing how many times we have asked the user for the distinct data. x = int(1) #y acts as a placemark to choose an item in the labels list. for example we are looking for items 2 and 3 (total labels per item and total number of items) so we can calculate the total number of labels for that distinct batch. y = int(2) #i will act as the number of distinct items later so we know how often to ask for unique data. i = int(0) ######################################################################## #Intro Message to ensure use of the correct company's program. def start(): print ("********************************************************") print (" Welcome to the Prototype Label Template Program V2.0!") print ("********************************************************") print ("") print ("") print ("") sending_address() ######################################################################## #Sending address information. def sending_address(): #Accessing variables outside of the function. global send_line1 global send_line2 global send_city global send_postcode #Here we are saving the address data which is the same for every item on this order. print ("Please input the sending address following the prompts below.") print ("Line 1: ") send_line1 = input() print ("Line 2: ") send_line2 = input() print ("City/Town: ") send_city = input() print ("Postcode: ") send_postcode = input() #Here we are checking if the information is accurate. print (send_line1 + ", " + send_line2 + ", " + send_city + ", " + send_postcode) print (" ") print ("Is this correct? Y/N: ") a = input() #Possibly add an if error statement so the user doesn't accidentally continue. if a == ("N") or a ==("n"): sending_address() if a == ("No") or a == ("no"): sending_address() recieving_address() ######################################################################## #Recieving address information def recieving_address(): global rec_line1 global rec_line2 global rec_city global rec_postcode print ("Please input the recieving address following the prompts below.") print ("Line 1: ") rec_line1 = input() print ("Line 2: ") rec_line2 = input() print ("City/Town: ") rec_city = input() print ("Postcode: ") rec_postcode = input() #See above for relevant comments. print (rec_line1 + ", " + rec_line2 + ", " + rec_city + ", " + rec_postcode) print (" ") print ("Is this correct? Y/N: ") b = input () if b == ("N") or b == ("n"): recieving_address() if b == ("No") or b == ("no"): recieving_address() order_input() ######################################################################## #Order details such as the unique order number and the number of distinct items. def order_input(): #Here we store the order number which will be shown on each label. print ("Please input the order number for this order: ") global order order = input() #This part is important. It saves the number of distinct items on the order so the program knows how many times it needs to take in data. print ("Please input the number of distinct items on the PO: ") global i i = int(input()) item_input() ######################################################################## #This will run for every distinct item on the list. It asks for the item details and the quantity. def item_input(): global x global i global y #The first two questions are for information which will show on the label. print ("Please input the style number of this item: ") label_list.append(input()) print("Please input the colour of this item: ") label_list.append(input()) #Here it will calculate how many labels are required based off of per-unit * number_of_units print("Please input the number of labels required for each unit of this item: ") label_list.append(int(input())) print("Please input the quantity of this item: ") label_list.append(int(input())) total_labels.append(label_list[y] * label_list[y + 1]) print (label_list) print (total_labels) #Here we are checking if the above questions have been asked for each distinct item. #Maybe change the below to a for loop. if x < i : x = x + 1 y = y + 4 item_input() label_maker() ######################################################################## #This is the FPDF code which will make the PDF file. def label_maker(): global total_labels global label_list global order global i #In theory, threeandfour should act as a reference point to items three and four on the labels_list list which are required in the label. threeandfour = int(2) #Labels complete is a reference to know how many times it has run through this code. labels_complete = int(0) style_colour = int(0) #z refers to an item on the list. if there are multiple different products, the code will check the first item as z is 0 and once the labels are completed for the first item, # it will increment and create all the labels for the next item and so on. z = int(0) #This saves the FPDF functions. pdf = FPDF() #This creates a new page. I will need to use this after every four labels. pdf.add_page() #This code just chooses the font type and size. pdf.set_font("Arial", size = 14) #for loop in range(total_labels[z]): while total_labels[z] != labels_complete: #The codeblock below creates one label based on the information provided. pdf.cell(100, 10, txt = send_line1, ln = 1, align = '') pdf.cell(100, 10, txt = send_line2, ln = 2, align = '') pdf.cell(100, 10, txt = send_city + ", " + send_postcode, ln = 3, align = '') pdf.cell(100, 10, txt = " ", ln = 4, align = '') pdf.cell(100, 10, txt = " ", ln = 5, align = '') pdf.cell(100, 10, txt = "PO#: " + order, ln = 6, align = '') pdf.cell(100, 10, txt = " ", ln = 7, align = '') pdf.cell(100, 10, txt = str(label_list[style_colour]) + " " + str(label_list[style_colour + 1]), #Note: Check here later as list item number (0 and 1) will need changing to variable. ln = 8, align = '') pdf.cell(100, 10, txt = " ", ln = 9, align = '') pdf.cell(100, 10, txt = " ", ln = 10, align = '') pdf.cell(100, 10, txt = rec_line1, ln = 11, align = '') pdf.cell(100, 10, txt = rec_line2, ln = 12, align = '') pdf.cell(100, 10, txt = rec_city + ", " + rec_postcode, ln = 13, align = '') pdf.cell(100,10, txt = " ", ln = 14, align = '') labels_complete = labels_complete + 1 #This code checks if the number of completed labels matches the number of labels required for one distinct item. If not, it creates another label as seen below. if labels_complete == total_labels[z]: if len(total_labels) > (z + 1): style_colour = style_colour + 4 z = z + 1 labels_complete = 0 pdf.add_page() if total_labels[z] > labels_complete: pdf.cell(100, 10, txt = send_line1, ln = 1, align = '') pdf.cell(100, 10, txt = send_line2, ln = 2, align = '') pdf.cell(100, 10, txt = send_city + ", " + send_postcode, ln = 3, align = '') pdf.cell(100, 10, txt = " ", ln = 4, align = '') pdf.cell(100, 10, txt = " ", ln = 5, align = '') pdf.cell(100, 10, txt = "PO#: " + order, ln = 6, align = '') pdf.cell(100, 10, txt = " ", ln = 7, align = '') pdf.cell(100, 10, txt = str(label_list[style_colour]) + " " + str(label_list[style_colour + 1]), ln = 8, align = '') pdf.cell(100, 10, txt = " ", ln = 9, align = '') pdf.cell(100, 10, txt = " ", ln = 10, align = '') pdf.cell(100, 10, txt = rec_line1, ln = 11, align = '') pdf.cell(100, 10, txt = rec_line2, ln = 12, align = '') pdf.cell(100, 10, txt = rec_city + ", " + rec_postcode, ln = 13, align = '') labels_complete = labels_complete + 1 #this does the same as the above however if there are more distinct items it needs to start making labels for the next item. if labels_complete == total_labels[z]: if len(total_labels) < (z + 1): style_colour = style_colour + 4 z = z + 1 labels_complete = 0 pdf.add_page() # Note: Check this as it will not currently work. Needs to - If more labels required, go back to the start. #if there are no more items in the list it needs to check the length of the list and then say if list length is greater than the item on the list which has just been completed #(3 items on a list, item 1 complete) then re run the code. #if number of distinct items completed is equal to total distinct items, save the labels as a pdf with the order number as the name. if z == i: break pdf.output(order + ".pdf") ######################################################################## #Callback to the start function. start()