How can I position a table at a particular x and y coordinate via reportlab












1















I have the following code which produces a pdf:



def colr(x, y, z):
return (x/255, y/255, z/255)
import reportlab
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.utils import ImageReader
from reportlab.platypus import SimpleDocTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph
from reportlab.platypus.tables import Table
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
width, height = A4
logo = '/home/joel/myappointments/appointments/static/clinic/img/logo/logo.png'
elements =
print(f'Height={height}')
imgw = imgh = 100
im = Image(logo, width=imgw, height=imgh)
im.hAlign = 'LEFT'
elements.append(im)

headstyle = ParagraphStyle(
name='MyHeader',
fontName='Helvetica-Bold',
fontSize=16,
leading =10
)
doctorstyle = ParagraphStyle(
name='MyDoctorHeader',
fontName='Helvetica',
fontSize=13,
leading =10
)
data = [[Paragraph("Dr John Doe's ENT Clinic", style = headstyle)], [Paragraph("Dr John Doe", style = doctorstyle)], [Paragraph("ENT Specialist", style = doctorstyle)], [Paragraph("Registration No. ", style = doctorstyle)]]
elements.append(Table(data, repeatRows=1))
line1 = ("Name", "Test", "Age", "20yr")
line2 = ("MRD No.", "18","Date", "14-11-2018")
line3 = ("No.","#", "Doctor", "Dr.John Doe")
data=[line1,line2, line3]
patientdetailstable = Table(data)
patientdetailstable.setStyle(TableStyle([
('BACKGROUND', (0, 0), (4, 0), '#CFEAD4'),
('BACKGROUND', (0, 2), (4, 2), '#CFEAD4'),
('BOX',(0,0),(-1,-1), 0.5, '#CFEAD4'),
('GRID',(0,0),(-1,-1), 0.5, colr(12, 43, 8)),
]))
elements.append(patientdetailstable)
elements.append(Spacer(1, 20))
# We use paragraph style because we need to wrap text. We cant directly wrap cells otherwise
line1 = ["Sl.", "Medicine" , "Dose", "Freq", "Durn", "Note"]
drug1 = Paragraph('AUGMED Syrup 30ml (AMOXICILLIN 200MG + CLAVULANATE(CLAVULANIC ACID) 28.5MG)', styleN)
line2 = ["1", drug1, "1 Tab", "1-0-1", "5 days", ""]
line3 = ["2", drug1, "1 Tab", "1-0-1", "5 days", ""]
data=[line1,line2, line3]
for i in range(3,50):
temp = [str(i), "Some Drug here", "5 ml", "1-0-1", "10 days", "No comments"]
data.append(temp)

medstable = Table(data, repeatRows=1)
medstable.setStyle(TableStyle([
('VALIGN',(0,0),(-1,-1), 'TOP'),
('TEXTCOLOR',(0,0),(-1,0),colors.white),
('BACKGROUND', (0, 0), (-1, 0), colr(40, 196, 15)),
('GRID',(0,1),(-1,-1), 0.5, '#CFEAD4'),
]))
elements.append(medstable)
doc = SimpleDocTemplate('output.pdf', pagesize=A4, rightMargin=20, leftMargin=20,
topMargin=20, bottomMargin=20, allowSplitting=1,
title="Prescription", author="MyOPIP.com")
doc.build(elements)


It's all fine, except for the fact that I need the section with: John Doe's ENT clinic upto Registration No to the right of the logo, instead of underneath it. If I were working on Canvas directly instead of a flowable SimpleDocTemplate, I could easily set its position.



Using a SimpleDocTemplate, how can I position the section arbitrarily?



Screenshot










