Left-align image using Python ReportLab with Platypus flowable?

646 Views Asked by At

How do I left align an image that I've added to a PDF using reportlab platypus? By default, the image gets centered.

from reportlab.lib.units import cm, inch
from reportlab.lib.pagesizes import LETTER
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image
from reportlab.lib.styles import getSampleStyleSheet

text = "<b>BOLD</b> normal <br/><br/>" + \
    " After newline <font color=red>red</font>" + \
    " <font size=20>Larger</font> normal.<br/><br/>"
    
new_pdf_path = "pdf_path"
img_path = "img_path"

doc = SimpleDocTemplate(new_pdf_path,pagesize=LETTER,rightMargin=1*inch,leftMargin=1*inch,topMargin=1*inch,bottomMargin=1*inch)

parts = []
parts.append(Paragraph(text, getSampleStyleSheet()['Normal']))

# HOW left align this 1-inch wide image?
parts.append(Image(img_path, 1*inch, 1*inch))

doc.build(parts)
1

There are 1 best solutions below

0
On

I figured it out.

From here.

parts.append(Image(qr_img_with_path, 1*inch, 1*inch,hAlign="LEFT"))