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

Discussão em 'Python' iniciado por Stack, Novembro 20, 2024.

  1. Stack

    Stack Membro Participativo

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

    Continue reading...

Compartilhe esta Página