Bottom Page

Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Python ReportLab
#1
I need to generate pdf report Avery 5160 format. I am having trouble displaying report on multiple pages. 
[/font][/color]
[color=#222222][font=Verdana, arial, sans-serif]"""
[/font][/color]
    Created by Egor Kozitski

"""

from reportlab.pdfgen import canvas

from reportlab.lib.pagesizes import LETTER

from reportlab.lib.units import inch,mm

from reportlab.graphics.barcode import code128

import json,sys,operator,itertools

"""

    @class LabelGenerator will generate Lbels using reportlab

    @param filelocation is the location of the file where file will be saved

"""

class LabelGenerator():



    def __init__(self,fileLocation):

        self.fileLocation = fileLocation

        self.canv = canvas.Canvas(self.fileLocation,pagesize=LETTER)

        self.canv.setPageCompression(0)

        #LABELW is the width of the label

        self.LABELW = 2.6 * inch

        #LABELSEP  is the label separation

        self.LABELSEP = 2.75 * inch

        #LABELH is the height of the label

        self.LABELH = 1 * inch

    #helper method to split list into chunks of 30 elements

    def grouper(self,n, iterable):

        #create iterable object out of iterable

        it = iter(iterable)

        #iterate infinitely and create chunks

        while True:

            #slice iterable by n ammount of elements

            chunk = list(itertools.islice(it, n))

            if not chunk:

                return

            #yield back the result

            yield chunk



    def label_position(self,ordinal):

        """

            @method label_position will find the coordinates for the next label

            @param ordinal is the label number for which to find postion

        """

        y,x = divmod(ordinal , 3)

        x = 14 + x * self.LABELSEP

        y = 756 - y * self.LABELH

        #return the coordinates

        return x , y



    def label_generator(self,json_list):

        """

        @method label_generator will generate labels for n ammount of items

        """

        # self.generate_label(json_list)

        if len(json_list) < 30:

            self.generate_label(json_list)

        else:



            for label in list(self.grouper(30,json_list)):

                self.generate_label(label)

                self.canv.showPage()

            self.canv.save()









    def generate_label(self,json_list):

        """

            @method generate_label will iterate over list of labels and generate labels

        """



        #iterate over list of labels

        for i in range(1,len(json_list)+1):

            #iterate over the label to extract Order and make sure that its matches index

            for item in json_list:

                #if we find the match than generate label

                if item['Order'] == str(i):

                    #set barcode width

                    barcode_setBarWidth = 1

                    #set coordinates for label

                    x,y = self.label_position(i-1)

                    #while true iterate infinitely and generate the right baarcode

                    while True:

                        barcode = code128.Code128(item['UPC'],barHeight=6*mm,\

                                                              barWidth=barcode_setBarWidth)

                        if barcode.width >= 300:

                            barcode_setBarWidth = barcode_setBarWidth - 0.01

                        else:

                            break

                    #get image from the item and scale it to the size of the label

                    #postion it to the right position

                    self.canv.drawImage("image.png",x,y,width=self.LABELW,height=-self.LABELH+10)

                    #create text object which will hold UPC number

                    #position text in the label

                    text = self.canv.beginText(x+22,y-55)

                    #draw barcode on the label

                    barcode.drawOn(self.canv,x-15,y-48)

                    #set text style and fond

                    text.setFont('Helvetica',7,7)

                    #set the text to be drawn

                    text.textLines(item['UPC'])

                    #draw text on the canvas

                    self.canv.drawText(text)

        

        #save it into pdf





        # self.canv.save()









if __name__ =='__main__':

    json_list = """[{"TemplateID":"Microsoft 4YM-0016","UPC":"854024427546","Order":"1"},{"TemplateID":"Microsoft 4YM-0016","UPC":"896070896551","Order":"2"},{"TemplateID":"Microsoft 4YM-0016","UPC":"492535039795","Order":"3"},{"TemplateID":"Microsoft 4YM-0016","UPC":"980144179532","Order":"4"},{"TemplateID":"Microsoft 4YM-0016","UPC":"953237474752","Order":"5"},{"TemplateID":"Microsoft 4YM-0016","UPC":"800403475381","Order":"6"},{"TemplateID":"Microsoft 4YM-0016","UPC":"459867062992","Order":"7"},{"TemplateID":"Microsoft 4YM-0016","UPC":"314882354925","Order":"8"},{"TemplateID":"Microsoft 4YM-0016","UPC":"421298698843","Order":"9"},{"TemplateID":"Microsoft 4YM-0016","UPC":"146153461080","Order":"10"},{"TemplateID":"Microsoft 4YM-0016","UPC":"854805864303","Order":"11"},{"TemplateID":"Microsoft 4YM-0016","UPC":"678579770722","Order":"12"},{"TemplateID":"Microsoft 4YM-0016","UPC":"168480935247","Order":"13"},{"TemplateID":"Microsoft 4YM-0016","UPC":"668791157220","Order":"14"},{"TemplateID":"Microsoft 4YM-0016","UPC":"740939140828","Order":"15"},{"TemplateID":"Microsoft 4YM-0016","UPC":"001570965556","Order":"16"},{"TemplateID":"Microsoft 4YM-0016","UPC":"324975221753","Order":"17"},{"TemplateID":"Microsoft 4YM-0016","UPC":"120476838669","Order":"18"},{"TemplateID":"Microsoft 4YM-0016","UPC":"598077900477","Order":"19"},{"TemplateID":"Microsoft 4YM-0016","UPC":"250843881374","Order":"20"},{"TemplateID":"Microsoft 4YM-0016","UPC":"811257091846","Order":"21"},{"TemplateID":"Microsoft 4YM-0016","UPC":"915409953650","Order":"22"},{"TemplateID":"Microsoft 4YM-0016","UPC":"881926369040","Order":"23"},{"TemplateID":"Microsoft 4YM-0016","UPC":"062532812772","Order":"24"},{"TemplateID":"Microsoft 4YM-0016","UPC":"073714100107","Order":"25"},{"TemplateID":"Microsoft 4YM-0016","UPC":"212960117066","Order":"26"},{"TemplateID":"Microsoft 4YM-0016","UPC":"506050029642","Order":"27"},{"TemplateID":"Microsoft 4YM-0016","UPC":"016286646554","Order":"28"},{"TemplateID":"Microsoft 4YM-0016","UPC":"294969321128","Order":"29"},{"TemplateID":"Microsoft 4YM-0016","UPC":"929011126614","Order":"30"},{"TemplateID":"Microsoft 4YM-0016","UPC":"529476319910","Order":"31"},{"TemplateID":"Microsoft 4YM-0016","UPC":"727535059435","Order":"32"},{"TemplateID":"Microsoft 4YM-0016","UPC":"986364488579","Order":"33"},{"TemplateID":"Microsoft 4YM-0016","UPC":"395522038237","Order":"34"},{"TemplateID":"Microsoft 4YM-0016","UPC":"229477467590","Order":"35"},{"TemplateID":"Microsoft 4YM-0016","UPC":"918033470486","Order":"36"},{"TemplateID":"Microsoft 4YM-0016","UPC":"800393354664","Order":"37"},{"TemplateID":"Microsoft 4YM-0016","UPC":"088054124430","Order":"38"},{"TemplateID":"Microsoft 4YM-0016","UPC":"324938407073","Order":"39"},{"TemplateID":"Microsoft 4YM-0016","UPC":"830941102460","Order":"40"},{"TemplateID":"Microsoft 4YM-0016","UPC":"941487562347","Order":"41"},{"TemplateID":"Microsoft 4YM-0016","UPC":"801340683962","Order":"42"},{"TemplateID":"Microsoft 4YM-0016","UPC":"189323958875","Order":"43"},{"TemplateID":"Microsoft 4YM-0016","UPC":"746227509638","Order":"44"},{"TemplateID":"Microsoft 4YM-0016","UPC":"055060376660","Order":"45"},{"TemplateID":"Microsoft 4YM-0016","UPC":"446781569032","Order":"46"},{"TemplateID":"Microsoft 4YM-0016","UPC":"423709974662","Order":"47"},{"TemplateID":"Microsoft 4YM-0016","UPC":"587053879378","Order":"48"},{"TemplateID":"Microsoft 4YM-0016","UPC":"747103897209","Order":"49"},{"TemplateID":"Microsoft 4YM-0016","UPC":"258132557428","Order":"50"},{"TemplateID":"Microsoft 4YM-0016","UPC":"488228790214","Order":"51"},{"TemplateID":"Microsoft 4YM-0016","UPC":"425761168401","Order":"52"},{"TemplateID":"Microsoft 4YM-0016","UPC":"827175725340","Order":"53"},{"TemplateID":"Microsoft 4YM-0016","UPC":"560804923240","Order":"54"},{"TemplateID":"Microsoft 4YM-0016","UPC":"161877874043","Order":"55"},{"TemplateID":"Microsoft 4YM-0016","UPC":"886087107202","Order":"56"},{"TemplateID":"Microsoft 4YM-0016","UPC":"211523352048","Order":"57"},{"TemplateID":"Microsoft 4YM-0016","UPC":"818659045277","Order":"58"},{"TemplateID":"Microsoft 4YM-0016","UPC":"960161455387","Order":"59"},{"TemplateID":"Microsoft 4YM-0016","UPC":"802917018499","Order":"60"},{"TemplateID":"Microsoft 4YM-0016","UPC":"656937238692","Order":"61"},{"TemplateID":"Microsoft 4YM-0016","UPC":"694357455377","Order":"62"},{"TemplateID":"Microsoft 4YM-0016","UPC":"735951196240","Order":"63"},{"TemplateID":"Microsoft 4YM-0016","UPC":"678074643581","Order":"64"},{"TemplateID":"Microsoft 4YM-0016","UPC":"551659479192","Order":"65"},{"TemplateID":"Microsoft 4YM-0016","UPC":"527330347299","Order":"66"},{"TemplateID":"Microsoft 4YM-0016","UPC":"637794765129","Order":"67"},{"TemplateID":"Microsoft 4YM-0016","UPC":"806857974484","Order":"68"},{"TemplateID":"Microsoft 4YM-0016","UPC":"303689331607","Order":"69"}]"""

    #create object

    label = LabelGenerator("OttoVonBismark")

    #generate pdf file
[color=#222222][font=Verdana, arial, sans-serif]    label.label_generator(json.loads(json_list))

[/font][/color]
[color=#222222][font=Verdana, arial, sans-serif]

I am using json_list as a sample.
I think I am doing something wrong in the label_generator. The output is just one page with 30 labels
Quote
#2
can you insert a PageBreak?
Quote
#3
I found the problem and now I am trying to fix it. My problem is when I am trying to sort by Order which is line 148 and down
Quote
#4
This is not what's causing the problem, but:
        for i in range(1,len(json_list)+1):
 
            #iterate over the label to extract Order and make sure that its matches index
 
            for item in json_list:
can be replaced with:
        for i, item in enumerate(json_list):
You can sort the list with:
json_lsit = json_list.sort()
Quote
#5
Hmm , than I do not understand what do you mean by PageBreak
Quote
#6
Quote:Hmm , than I do not understand what do you mean by PageBreak

see page 92 of doc.

Sorry you can't sort it that way, I thought it was a regular list (even though named json_list)
Quote
#7
I rewrote it with this
sorted(json.loads(json_list),key=lambda d:("Order" not in d, int(d.get("Order"))))
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  cyrillic symbols in tables in reportlab. hiroz 4 938 Mar-20-2019, 07:42 PM
Last Post: nilamo
  Python Reportlab Wordwrap Table Mady 0 1,219 Dec-18-2018, 06:31 AM
Last Post: Mady
  Reportlab Dynamic Table Q Gutt 0 1,573 Jun-13-2018, 10:18 PM
Last Post: Gutt
  Reportlab: Add xlabel, ylabel and grid to lineplot denissanga 2 2,010 Dec-19-2017, 04:48 PM
Last Post: denissanga
  ReportLab ek5442 2 1,523 Aug-24-2017, 01:29 PM
Last Post: ek5442

Forum Jump:


Users browsing this thread: 1 Guest(s)