Printing a text file using Python

Discussion in 'Python' started by Serge Guay, Oct 25, 2003.

  1. Serge Guay

    Serge Guay Guest

    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.

    Thanks in advance...
    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.524 / Virus Database: 321 - Release Date: 10/6/2003
    Serge Guay, Oct 25, 2003
    #1
    1. Advertising

  2. Serge Guay

    Tim Roberts Guest

    "Serge Guay" <> 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)

    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,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Oct 25, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon B
    Replies:
    7
    Views:
    7,479
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    499
    msalters
    Dec 9, 2004
  3. iffy agbim
    Replies:
    1
    Views:
    95
    Mark Andrews
    May 24, 2004
  4. iffy agbim
    Replies:
    0
    Views:
    95
    iffy agbim
    May 21, 2004
Loading...

Share This Page