Python Forum
How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file?
#9
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:

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))
Reply


Messages In This Thread
RE: How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? - by Pedroski55 - Jan-19-2025, 08:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 1,083 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,796 Oct-17-2024, 01:15 AM
Last Post: Winfried
  Print text with big font and style tomtom 6 21,245 Aug-13-2024, 07:26 AM
Last Post: yazistilleriio
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,516 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 1,774 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Python code for alignment and font size 1418 0 1,021 Jan-14-2024, 03:56 AM
Last Post: 1418
  write to csv file problem jacksfrustration 11 5,743 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 4,153 Nov-09-2023, 10:56 AM
Last Post: mg24
  logging: change log file permission with RotatingFileHandler erg 0 2,691 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 2,349 Jul-17-2023, 10:15 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020