pythontextprinting

How to print text with specific format to a printer in Python?


I am printing a text string to a printer like so:

import os

string_value = 'Hello, World!'

printer = os.popen('lpr -P Canon_iP110_series', 'w')
printer.write(string_value)
printer.close()

This works perfectly, printing the text to the printer in what I assume is the default font/color (black).

I want to change some features of the text, though. For example, I want to bold the word 'Hello', perhaps, or print 'World' in green maybe.

I have found several answers having to do with "printing" text, but they're giving escape codes relating to the terminal/console output - not to a printer. Eg, How can I print bold text in Python?

These escape codes do work when I print the string to the console. Eg,

bold_on = '\033[1m'
bold_off = '\033[0m'

string_value = '{0}Hello{1}, World!'.format(bold_on, bold_off)

print(string_value)

does output:

Hello, World!

to the console. However, it does not bold the 'Hello' text on the printer.

How can I bold text at least, and possibly change other font attributes, when printing text to a printer in Python?

(Python 3.9+)


Solution

  • As @chepner commented, it seems Python is pretty basic when it comes to printing text to a printer. What I ended up doing instead is using FPDF v1.7.2 to place the text in a PDF file. Sending the file to the printer, then, outputs the text with the attributes I defined.

    The FPDF-related functions in my example should work the same on all platforms.
    The printer-related functions in this example should work on macOS and Linux. Windows would require different methods for printing.

    import os  # for printing to printer
    from fpdf import FPDF  # for pdf creation
    
    # set up pdf basics:
    pdf = FPDF('P', 'pt', 'Letter')  # P(ortrait), points size ref, Letter-size paper
    pdf.add_page()  # add a blank page to start
    pdf.set_font('helvetica', size=12)  # optional here, but useful if most text is plain
    
    hello_string = 'Hello'
    world_string = ', World!'
    
    # set font to B(old) *and* I(talic)
    pdf.set_font('helvetica', size=12, style='BI')
    
    # write string into PDF page, stopping at end of string; line height = 6 pts
    pdf.write(h=6, txt=hello_string)
    
    # change back to regular font
    pdf.set_font('helvetica', size=12)
    
    # write rest of string into PDF
    pdf.write(h=6, txt=world_string)
    
    
    # output the created page(s) as a PDF file
    pdf_filename = 'hello_world.pdf'
    pdf.output(pdf_filename)
    
    
    # finally, print the PDF file to the printer
    printer_name = 'Canon_iP110_series'
    printer_string = 'lpr -P {0} {1}'.format(printer_name, pdf_filename)
    os.system(printer_string)
    

    Notes:

    UPDATE (November 2024): There's an updated fork of fpdf that's up-to-date and well-maintained. It's called fpdf2, and it's at version 2.8.1 as of this writing.