Discussion:
Printing forms and labels in Python
Monte Milanuk
2010-06-13 04:19:49 UTC
Permalink
Hello,

I'm still a relative newbie to python, so I apologize if this is covered
in detail somewhere and I missed it.

I have a program or two that I want to work on once I get more
proficient with python and sqlite and tkinter/wxpython. One of the big
'features' of those programs I want to make is going to be printing out
match results (scores) from a competition along with competitor names
and other pertinent information. Currently most people are doing this
in Excel, though there is one (old) DOS program written in BASIC that
not only prints the match results and the reports for the sanctioning
body, but also prints labels via a Dymo Label maker.

I realized today that one thing I have never seen covered in any Python
tutorial is how to format and print things to a physical printer. I did
a little bit of searching and didn't come up with much... either I'm
really not using the right search terms, or physical printing is a black
hole/art...?

TIA,

Monte
Mel
2010-06-13 13:14:56 UTC
Permalink
Post by Monte Milanuk
I realized today that one thing I have never seen covered in any Python
tutorial is how to format and print things to a physical printer. I did
a little bit of searching and didn't come up with much... either I'm
really not using the right search terms, or physical printing is a black
hole/art...?
It's massively device-specific...

What I did when I needed precision printing was have my Python programs
generate PostScript code, then run that through ghostscript to do the actual
printing. PostScript was designed to control printers down to the pixel.
It's possible to streamline the production run by running ghostscript using
the subprocess module. Of course you have to learn PostScript to do this,
which is not really simple.

