Lino Website

Documentation · Modules · adamo · Examples

textprinter1.py

This example shows how to use TextPrinter objects.

Source code:

# -*- coding: latin1 -*-
def doit(tp):
    tp.writeln("")
    tp.writeln("TextPrinter Test page")
    tp.writeln("")
    cols = int(tp.lineWidth() / 10) + 1
    tp.writeln("".join([" "*9+str(i+1) for i in range(cols)]))
    tp.writeln("1234567890"*cols)
    tp.writeln("")
    tp.writeln("Here is some \033b1bold\033b0 text.")
    tp.writeln("Here is some \033u1underlined\033u0 text.")
    tp.writeln("Here is some \033i1italic\033i0 text.")

    tp.writeln("Here is some more text.")
    tp.writeln(u"Ännchen Müller machte groáe Augen.")
    tp.write("And here")
    tp.write(" is some")
    tp.write(" frag")
    tp.writeln("mented text.")

    tp.writeln()
    tp.write("This is a very long line. ")
    tp.write("Just do demonstrate that TextPrinter ")
    tp.write("doesn't wrap paragraphs for you...")
    tp.write("Blabla bla. "*20)
    tp.writeln("Amen.")
    tp.close()
        

if __name__ == "__main__":
    # do it on sys.stdout:
    from lino.textprinter.plain import PlainTextPrinter
    doit(PlainTextPrinter())

    # do it in a PDF document:
    from lino.textprinter.pdfprn import PdfTextPrinter
    doit(PdfTextPrinter("tmp.pdf"))

    # do it in a HTML file:
    from lino.textprinter.htmlprn import HtmlTextPrinter
    doit(HtmlTextPrinter("tmp.html"))

    # do it on the default printer:
    if False:
        from lino.textprinter.winprn import Win32TextPrinter
        doit(Win32TextPrinter(self))


Output:

+------------------------------------------------------------------------+
|                                                                        |
|TextPrinter Test page                                                   |
|                                                                        |
|         1         2         3         4         5         6         7  |
|123456789012345678901234567890123456789012345678901234567890123456789012|
|                                                                        |
|Here is some bold text.                                                 |
|Here is some underlined text.                                           |
|Here is some italic text.                                               |
|Here is some more text.                                                 |
|Ännchen Müller machte groáe Augen.                                      |
|And here is some fragmented text.                                       |
|                                                                        |
|This is a very long line. Just do demonstrate that TextPrinter doesn't w|
+------------------------------------------------------------------------+

To doit() method is called three times, each time with a different variant of textprinter Document instance.

A textprinter document has only three basic methods: write(), writeln() and endDoc().

Simple text formatting can be done by using escape sequences (as in the example) or by using the formatting methods setCpi(), setBold(), setItalic(), ...

write() may not be used to print several lines. Text that is longer than the document's lineWidth() will be cut off. This is usually what happens if you print too much text to a line of a physical text printer.

Refering articles:

Copyright 2001-2007 Luc Saffre.
http://lino.saffre-rumma.ee
Generated 2007-06-07 16:23:41