f0rd42
f0rd42

Reputation: 1459

Python ReportLab: Center string

I'm currently placing a string with:

can = canvas.Canvas(packet, pagesize=letter)
        can.setFont('Helvetica-Bold',12) # Change Font, Font-size, etc
        can.drawString(300, 340, row["name"]) # Change position. First number = points from left, secoond = points from bottom

I now have the need to center the string, i.e. just give drawstring the height, the string itself must always be centred.

I searched the net, but couldn't find an easy way to do so. Am I overseeing something?

thanks

Upvotes: 2

Views: 2972

Answers (1)

Vishal Singh
Vishal Singh

Reputation: 6234

Always try to put your text in a paragraph in that way you can always customize your text in the way you want.

doc = SimpleDocTemplate("paragraph_spacing.pdf",
                        pagesize=letter
                        )
# creating custom stylesheet
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='my_custom_name', fontName="Helvetica-Bold", fontSize=12, leftIndent=200))
# giving alias to the stylesheet
my_style = styles['my_custom_name']
flowables = []
flowables.append(Spacer(0, 0))
# using the stylesheet
flowables.append(Paragraph(row["name"], my_style))
doc.build(flowables)

you can use Spacer(0, height) to give height and leftIndent to mark the center of the width of your document

Upvotes: 1

Related Questions