Printing a text file using Python

S

Serge Guay

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...
 
T

Tim Roberts

Serge Guay said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top