The other possibility that I'm aware of might be to get a graphics package
(the one built in to wx is the one I'm familiar with) and set up your
printable text and graphics in a appropriate device context suitable for
your printer.

Mel.
jfabiani
2010-06-13 14:22:08 UTC
Permalink
Post by Monte Milanuk
Hello,
I'm still a relative newbie to python, so I apologize if this is covered
in detail somewhere and I missed it.
I have a program or two that I want to work on once I get more
proficient with python and sqlite and tkinter/wxpython. One of the big
'features' of those programs I want to make is going to be printing out
match results (scores) from a competition along with competitor names
and other pertinent information. Currently most people are doing this
in Excel, though there is one (old) DOS program written in BASIC that
not only prints the match results and the reports for the sanctioning
body, but also prints labels via a Dymo Label maker.
I realized today that one thing I have never seen covered in any Python
tutorial is how to format and print things to a physical printer. I did
a little bit of searching and didn't come up with much... either I'm
really not using the right search terms, or physical printing is a black
hole/art...?
TIA,
Monte
It's still possible to send esc char's to the printer (just like the dos
program). So in days of DOS programs there were routines that matched
epsons, HP, and a few other printers that the dos programs used to print to
(http://webpages.charter.net/dperr/links/esc_p2.htm).

Idea was simple send the printer the esc codes and the text you wanted
printed. Just insure you have the right port (BTW not as easy as it used
to be).

Johnf
Joel Goldstick
2010-06-13 15:00:32 UTC
Permalink
Post by Monte Milanuk
Hello,
I'm still a relative newbie to python, so I apologize if this is covered
in detail somewhere and I missed it.
I have a program or two that I want to work on once I get more
proficient with python and sqlite and tkinter/wxpython. One of the big
'features' of those programs I want to make is going to be printing out
match results (scores) from a competition along with competitor names
and other pertinent information. Currently most people are doing this
in Excel, though there is one (old) DOS program written in BASIC that
not only prints the match results and the reports for the sanctioning
body, but also prints labels via a Dymo Label maker.
I realized today that one thing I have never seen covered in any Python
tutorial is how to format and print things to a physical printer. I did
a little bit of searching and didn't come up with much... either I'm
really not using the right search terms, or physical printing is a black
hole/art...?
TIA,
Monte
Why not go the other direction. Use python to do your processing, and
send the results to excel. There are python modules that read and write
excel files.

Joel Goldstick
Monte Milanuk
2010-06-13 16:40:58 UTC
Permalink
Why not go the other direction. Use python to do your processing, and
send the results to excel. There are python modules that read and write
excel files.
Well... partly because Excel is not exactly cross-platform. Granted,
the mass majority of people using a program like I have in mind will be
doing so on Windows, but I was hoping to *not* tie things to any one
platform if I could avoid it. Probably an overly ambitious goal at this
point.

The other part... maybe its my relative newbie experience level showing
here, but having to call/open Excel to print something from a python app
feels a little like admitting that python can't do it in a reasonable
manner. If I wanted to have to open Excel to print out the results, I
think I'd just as soon spend the effort and make the whole thing an
Excel/VBA app and skip python entirely.

Perhaps (probably) I'm not understanding some of how the lower level
systems stuff works here... I thought the print drivers were there to
take care of the low-level device-specific communication, and provide a
somewhat simplified common interface for regular programs like M$ Word,
Open Office, Adobe Reader, etc. to use. Those programs don't access the
hardware directly, do they? Don't they hand off the print job to the
OS-specific print drivers, and let them handle the spooling, etc.? From
my (admittedly limited) understanding of things on Windows, Mac & Linux,
that seems to be somewhat common between platforms. Isn't there some
sort of module in Python to do the same? I could understand it needing
to be somewhat *platform* specific, perhaps... but I have old copies of
M$ Office 2003 printing out just duckily on printers that only came out
on the market less than a year ago. I updated the printer drivers and
changed the default printer, but nothing printer-related in Word or
Excel changed (that I know of)... but it still manages to hand off to
the operating system print drivers just fine... which is kind of what I
figured Python would do.

Is that (to some degree) what ReportLab does?

TIA,

Monte
Diez B. Roggisch
2010-06-13 17:23:46 UTC
Permalink
Post by Monte Milanuk
Why not go the other direction. Use python to do your processing,
and
send the results to excel. There are python modules that read and
write
excel files.
Well... partly because Excel is not exactly cross-platform. Granted,
the mass majority of people using a program like I have in mind will
be doing so on Windows, but I was hoping to *not* tie things to any
one platform if I could avoid it. Probably an overly ambitious goal
at this point.
The other part... maybe its my relative newbie experience level
showing here, but having to call/open Excel to print something from a
python app feels a little like admitting that python can't do it in a
reasonable manner. If I wanted to have to open Excel to print out the
results, I think I'd just as soon spend the effort and make the whole
thing an Excel/VBA app and skip python entirely.
Perhaps (probably) I'm not understanding some of how the lower level
systems stuff works here... I thought the print drivers were there to
take care of the low-level device-specific communication, and provide
a somewhat simplified common interface for regular programs like M$
Word, Open Office, Adobe Reader, etc. to use. Those programs don't
access the hardware directly, do they? Don't they hand off the print
job to the OS-specific print drivers, and let them handle the
spooling, etc.? From my (admittedly limited) understanding of things
on Windows, Mac & Linux, that seems to be somewhat common between
platforms. Isn't there some sort of module in Python to do the same?
I could understand it needing to be somewhat *platform* specific,
perhaps... but I have old copies of M$ Office 2003 printing out just
duckily on printers that only came out on the market less than a year
ago. I updated the printer drivers and changed the default printer,
but nothing printer-related in Word or Excel changed (that I know
of)... but it still manages to hand off to the operating system print
drivers just fine... which is kind of what I figured Python would do.
Is that (to some degree) what ReportLab does?
No. Reportlab "just" generates PDF files which your operating system
will print. If you know how to - there used to be times when you just
needed to copy a file to a printer device to print it.

However, the overall problem here is that printer APIs are very
different between os and they aren't abstracted in python to some common
module. They need access to GUI libraries which python doesn't expose
out of the box.

So as sad as it may sound - going through some native program such as
excel or acrobat to perform the printing might be your best bet.

Diez
Monte Milanuk
2010-06-13 18:13:08 UTC
Permalink
Post by Diez B. Roggisch
However, the overall problem here is that printer APIs are very
different between os and they aren't abstracted in python to some common
module. They need access to GUI libraries which python doesn't expose
out of the box.
I know the usual response to what I'm about to say is "Knock yourself
out, let us know when you have something workable" but I have to say it
anyways... wow, that seems like such a gaping hole that I can't hardly
believe its not filled. Printing final results is a *huge* part of what
I'm wanting to do.

Seems like it'd be a huge opportunity for someone wanting to
improve/contribute to python, or like one of the 'summer of code'
projects, etc.
Post by Diez B. Roggisch
So as sad as it may sound - going through some native program such as
excel or acrobat to perform the printing might be your best bet.
Opening Adobe Reader as a sort of 'print preview' might be a workable
solution... since the results will likely need to be a) archived and b)
emailed out to the group.
rantingrick
2010-06-13 19:24:39 UTC
Permalink
Post by Monte Milanuk
Post by Diez B. Roggisch
However, the overall problem here is that printer APIs are very
different between os and they aren't abstracted in python to some common
module. They need access to GUI libraries which python doesn't expose
out of the box.
I know the usual response to what I'm about to say is "Knock yourself
out, let us know when you have something workable" but I have to say it
anyways... wow, that seems like such a gaping hole that I can't hardly
believe its not filled. ?Printing final results is a *huge* part of what
I'm wanting to do.
Seems like it'd be a huge opportunity for someone wanting to
improve/contribute to python, or like one of the 'summer of code'
projects, etc.
EXACTLY! This is a major missing piece of Python stdlib or at least a
needed 3rd party module. But in order for it to be adopted we need to
make it cross platform. I would also like to include some Image
Acquisition capabilities just to round out the usability of the thing.
I can help on the windows side unfortunately i know nothing of mac and
Linux so if anyone else wants to try to create a stdlib "acceptable"
module (or just a good 3rd party mod) i will offer my limited win32
experiences to the pile.

