HELP Printing with wxPython

M

Mario

Hello all, I'm trying hard to make possible to print some simple text from
python to the default printer using wxPython, after days of internet
searches I found this page: http://wiki.wxpython.org/index.cgi/Printing but
is impossible to use this script even if I do exactly as said there. I think
the script is buggy or I am not able to use it, even if seems very simple to
use...

Anyone can give me an hint on how to easily and simply print some text? Is
there a library ready to download and use? Something like SendPrinter("some
text\n")?

Thanks in advance if anyone can give any help.

Mario
 
T

Tim G

Hello all, I'm trying hard to make possible to print some simple text
from
python to the default printer using wxPython, after days of internet
searches I found this page:
http://wiki.wxpython.org/index.cgi/Printing but
is impossible to use this script even if I do exactly as said there. I think
the script is buggy or I am not able to use it, even if seems very simple to
use...

Anyone can give me an hint on how to easily and simply print some text? Is
there a library ready to download and use? Something like SendPrinter("some
text\n")?

On the strict wxPython front, I can't help
you, but I might be able to offer some help.
Depends a little bit on how much you're tied
to wxPython and how much you need to be cross-platform.

Essentially, if you're on Windows (and have no need
to run on anything else) then consider some of the
solutions here:

http://tgolden.sc.sabren.com/python/win32_how_do_i/print.html

If you're on Unix / whatever with no need to do
anything else, then I'm sure there are straightforward
ways to push stuff to a printer. (Something using lpr
he thinks, hoping someone with real knowledge can chip
in).

If you *really* want to use wxPython for cross-platform
needs, or just because you think it's cleaner, then try
on the wxPython lists if you haven't already.

TJG
 
L

Larry Bates

Mario,

Here is a function stripped from a working program that uses printpreview
from wxWindows to print out cells from a grid that the user is working
on. Hopefully this can point you in the proper direction.

Regards,
Larry Bates

def DO_printpreview(self, event):
if self._trace:
self.logf.writelines("T","Entering OuterFrame.DO_printpreview")
self.SetStatusText('Generating Print Preview', 0)
#
# Define the columns on the printout
#
columndefs=(("Cust#", 8.0/16.0, wxALIGN_LEFT),
("Customer Name", 29.0/16.0, wxALIGN_LEFT),
("Reference", 16.0/16.0, wxALIGN_LEFT),
("Type", 6.0/16.0, wxALIGN_CENTRE),
("Inv. #", 8.0/16.0, wxALIGN_CENTRE),
("InvDate", 9.0/16.0, wxALIGN_CENTRE),
("L#", 4.0/16.0, wxALIGN_CENTRE),
("Item #", 12.0/16.0, wxALIGN_CENTRE),
("Item Description",45.0/16.0, wxALIGN_LEFT),
("Qty", 6.0/16.0, wxALIGN_RIGHT),
("Cost", 10.0/16.0, wxALIGN_RIGHT),
("OMMC $", 10.0/16.0, wxALIGN_RIGHT)
)
columncount=len(columndefs)
#
# Create a new frame for the print preview and give it to PrintTable
#
self.previewframe=PreviewFrame(None, sys.stdout)
if self._trace:
log.writelines("T","DO_ppv-Creating PrintTable instance")
prt=PrintTable(self.previewframe)
#
# Set printout column widths
#
if self._trace: self.logf.writelines("T","DO_ppv-Setting column widths")
prt.set_column=[a[1] for a in columndefs]
#
# Set the column labels (headings)
#
if self._trace:
self.logf.writelines("T","DO-ppv-Setting column headings")
prt.label=[a[0] for a in columndefs]
#
# Set the column alignments
#
if self._trace:
self.logf.writelines("T", "DO-ppv-Setting column alignments")
map(prt.SetColAlignment, range(columncount), [a[2] for a in columndefs])
#
# Get the data values from the grid that are selected
#
data=self._getdata('1')
#
# Skip the first (flag) column and columns 11,13,15,16
# to delete them from the printout.
#
data=[[v[1],v[2],v[3],v[4][0:2],v[5],v[6],
v[7],v[8],v[9],v[10],v[12],v[14]] for v in data]
#
# Loop over the lines and insert a blank line between customers
#
blankline=columncount*[''] # Create a blank output line
lastcustomer=None
pdata=[]
for values in data:
if lastcustomer != None and values[0] != lastcustomer:
pdata.append(blankline)

lastcustomer=values[0]
pdata.append(values)

pdata.append(blankline)
prt.data=pdata
prt.left_margin=0.3
prt.SetLandscape()
prt.SetHeader("OMMC Recap Listing-Hardware Items")
prt.SetFooter()
prt.SetFooter("Date: ", type = "Date & Time", \
align=wxALIGN_LEFT, \indent=prt.left_margin)
#
# Override the default font sizes
#
prt.text_font_size = 8
#prt.label_font_size= 7
prt.cell_left_margin = 1.0/32.0
prt.Preview()
if self._trace:
self.logf.writelines("T","Entering OuterFrame.DO_printpreview")
return
 
J

James Carroll

Hi Mario,
Something like SendPrinter("some text\n")?

If you are doing this just for yourself, and you know you have a
printer that will really print just the plain text when you send it
plain text (like a dot matrix printer from the early 90s) then you can
probably open the printer device and send it text. Under windows you
can try opening LPT1 or on unix it's probably /dev/lpr or something
like that.

wxPython makes it pretty easy to give full modern support for any
printer, including letting the user select which printer to print to,
and getting a print preview, choosing paper size, etc.

I especially like the HtmlEasyPrinting write-up here:
http://wiki.wxpython.org/index.cgi/Printing

-Jim
 
M

Mario

Larry Bates said:
Mario,

Here is a function stripped from a working program that uses printpreview
from wxWindows to print out cells from a grid that the user is working
on. Hopefully this can point you in the proper direction.

Thank you very much for the help, I will study all the code that you sent me
for a future and a more professional programming, for now I used a very
simple win32api call, not very evolved but working fine for what I needed,
since what I am doing needs to run only on a windows machine.

Thanks again :)

