Discussion:
[python-win32] printing landscape in Windows
Hans Becker
2015-06-10 18:48:16 UTC
Permalink
Just about a month ago I started reading Learning Python by Mark Lutz.
It's a new paradigm for me. I have a whole bunch of old mainframe files
with carriage control characters as the 1st character in each line.
Well, a got a very simple script working except I need to be able to
print landscape on my pc printer. Does anyone by chance have sample code I
could cut and paste to print landscape? I'm running Windows 7 Pro and
Python 2.7


#prt1a is a copy of prt11

import os, sys, tempfile
import win32api, win32print

printer_name = win32print.GetDefaultPrinter ()
hPrinter = win32print.OpenPrinter (printer_name)

hJob = win32print.StartDocPrinter (hPrinter, 1, ('alfie', None, "RAW"))
win32print.StartPagePrinter (hPrinter)

for line in open('alf1'):
cc = line[0]
line = line[1:]
line = line.rstrip()

if cc == '1': line = '\f' + line
elif cc == ' ': line = '\n' + line
elif cc == '0': line = '\n\n' + line
elif cc == '-': line = '\n\n\n' + line

line = line + '\r' # takes care of the overprinting
win32print.WritePrinter(hPrinter, line)

win32print.WritePrinter(hPrinter,"\f")
win32print.EndPagePrinter (hPrinter)
win32print.EndDocPrinter (hPrinter)
win32print.ClosePrinter (hPrinter)


my test data 'alf1':

1 QWDDNNNDNDN
++2 /111111111
++2a XXXXXXXX11
3 ZDDDDNDNDNNN
++4 #/1111111111
++4a #XXXXXXXXX11
++4 b #WWWWWWWWWW1
5 MMMMMMNMMMMMMMMNMNNNNMNNNM
++6 IIIIII1IIIIIIII1I1111I111I
++6a IIIIII1IIIIIIII1I1111I111I
7 NNMNNNNNNNNNNNNNNNNNNNNNMM
++8 11I111111111111111111111II
9 ..........................
10 --------------------------
11 ;;;;;;;;


Thanks

Hans Becker
Todd Fiske
2015-06-10 22:43:40 UTC
Permalink
Does anyone by chance have sample code I could cut and paste to print
landscape?
I don't have a cut and paste example on hand, but you might benefit from
reading the source of this module, or even using it:

MSWinPrint 1.1 : Python Package Index
https://pypi.python.org/pypi/MSWinPrint

The method they show for setting landscape mode (setting
devmode.Orientation) looks like it will blend in with what you have with
only a little adjustment.

Todd
Todd Fiske
2015-06-11 04:20:52 UTC
Permalink
It seems I'm wrong about just needing a little adjustment. When you use
WritePrinter, you are sending raw bytes to the printer, thanks to opening
the device in "RAW" mode. In this mode, you would need to send the actual
printer escape code that sets it to Landscape, in the language of its
default driver which may be PCL or XPS, or may be something proprietary.

In contrast, MSWinPrint creates a printer device context (dc) and draws to
it with GDI commands, which is a more flexible device-independent method.

Apologies for the false lead, I should have tested it first.
Post by Todd Fiske
Does anyone by chance have sample code I could cut and paste to print
landscape?
I don't have a cut and paste example on hand, but you might benefit from
MSWinPrint 1.1 : Python Package Index
https://pypi.python.org/pypi/MSWinPrint
The method they show for setting landscape mode (setting
devmode.Orientation) looks like it will blend in with what you have with
only a little adjustment.
Todd
Andreas Holtz
2015-07-13 21:40:00 UTC
Permalink
Think about using a batch print tool like
http://www.verypdf.com/app/pdf-print-cmd/
or
http://www.doc2prn.com/

I used the first one and implemented a Python script to create the command (it prints more than PDF).

Some professional printer offer a webserver where you can upload the files. Check this, too.

As you only want to print text files, you can use AutoIt to automate Notepad++ or the like.
Set the default setting of the printer to landscape manually before.

Regards

Andreas
Post by Hans Becker
Just about a month ago I started reading Learning Python by Mark Lutz.
It's a new paradigm for me. I have a whole bunch of old mainframe files
with carriage control characters as the 1st character in each line.
Well, a got a very simple script working except I need to be able to
print landscape on my pc printer. Does anyone by chance have sample code I
could cut and paste to print landscape? I'm running Windows 7 Pro and
Python 2.7
#prt1a is a copy of prt11
import os, sys, tempfile
import win32api, win32print
printer_name = win32print.GetDefaultPrinter ()
hPrinter = win32print.OpenPrinter (printer_name)
hJob = win32print.StartDocPrinter (hPrinter, 1, ('alfie', None, "RAW"))
win32print.StartPagePrinter (hPrinter)
cc = line[0]
line = line[1:]
line = line.rstrip()
if cc == '1': line = '\f' + line
elif cc == ' ': line = '\n' + line
elif cc == '0': line = '\n\n' + line
elif cc == '-': line = '\n\n\n' + line
line = line + '\r' # takes care of the overprinting
win32print.WritePrinter(hPrinter, line)
win32print.WritePrinter(hPrinter,"\f")
win32print.EndPagePrinter (hPrinter)
win32print.EndDocPrinter (hPrinter)
win32print.ClosePrinter (hPrinter)
1 QWDDNNNDNDN
++2 /111111111
++2a XXXXXXXX11
3 ZDDDDNDNDNNN
++4 #/1111111111
++4a #XXXXXXXXX11
++4 b #WWWWWWWWWW1
5 MMMMMMNMMMMMMMMNMNNNNMNNNM
++6 IIIIII1IIIIIIII1I1111I111I
++6a IIIIII1IIIIIIII1I1111I111I
7 NNMNNNNNNNNNNNNNNNNNNNNNMM
++8 11I111111111111111111111II
9 ..........................
10 --------------------------
11 ;;;;;;;;
Thanks
Hans Becker
_______________________________________________
python-win32 mailing list
https://mail.python.org/mailman/listinfo/python-win32
Loading...