As far as printing raw text to a printer Tim Golden has posted some
code here. Although limited it's a good start...

http://timgolden.me.uk/python/win32_how_do_i/print.html

Also i found an incomplete script many moons ago if you are
interested. It's needs polishing however it's a start. The script
processes raw test and organizes it into pages before sending the data
to printer (win32 only!) I cannot find the link but if anybody wants
the script contact me. I need to wrap it into a class. It's pretty
ugly, albeit working code.
Gregory Ewing
2010-06-18 01:58:08 UTC
Permalink
Post by Monte Milanuk
Opening Adobe Reader as a sort of 'print preview' might be a workable
solution.
Or if you think Acrobat Reader sucks too much, Foxit Reader
is a nice, lightweight alternative:

http://www.foxitsoftware.com/pdf/reader/reader3.php
--
Greg
Anssi Saari
2010-06-13 18:12:52 UTC
Permalink
Post by Monte Milanuk
Hello,
I'm still a relative newbie to python, so I apologize if this is
covered in detail somewhere and I missed it.
I have a program or two that I want to work on once I get more
proficient with python and sqlite and tkinter/wxpython. One of the
big 'features' of those programs I want to make is going to be
printing out match results (scores) from a competition along with
competitor names and other pertinent information. Currently most
people are doing this in Excel, though there is one (old) DOS program
written in BASIC that not only prints the match results and the
reports for the sanctioning body, but also prints labels via a Dymo
Label maker.
I actually looked into label printers recently. It seems that at least
the cheaper models from Brother and Dymo accept a bitmap in specific
dimensions and they print it pixel exactly. Very simple, in other
words. But different printers need different formats, which is why
there are printer drivers. I'd assume the DOS program you mention
supports a very specific Dymo printer?

Anyways, for operating systems using CUPS for printing (that would be
Mac OS X, Linux, *BSD at least), there seems to be pycups which wraps
the CUPS API.
Post by Monte Milanuk
From a quick study of
http://timgolden.me.uk/python/win32_how_do_i/print.html, it looks like
in Windows you can just use the win32 APIs for printing, which is
hard. An easier alternative seems to be using PIL to generate a DIB
with your data in it and printing that.
Monte Milanuk
2010-06-13 18:33:17 UTC
Permalink
Post by Anssi Saari
I actually looked into label printers recently. It seems that at least
the cheaper models from Brother and Dymo accept a bitmap in specific
dimensions and they print it pixel exactly. Very simple, in other
words. But different printers need different formats, which is why
there are printer drivers. I'd assume the DOS program you mention
supports a very specific Dymo printer?
Honestly, I don't know. The interface of that program usually has me
grinding my teeth within minutes. It was written years ago, and is in
fact now being updated... to a 'new' version of BASIC (still with a
'DOS' interface) only because the existing version will not run on
anything newer than Win XP due to memory issues (assuming I understood
the problem correctly). I was looking today and see that Dymo makes a
SDK available that is supposed to be cross-platform (more specifically,
download the SDK for the platform you want to use), but given the age of
this setup... I'm guessing it was probably hard-coded to a particular
device.