Mario
 
M

Mario

James Carroll said:
I especially like the HtmlEasyPrinting write-up here:
http://wiki.wxpython.org/index.cgi/Printing

Thank you for your suggestion but I'm just not able to make it work, as i
said on the original post, I do exactly what is wrote there, but it gives
errors, I think the script is not updated and not working fine with the
latest versions of python or wxpython.

Mario
 
J

jeff elkins

Under Unix, you shouldn't be able to open the line printer, whether
it's /dev/lpr or /dev/lpt0 or whater. You shouldn't have
permission.

Instead, as was suggested earlier, use the "lpr" command and send it
the text/data on standard input. Any reasonably managed Unix system
should be able to handle a fair range of graphics formats, though
postscript is preferred.

<mike

I've been using:

p=os.popen('lp','w')
p.write("some text')
p.Close()


Jeff
 
M

Mike Meyer

James Carroll said:
If you are doing this just for yourself, and you know you have a
printer that will really print just the plain text when you send it
plain text (like a dot matrix printer from the early 90s) then you can
probably open the printer device and send it text. Under windows you
can try opening LPT1 or on unix it's probably /dev/lpr or something
like that.

Under Unix, you shouldn't be able to open the line printer, whether
it's /dev/lpr or /dev/lpt0 or whater. You shouldn't have
permission.

Instead, as was suggested earlier, use the "lpr" command and send it
the text/data on standard input. Any reasonably managed Unix system
should be able to handle a fair range of graphics formats, though
postscript is preferred.

<mike
 
M

Mike Meyer

jeff elkins said:
I've been using:

p=os.popen('lp','w')
p.write("some text')
p.Close()

Yeah. System V called it lp. BSD called it lpr. Most modern systems
have both, as they are just a front end to the line printer (when was
the last time you saw an honest-to-gods line printer?) daemon.

<mike
 
J

James Carroll

There's a working version in the wxPython Demo...

If you run the wxpython demo, and choose Print Framework under
Miscellaneous it has code that looks a whole lot like what's on the
wiki, only you can test it and try it out and see if it's broken for
you there. If anything doesn't work, let the wxPython mailing list
people know about it, and they'll have a work-around or a fix (or show
you what you should be doing) in no time.

-Jim
 

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

Similar Threads

Help 1
Troubles with Fullpage / please help 0
Help with Loop 0
[Code Challenge] WxPython versus Tkinter. 220
Help with my responsive home page 2
Need help with this script 4
printing with wxPython 3
Help with code 0

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top