Jan-19-2025, 08:30 AM
If you are happy with PDFs, you can use reportlab and have absolute and minute control over every aspect of formatting.
Read the reportlab_userguide.pdf, especially from page 72.
Here a small example of the almost endless possibilities:
Read the reportlab_userguide.pdf, especially from page 72.
Here a small example of the almost endless possibilities:
from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import A4 from reportlab.lib.colors import pink, green, brown, white, black from reportlab.lib.styles import ParagraphStyle from reportlab.lib.styles import getSampleStyleSheet path2sizes_pdf = 'reportlab/pdfs/text_sizes.pdf' stylesheet=getSampleStyleSheet() c = canvas.Canvas(path2sizes_pdf, pagesize=A4) def fonts(canvas): from reportlab.lib.units import inch text = "Now is the time for all good men to..." x = 3 * inch y = 8 * inch size = 13 for font in canvas.getAvailableFonts(): canvas.setFont(font, size) canvas.drawString(x,y,text) canvas.setFont("Helvetica", size) canvas.drawRightString(x,y, font+":") y = y-size size = size + 1 fonts(c) c.showPage() # closes the page, can't add more to this page c.save() path2colour_pdf = 'reportlab/pdfs/text_colours.pdf' mycolours = [pink, green, brown, white, black] def colours(canvas): from reportlab.lib.units import inch text = "Now is the time for all good men to..." x = 2 * inch y = 8 * inch for colour in mycolours: canvas.setFont("Helvetica", 14) c.setFillColor(colour) canvas.drawString(x,y,text) y = y-15 colours(c) c.showPage() # closes the page, can't add more to this page c.save()You can direct reportlab to any font you like and then use that font.
from reportlab.pdfbase import pdfmetrics # important for getting a Chinese ttf fontpath = '/home/pedro/.local/share/fonts/' ttfFile = os.path.join(fontpath, '萌萌哒情根深种-中文.ttf') pdfmetrics.registerFont(TTFont("Chinese", ttfFile))