The labels are sliding backwards into the 'nice to have' category.
Being able to print out the final results sheets (one or two pages at
the end of the day) is the primary goal at this point.
Post by Anssi Saari
Anyways, for operating systems using CUPS for printing (that would be
Mac OS X, Linux, *BSD at least), there seems to be pycups which wraps
the CUPS API.
Post by Monte Milanuk
From a quick study of
http://timgolden.me.uk/python/win32_how_do_i/print.html, it looks like
in Windows you can just use the win32 APIs for printing, which is
hard. An easier alternative seems to be using PIL to generate a DIB
with your data in it and printing that.
Actually... for the results sheet, the shellExecute method he described
just might work.
Joel Goldstick
2010-06-13 18:30:01 UTC
Permalink
Post by Monte Milanuk
Hello,
I'm still a relative newbie to python, so I apologize if this is covered
in detail somewhere and I missed it.
I have a program or two that I want to work on once I get more
proficient with python and sqlite and tkinter/wxpython. One of the big
'features' of those programs I want to make is going to be printing out
match results (scores) from a competition along with competitor names
and other pertinent information. Currently most people are doing this
in Excel, though there is one (old) DOS program written in BASIC that
not only prints the match results and the reports for the sanctioning
body, but also prints labels via a Dymo Label maker.
I realized today that one thing I have never seen covered in any Python
tutorial is how to format and print things to a physical printer. I did
a little bit of searching and didn't come up with much... either I'm
really not using the right search terms, or physical printing is a black
hole/art...?
TIA,
Monte
I agree with you that it seems surprising that there isn't a simple way
to print with python. Another idea, which you may well think of as
overkill:

Use django or another web framework, and make your application a web
app. With this approach you can display output to a web page, and
create a print stylesheet that can be finely tuned to print.

This ups your work to get involved with a web framework, but it lets you
provide your application to users without the need to install. It also
makes it totally platform agnostic


Joel Goldstick
Monte Milanuk
2010-06-13 19:25:57 UTC
Permalink
Post by Joel Goldstick
Use django or another web framework, and make your application a web
app. With this approach you can display output to a web page, and
create a print stylesheet that can be finely tuned to print.
This ups your work to get involved with a web framework, but it lets you
provide your application to users without the need to install. It also
makes it totally platform agnostic
Actually, this was kind of the way I originally started out (albeit
looking at PHP & MySQL), for exactly those reasons, except one - the
installation. Everything else - the gui would be in a familiar browser
frame of reference, and a lot of the get-this/send-that would be a bit
simpler, plus it would be an extension of what I know (and am still
learning) with html/css.

The installation is the big snafu. This isn't something I can install
on a remote server and just have the users (tournament coordinators and
their data entry helpers) connect to over the internet. 99% of the
time, it will be one person on one computer at a location that is lucky
if they have a 110VAC power outlet nearby. Any kind of external network
access short of a cell modem is pretty much out of the question.
LAN/Wifi access between machines for some parallel data entry would be
nice, but still asking a lot. Expecting the end user (volunteers) to
install/configure Apache server, MySQL server, Python, and Django starts
to sound to be a little far-fetched. I'd had some hope for cherrypy or
web2py since they appear to provide a local http server without needing
all the ancilliary stuff... somehow I didn't get the impression Django
worked that way?
Benjamin Kaplan
2010-06-13 20:06:04 UTC
Permalink
Post by Joel Goldstick
Use django or another web framework, and make your application a web
app. ?With this approach you can display output to a web page, and
create a print stylesheet that can be finely tuned to print.
This ups your work to get involved with a web framework, but it lets you
provide your application to users without the need to install. ?It also
makes it totally platform agnostic
Actually, this was kind of the way I originally started out (albeit looking
at PHP & MySQL), for exactly those reasons, except one - the installation.
?Everything else - the gui would be in a familiar browser frame of
reference, and a lot of the get-this/send-that would be a bit simpler, plus
it would be an extension of what I know (and am still learning) with
html/css.
The installation is the big snafu. ?This isn't something I can install on a
remote server and just have the users (tournament coordinators and their
data entry helpers) connect to over the internet. ?99% of the time, it will
be one person on one computer at a location that is lucky if they have a
110VAC power outlet nearby. ?Any kind of external network access short of a
cell modem is pretty much out of the question. LAN/Wifi access between
machines for some parallel data entry would be nice, but still asking a lot.
?Expecting the end user (volunteers) to install/configure Apache server,
MySQL server, Python, and Django starts to sound to be a little far-fetched.
?I'd had some hope for cherrypy or web2py since they appear to provide a
local http server without needing all the ancilliary stuff... somehow I
didn't get the impression Django worked that way?
Django has its own http server, for debugging purposes. That should
work well enough for this.
Loading...