share|improve this question





























    1















    I have the following code which produces a pdf:



    def colr(x, y, z):
    return (x/255, y/255, z/255)
    import reportlab
    from reportlab.lib.pagesizes import A4
    from reportlab.pdfgen.canvas import Canvas
    from reportlab.lib.utils import ImageReader
    from reportlab.platypus import SimpleDocTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph
    from reportlab.platypus.tables import Table
    from reportlab.lib import colors
    from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
    from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
    styles = getSampleStyleSheet()
    styleN = styles["BodyText"]
    styleN.alignment = TA_LEFT
    width, height = A4
    logo = '/home/joel/myappointments/appointments/static/clinic/img/logo/logo.png'
    elements =
    print(f'Height={height}')
    imgw = imgh = 100
    im = Image(logo, width=imgw, height=imgh)
    im.hAlign = 'LEFT'
    elements.append(im)

    headstyle = ParagraphStyle(
    name='MyHeader',
    fontName='Helvetica-Bold',
    fontSize=16,
    leading =10
    )
    doctorstyle = ParagraphStyle(
    name='MyDoctorHeader',
    fontName='Helvetica',
    fontSize=13,
    leading =10
    )
    data = [[Paragraph("Dr John Doe's ENT Clinic", style = headstyle)], [Paragraph("Dr John Doe", style = doctorstyle)], [Paragraph("ENT Specialist", style = doctorstyle)], [Paragraph("Registration No. ", style = doctorstyle)]]
    elements.append(Table(data, repeatRows=1))
    line1 = ("Name", "Test", "Age", "20yr")
    line2 = ("MRD No.", "18","Date", "14-11-2018")
    line3 = ("No.","#", "Doctor", "Dr.John Doe")
    data=[line1,line2, line3]
    patientdetailstable = Table(data)
    patientdetailstable.setStyle(TableStyle([
    ('BACKGROUND', (0, 0), (4, 0), '#CFEAD4'),
    ('BACKGROUND', (0, 2), (4, 2), '#CFEAD4'),
    ('BOX',(0,0),(-1,-1), 0.5, '#CFEAD4'),
    ('GRID',(0,0),(-1,-1), 0.5, colr(12, 43, 8)),
    ]))
    elements.append(patientdetailstable)
    elements.append(Spacer(1, 20))
    # We use paragraph style because we need to wrap text. We cant directly wrap cells otherwise
    line1 = ["Sl.", "Medicine" , "Dose", "Freq", "Durn", "Note"]
    drug1 = Paragraph('AUGMED Syrup 30ml (AMOXICILLIN 200MG + CLAVULANATE(CLAVULANIC ACID) 28.5MG)', styleN)
    line2 = ["1", drug1, "1 Tab", "1-0-1", "5 days", ""]
    line3 = ["2", drug1, "1 Tab", "1-0-1", "5 days", ""]
    data=[line1,line2, line3]
    for i in range(3,50):
    temp = [str(i), "Some Drug here", "5 ml", "1-0-1", "10 days", "No comments"]
    data.append(temp)

    medstable = Table(data, repeatRows=1)
    medstable.setStyle(TableStyle([
    ('VALIGN',(0,0),(-1,-1), 'TOP'),
    ('TEXTCOLOR',(0,0),(-1,0),colors.white),
    ('BACKGROUND', (0, 0), (-1, 0), colr(40, 196, 15)),
    ('GRID',(0,1),(-1,-1), 0.5, '#CFEAD4'),
    ]))
    elements.append(medstable)
    doc = SimpleDocTemplate('output.pdf', pagesize=A4, rightMargin=20, leftMargin=20,
    topMargin=20, bottomMargin=20, allowSplitting=1,
    title="Prescription", author="MyOPIP.com")
    doc.build(elements)


    It's all fine, except for the fact that I need the section with: John Doe's ENT clinic upto Registration No to the right of the logo, instead of underneath it. If I were working on Canvas directly instead of a flowable SimpleDocTemplate, I could easily set its position.



    Using a SimpleDocTemplate, how can I position the section arbitrarily?



    Screenshot










    share|improve this question



























      1












      1








      1








      I have the following code which produces a pdf:



      def colr(x, y, z):
      return (x/255, y/255, z/255)
      import reportlab
      from reportlab.lib.pagesizes import A4
      from reportlab.pdfgen.canvas import Canvas
      from reportlab.lib.utils import ImageReader
      from reportlab.platypus import SimpleDocTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph
      from reportlab.platypus.tables import Table
      from reportlab.lib import colors
      from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
      from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
      styles = getSampleStyleSheet()
      styleN = styles["BodyText"]
      styleN.alignment = TA_LEFT
      width, height = A4
      logo = '/home/joel/myappointments/appointments/static/clinic/img/logo/logo.png'
      elements =
      print(f'Height={height}')
      imgw = imgh = 100
      im = Image(logo, width=imgw, height=imgh)
      im.hAlign = 'LEFT'
      elements.append(im)

      headstyle = ParagraphStyle(
      name='MyHeader',
      fontName='Helvetica-Bold',
      fontSize=16,
      leading =10
      )
      doctorstyle = ParagraphStyle(
      name='MyDoctorHeader',
      fontName='Helvetica',
      fontSize=13,
      leading =10
      )
      data = [[Paragraph("Dr John Doe's ENT Clinic", style = headstyle)], [Paragraph("Dr John Doe", style = doctorstyle)], [Paragraph("ENT Specialist", style = doctorstyle)], [Paragraph("Registration No. ", style = doctorstyle)]]
      elements.append(Table(data, repeatRows=1))
      line1 = ("Name", "Test", "Age", "20yr")
      line2 = ("MRD No.", "18","Date", "14-11-2018")
      line3 = ("No.","#", "Doctor", "Dr.John Doe")
      data=[line1,line2, line3]
      patientdetailstable = Table(data)
      patientdetailstable.setStyle(TableStyle([
      ('BACKGROUND', (0, 0), (4, 0), '#CFEAD4'),
      ('BACKGROUND', (0, 2), (4, 2), '#CFEAD4'),
      ('BOX',(0,0),(-1,-1), 0.5, '#CFEAD4'),
      ('GRID',(0,0),(-1,-1), 0.5, colr(12, 43, 8)),
      ]))
      elements.append(patientdetailstable)
      elements.append(Spacer(1, 20))
      # We use paragraph style because we need to wrap text. We cant directly wrap cells otherwise
      line1 = ["Sl.", "Medicine" , "Dose", "Freq", "Durn", "Note"]
      drug1 = Paragraph('AUGMED Syrup 30ml (AMOXICILLIN 200MG + CLAVULANATE(CLAVULANIC ACID) 28.5MG)', styleN)
      line2 = ["1", drug1, "1 Tab", "1-0-1", "5 days", ""]
      line3 = ["2", drug1, "1 Tab", "1-0-1", "5 days", ""]
      data=[line1,line2, line3]
      for i in range(3,50):
      temp = [str(i), "Some Drug here", "5 ml", "1-0-1", "10 days", "No comments"]
      data.append(temp)

      medstable = Table(data, repeatRows=1)
      medstable.setStyle(TableStyle([
      ('VALIGN',(0,0),(-1,-1), 'TOP'),
      ('TEXTCOLOR',(0,0),(-1,0),colors.white),
      ('BACKGROUND', (0, 0), (-1, 0), colr(40, 196, 15)),
      ('GRID',(0,1),(-1,-1), 0.5, '#CFEAD4'),
      ]))
      elements.append(medstable)
      doc = SimpleDocTemplate('output.pdf', pagesize=A4, rightMargin=20, leftMargin=20,
      topMargin=20, bottomMargin=20, allowSplitting=1,
      title="Prescription", author="MyOPIP.com")
      doc.build(elements)


      It's all fine, except for the fact that I need the section with: John Doe's ENT clinic upto Registration No to the right of the logo, instead of underneath it. If I were working on Canvas directly instead of a flowable SimpleDocTemplate, I could easily set its position.



      Using a SimpleDocTemplate, how can I position the section arbitrarily?



      Screenshot










      share|improve this question
















      I have the following code which produces a pdf:



      def colr(x, y, z):
      return (x/255, y/255, z/255)
      import reportlab
      from reportlab.lib.pagesizes import A4
      from reportlab.pdfgen.canvas import Canvas
      from reportlab.lib.utils import ImageReader
      from reportlab.platypus import SimpleDocTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph
      from reportlab.platypus.tables import Table
      from reportlab.lib import colors
      from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
      from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
      styles = getSampleStyleSheet()
      styleN = styles["BodyText"]
      styleN.alignment = TA_LEFT
      width, height = A4
      logo = '/home/joel/myappointments/appointments/static/clinic/img/logo/logo.png'
      elements =
      print(f'Height={height}')
      imgw = imgh = 100
      im = Image(logo, width=imgw, height=imgh)
      im.hAlign = 'LEFT'
      elements.append(im)

      headstyle = ParagraphStyle(
      name='MyHeader',
      fontName='Helvetica-Bold',
      fontSize=16,
      leading =10
      )
      doctorstyle = ParagraphStyle(
      name='MyDoctorHeader',
      fontName='Helvetica',
      fontSize=13,
      leading =10
      )
      data = [[Paragraph("Dr John Doe's ENT Clinic", style = headstyle)], [Paragraph("Dr John Doe", style = doctorstyle)], [Paragraph("ENT Specialist", style = doctorstyle)], [Paragraph("Registration No. ", style = doctorstyle)]]
      elements.append(Table(data, repeatRows=1))
      line1 = ("Name", "Test", "Age", "20yr")
      line2 = ("MRD No.", "18","Date", "14-11-2018")
      line3 = ("No.","#", "Doctor", "Dr.John Doe")
      data=[line1,line2, line3]
      patientdetailstable = Table(data)
      patientdetailstable.setStyle(TableStyle([
      ('BACKGROUND', (0, 0), (4, 0), '#CFEAD4'),
      ('BACKGROUND', (0, 2), (4, 2), '#CFEAD4'),
      ('BOX',(0,0),(-1,-1), 0.5, '#CFEAD4'),
      ('GRID',(0,0),(-1,-1), 0.5, colr(12, 43, 8)),
      ]))
      elements.append(patientdetailstable)
      elements.append(Spacer(1, 20))
      # We use paragraph style because we need to wrap text. We cant directly wrap cells otherwise
      line1 = ["Sl.", "Medicine" , "Dose", "Freq", "Durn", "Note"]
      drug1 = Paragraph('AUGMED Syrup 30ml (AMOXICILLIN 200MG + CLAVULANATE(CLAVULANIC ACID) 28.5MG)', styleN)
      line2 = ["1", drug1, "1 Tab", "1-0-1", "5 days", ""]
      line3 = ["2", drug1, "1 Tab", "1-0-1", "5 days", ""]
      data=[line1,line2, line3]
      for i in range(3,50):
      temp = [str(i), "Some Drug here", "5 ml", "1-0-1", "10 days", "No comments"]
      data.append(temp)

      medstable = Table(data, repeatRows=1)
      medstable.setStyle(TableStyle([
      ('VALIGN',(0,0),(-1,-1), 'TOP'),
      ('TEXTCOLOR',(0,0),(-1,0),colors.white),
      ('BACKGROUND', (0, 0), (-1, 0), colr(40, 196, 15)),
      ('GRID',(0,1),(-1,-1), 0.5, '#CFEAD4'),
      ]))
      elements.append(medstable)
      doc = SimpleDocTemplate('output.pdf', pagesize=A4, rightMargin=20, leftMargin=20,
      topMargin=20, bottomMargin=20, allowSplitting=1,
      title="Prescription", author="MyOPIP.com")
      doc.build(elements)


      It's all fine, except for the fact that I need the section with: John Doe's ENT clinic upto Registration No to the right of the logo, instead of underneath it. If I were working on Canvas directly instead of a flowable SimpleDocTemplate, I could easily set its position.



      Using a SimpleDocTemplate, how can I position the section arbitrarily?



      Screenshot







      python django python-3.x reportlab platypus






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 19:42







      Joel G Mathew

















      asked Nov 15 '18 at 18:16









      Joel G MathewJoel G Mathew

      2,14792846




      2,14792846
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I found one solution, which may not be the most elegant of solutions. After fiddling with frames and other flowables, I reread the documentation and found that I could just encapsulate my table within another table just for formatting. Hence I created a one row, two column table and inserted the image in one column and the other table in the second.



          col1 = Table([[im]])
          col2 = Table(data, repeatRows=1)
          tblrow1 = Table([[col1, col2]], colWidths=None)
          tblrow1.setStyle(
          TableStyle([
          ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
          ('VALIGN', (0, 0), (-1, -1), 'TOP'),
          ]))
          elements.append(tblrow1)





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325607%2fhow-can-i-position-a-table-at-a-particular-x-and-y-coordinate-via-reportlab%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I found one solution, which may not be the most elegant of solutions. After fiddling with frames and other flowables, I reread the documentation and found that I could just encapsulate my table within another table just for formatting. Hence I created a one row, two column table and inserted the image in one column and the other table in the second.



            col1 = Table([[im]])
            col2 = Table(data, repeatRows=1)
            tblrow1 = Table([[col1, col2]], colWidths=None)
            tblrow1.setStyle(
            TableStyle([
            ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
            ('VALIGN', (0, 0), (-1, -1), 'TOP'),
            ]))
            elements.append(tblrow1)





            share|improve this answer




























              0














              I found one solution, which may not be the most elegant of solutions. After fiddling with frames and other flowables, I reread the documentation and found that I could just encapsulate my table within another table just for formatting. Hence I created a one row, two column table and inserted the image in one column and the other table in the second.



              col1 = Table([[im]])
              col2 = Table(data, repeatRows=1)
              tblrow1 = Table([[col1, col2]], colWidths=None)
              tblrow1.setStyle(
              TableStyle([
              ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
              ('VALIGN', (0, 0), (-1, -1), 'TOP'),
              ]))
              elements.append(tblrow1)





              share|improve this answer


























                0












                0








                0







                I found one solution, which may not be the most elegant of solutions. After fiddling with frames and other flowables, I reread the documentation and found that I could just encapsulate my table within another table just for formatting. Hence I created a one row, two column table and inserted the image in one column and the other table in the second.



                col1 = Table([[im]])
                col2 = Table(data, repeatRows=1)
                tblrow1 = Table([[col1, col2]], colWidths=None)
                tblrow1.setStyle(
                TableStyle([
                ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
                ('VALIGN', (0, 0), (-1, -1), 'TOP'),
                ]))
                elements.append(tblrow1)





                share|improve this answer













                I found one solution, which may not be the most elegant of solutions. After fiddling with frames and other flowables, I reread the documentation and found that I could just encapsulate my table within another table just for formatting. Hence I created a one row, two column table and inserted the image in one column and the other table in the second.



                col1 = Table([[im]])
                col2 = Table(data, repeatRows=1)
                tblrow1 = Table([[col1, col2]], colWidths=None)
                tblrow1.setStyle(
                TableStyle([
                ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
                ('VALIGN', (0, 0), (-1, -1), 'TOP'),
                ]))
                elements.append(tblrow1)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 20:21









                Joel G MathewJoel G Mathew

                2,14792846




                2,14792846
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325607%2fhow-can-i-position-a-table-at-a-particular-x-and-y-coordinate-via-reportlab%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma