简体   繁体   中英

How to position and space multiple images in reportlab Python

I'm experimenting with reportlab, and trying to space and position two images. The images are of a seal, and when I run the code, the pdf has the two images stacked directly on top of each other. I'd like to be able to space them, and position them with coordinates for the x and y axis, but I can't find any way to do so. I got the original code from this website link here, which is probably easier to copy. http://matthiaseisen.com/pp/patterns/p0150/ Can anyone help? Thanks.

from reportlab.platypus import (
              BaseDocTemplate, 
              PageTemplate, 
              Frame, 
              Paragraph,
              ParagraphAndImage,
              Image
                                 )
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_LEFT, TA_CENTER
from reportlab.lib.units import inch
from reportlab.lib.colors import (
                        black,
                         purple,
                          white,
                           yellow
                                 )

def stylesheet():
                          styles= {
                      'default': ParagraphStyle(
                        'default',
                        fontName='Times-Roman',
                        fontSize=10,
                        leading=12,
                        leftIndent=0,
                        rightIndent=0,
                        firstLineIndent=0,
                        alignment=TA_LEFT,
                        spaceBefore=0,
                        spaceAfter=0,
                        bulletFontName='Times-Roman',
                        bulletFontSize=10,
                        bulletIndent=0,
                        textColor= black,
                        backColor=None,
                        wordWrap=None,
                        borderWidth= 0,
                        borderPadding= 0,
                        borderColor= None,
                        borderRadius= None,
                        allowWidows= 1,
                        allowOrphans= 0,
                        textTransform=None,  # 'uppercase' | 'lowercase' |                 None
        endDots=None,         
        splitLongWords=1,
                ),
                }
      styles['title'] = ParagraphStyle(
            'title',
            parent=styles['default'],
            fontName='Helvetica-Bold',
            fontSize=24,
            leading=42,
            alignment=TA_CENTER,
            textColor=purple,
           )
         styles['alert'] = ParagraphStyle(
               'alert',
         parent=styles['default'],
         leading=14,
         backColor=yellow,
         borderColor=black,
         borderWidth=1,
         borderPadding=5,
         borderRadius=2,
         spaceBefore=10,
         spaceAfter=10,
    )
     return styles


def build_flowables(stylesheet):

    im = Image("seal.png", 3*inch, 3*inch)
             return [
    Paragraph("I'm a title!", stylesheet['title']),
    Paragraph('some text. ' * 30, stylesheet['default']),
    Paragraph('This is important!', stylesheet['alert']),
    Paragraph('. ' * 20, stylesheet["default"]),
    im,
    Paragraph('. ' * 40, stylesheet['default']),
    Paragraph('. ' * 40, stylesheet["default"]),
    im  ]




def build_pdf(filename, flowables):
   doc = BaseDocTemplate(filename)
    doc.addPageTemplates(
    [
        PageTemplate(
            frames=[
                Frame(
                    doc.leftMargin,
                    doc.bottomMargin,
                    doc.width,
                    doc.height,
                    id=None
                ),
            ]
        ),
       ] 
    )
  doc.build(flowables)

build_pdf('Project/Report/lab5.pdf', build_flowables(stylesheet()))

I found the solution. Import Spacer function from platypus

      from reportlab.platypus import (
          BaseDocTemplate, 
          PageTemplate, 
          Frame, 
          Paragraph,
          ParagraphAndImage,
          Image,
          Spacer
                             )

Then, inbetween any image, do something like Spacer(width = 1, height = 20), and you should get a space inbetween the images. Hope that helps anyone else, cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM