473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Printing a file to a printer &/or changing the device that is referred to by stdout

Hi;
I am writing my first python program and would like to know how to
change stdout to refer to my default printer or any other printer on
my network. The other question is, Is there an API set of classes that
allow me to interact with network devices.

In other words, if I have a path and filename inside of a string
variable in python, how do I send the file to the printer??

Thanks in advance

Jody Burgess
jo**********@sewardconsulting.com
Jul 18 '05 #1
2 2569
Jody,

Here is a class that I wrote some time ago that allows you
to print to Linux queue's (you didn't tell us if you are
on Linux, Mac or Windows). Hope it helps.

#! /usr/bin/python
"""
Name : lpr.py
Author : adapted from code by David Boddie
Created : Tue 08th August 2000
Last modified : Thu 06th September 2001
Purpose : Send a file to a printer queue.
"""

import os, socket, string, sys

class lprClass:
_debug=0
_trace=0

def __init__(self, host, user, server, printer):
#
# Handle empty arguments by assiging default values
#
if host: self.host=host
else: self.host=socket.gethostname()

if user: self.user=user
else:
try:
self.user=os.environ['USER']
except KeyError:

print "lprClass***Error no user passed to class and no USER env variable
found"
sys.exit(2)

if server: self.server=server
else: self.server=socket.gethostbyaddr(self.host)[2][0]

if printer: self.printer=printer
else:
try:
self.printer=os.environ['PRINTER']
except KeyError:
print "lprClass***Error no printer passed to class and no PRINTER env
variable found"
sys.exit(2)

if self._debug:
print "lprClass*** host=",self.host
print "lprClass*** user=",self.user
print "lprClass*** server=",self.server
print "lprClass*** printer=",self.printer

#
# Create a socket object to communcate with the LPR daemon
#
if self._trace: print "lprClass***Creating a socketobj instance"
self.socketobj=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self._trace: print "lprClass***Conecting to port 515 on server"
self.socketobj.connect((self.server, 515))
if self._trace: print "lprClass***Initializing LPR daemon for job to
printer=", self.printer
self.socketobj.send("\002"+self.printer+"\012")
d=self.socketobj.recv(1024)
#
# Wait for reply and check response string
#
errormsg="lprClass***Error initializing communications with LPR daemon"
if d != "\000": self._abort(d, errormsg)
return

def _initcontrol(self, filename):
#
# Build a control string
#
control='H'+self.host[:31]+"\012"+'N'+filename[:131]+"\012"+ \
'P'+self.user[:31]+"\012"+'o'+filename[:131]+"\012"
#---------------------------------------------------------------------------
---------
# Send the receive control file subcommand
#
if self._trace: print "lprClass***Sending the receive control file
subcommand"
self.socketobj.send("\002"+str(len(control))+" cfA000"+self.host+"\012")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Waiting for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in control string initialization for LPR
daemon"
if d != "\000": self._abort(d, errormsg)

#---------------------------------------------------------------------------
---------
# Send the control file
#
if self._trace: print "lprClass***Sending the control file command"
self.socketobj.send(control)
self.socketobj.send("\000")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Waiting for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in control string for LPR daemon"
if d != "\000": self._abort(d, errormsg)
return

def _abort(self, data, errormsg):
print errormsg
print data.strip()
self.socketobj.send("\001\012")
self.socketobj.close()
sys.exit(0)

def _closelpr(self):
self.socketobj.send("\000")
#
# Wait for reply
# print "Wait for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in _closelpr"
if d != "\000": self._abort(d, errormsg)
#
# Close the socket connection
#
self.socketobj.close()
return

def _sendheader(self, length):

#---------------------------------------------------------------------------
---------
# Send the receive data file subcommand
#
if self._trace: print "lprClass***Sending the receive data file
subcommand"
self.socketobj.send("\003"+str(length)+" dfA000"+self.host+"\012")
#
# Wait for reply and check response string
#
if self._trace: print "lprClass***Waiting for reply"
d=self.socketobj.recv(1024)
errormsg="lprClass***Error in receive data file subcommand for LPR daemon"
if d != "\000": self._abort(d, errormsg)
return
def queuefile(self, filename):
try:
ifile=open(filename,'r')
except:
print "lprClass***Unable to find filename=%s to print" % filename
return
#
# Call routine to initialize LPR daemon with control information
#
self._initcontrol(filename)
#
# Must determine the length of the file by seeking to the
# end, save for later
#
ifile.seek(0, 2)
length = ifile.tell()
if self._debug: print "lprClass***queuefile-length=",length
#
# Send file header to LPR
#
self._sendheader(length)
#
# Return to the beginning of the file
#
ifile.seek(0, 0)
#
# Send the data file
if self._debug: print "Sending the data file"
while 1:
contents = ifile.readline()
if not contents: break
self.socketobj.send(contents)

ifile.close()
self._closelpr()
return

def queuestring(self, string):
#
# Call routine to initialize LPR daemon with control information
#
self._initcontrol('')
self._sendheader(len(string))
self.socketobj.send(string)
self._closelpr()
return
Larry Bates
Syscon, Inc.
"Jody Burgess" <jo**********@sewardconsulting.com> wrote in message
news:26**************************@posting.google.c om...
Hi;
I am writing my first python program and would like to know how to
change stdout to refer to my default printer or any other printer on
my network. The other question is, Is there an API set of classes that
allow me to interact with network devices.

In other words, if I have a path and filename inside of a string
variable in python, how do I send the file to the printer??

Thanks in advance

Jody Burgess
jo**********@sewardconsulting.com

Jul 18 '05 #2

"Larry Bates" <lb****@swamisoft.com> wrote in message
news:4e********************@comcast.com...
Jody,

Here is a class that I wrote some time ago that allows you
to print to Linux queue's (you didn't tell us if you are
on Linux, Mac or Windows). Hope it helps.

#! /usr/bin/python
"""
Name : lpr.py
Author : adapted from code by David Boddie
Created : Tue 08th August 2000
Last modified : Thu 06th September 2001
Purpose : Send a file to a printer queue.
"""


FWIW, a *lot* of modern network printers will respond to LPR out of the box.
So the platform
is (possibly) moot.

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tom | last post by:
I am converting an old application that was printing directly to a specialized printer device (i.e. a special label printer). It was doing this by opening a file with the file path of 'LPT1:' and...
7
by: DazedAndConfused | last post by:
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the left and right where there is no print. The document displays perfect in print preview, but when I print it, about 1/2 inch...
5
by: Erik | last post by:
I'm using an ActiveX control to display a Gantt chart in my Access 2003 application. It's all setup and programmed and displays nicely. The control supports printing, but its documentation is...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
3
by: Pappy | last post by:
SHORT VERSION: Python File B changes sys.stdout to a file so all 'prints' are written to the file. Python file A launches python file B with os.popen("./B 2>&^1 >dev/null &"). Python B's output...
1
by: mehdi | last post by:
Hi, Consider a printing scenario where I have to draw the entire page on a 827x1169 (.01 inch) size. Thereafter, the entire bitmap has to be resized to fill a given Bounds rectangle (keeping the...
4
ADezii
by: ADezii | last post by:
Recently, there seems to be several questions specifically related to Printers and changing Printing characteristics for Forms and Reports. For this reason alone, I decided to dedicate this week's...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
1
by: billelev | last post by:
Here is some code that I have adapted slightly. It allows a report to be printed to a specific location. It works by calling SaveReportAsPDF and specifying the access report name, and the root...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.