Gewusst wie: hinzufügen bold und normal text in einer Zeile mit drawString-Methode in reportlab (python)

Ich bin neu reportlab lib, bin ich lernen Sie gleichzeitig arbeiten und auf ein college-Projekt.
Ich habe erstellt eine desktop-Anwendung in wxpython, das Ergebnis in die Speicherung der Daten in PDF.

Möchte ich hinzufügen, dass 2 Zeilen in mein pdf. Dort ist die Zeile beginnt mit einer Benutzereingabe name, dann einige Worte, wieder bei der 2. Zeile einige Wörter, Benutzernamen und dann wieder einige Worte...

Ich habe versucht, einige Paragraph und canvas Methoden und Klassen, aber ich war nicht in der Lage, um die gewünschte Ausgabe erhalten.

Gewünschte Ausgabe:

Alex arbeitet, college-Projekt.

reportlab ist eine sehr gute lib, Alex mochte es.

Mein code:

import os
import reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.pdfmetrics import registerFontFamily
from reportlab.pdfbase.ttfonts import TTFont

# Registered font family
pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf'))
pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf'))
pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))
# Registered fontfamily
registerFontFamily('Vera',normal='Vera',bold='VeraBd',italic='VeraIt',boldItalic='VeraBI')

# Output pdf file name.
can = canvas.Canvas("Bold_Trail.pdf", pagesize=A4)

# Setfont for whole pdf.
can.setFont('Vera', 12)

# student name variable.
student_name ="Alex"

# Content.
line1 = " is working on college project."
line2 = "Reportlab is very good lib, "
line3 = " liked it.<br>"

# Joining whole content together.
content = "<strong>" + student_name + "</strong>" + line1
content2 = line2 + "<strong>"+student_name + "</strong>" + line3

# drawString location calculation.
x = 0; y = 8.5 * 72

# First string.
can.drawString(x,y, content)

y = y - 72

# Second String.
can.drawString(x,y, content2)

# Create PDF.
can.save()

Gibt es eine andere Möglichkeit außer mit XML-Methode <strong> oder <b> das nicht im obigen Programm?

Die Worte bleiben auf einer Linie.

InformationsquelleAutor Omkar | 2015-01-01

Schreibe einen Kommentar