Printing a text file using Python

Tim Roberts timr at probo.com
Sat Oct 25 02:17:34 EDT 2003


"Serge Guay" <serge at theguays.com> wrote:

>I have been trying to print a text file to my printer but the most I have
>been able to do is print one line. I have been using the following commands.
>
>        dc = win32ui.CreateDC()
> 	  dc.CreatePrinterDC()
>        dc.SetMapMode(4)        # This is UI_MM_LOENGLISH
>        # With this map mode, 12 points is 12*100/72 units = 16
>        font = win32ui.CreateFont({'name' : 'Arial', 'height' : 16})
>        dc.SelectObject(font)
>        f=open("./Reports/Report.txt","r")
>        memory=f.read()
>        f.close
>        memory.split('\n')
>        dc.StartDoc("./Reports/Report.txt")
>        dc.StartPage()
>        dc.TextOut(10,10,memory)
>        dc.EndPage()
>        dc.EndDoc()
>
>Can anyone tell me how I need to change this so it will print out the entire
>file instead of just one line.

The Windows GDI calls don't know anything about lists.  There are several
ways to do what you want:

  for i in range(memory):
    dc.TextOut(10, 10+16*i, memory[i])

Of course, that only works if everything fits on one page.  Another
alternative is to let Windows do the splitting, using dc.DrawText with
DT_CALCRECT.  If you try that, DON'T do the split.  Pass the whole string
to dc.DrawText.  It will STILl have to fit on one page.

If you have multiple pages, you will have to count the Y coordinates
yourself and call EndPage/StartPage.

That's why I use reportlab for my printing.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.




More information about the Python-list mailing list