[Tutor] Windows printing

Tim Golden mail at timgolden.me.uk
Thu Sep 23 10:40:02 CEST 2010


On 23/09/2010 07:30, Rance Hall wrote:
> I'm using this page as a reference:
> http://timgolden.me.uk/python/win32_how_do_i/print.html
>
> I'm able to print to the default printer ok, but I can't seem to find
> out how to get to pick the printer I want to use.
>
> This is from a CLI app, so there is no gui.
>
> win32print seems like it has everything I need, I just can't quite see
> how to put it together
>
> I'd like to have my python app ask which printer to use as opposed to
> just using the default printer.
>
> win32print.EnumPrinters() does give me a list, but its not just a list
> of names. the list also includes other items like network paths
> depending on the level of detail you specify in EnumPrinters.
>
>
> Tim has two possible ways to print.  Tim shows how to use the win32api
> to pass a ShellExecute command that prints.  The catch to this command
> is you have to print a file type and it has to be the default type for
> an app to open.
>
> He also shows a way to skip the ShellExecute and use the print api
> directly to gain some more control.
>
> Based on my needs, I'm pretty sure that I'll need to use win32print,
>
>
> Tim's how-to is likely not for my version of python (mine is 3.1)
> since some of his command fail on my system because mine wants options
> or parameters that Tim doesn't mention.

I've fixed one issue: the win32print example now passes bytes for Py3.x
and str for 2.x; that example now works for me. I haven't worked through
the other examples yet but thanks for the heads-up.

> I'm going to be printing automatically generated check-in tickets.
>
> I can make the ticket, save it as a temp file, but I can not print to
> the default printer, I must be able to select the destination printer.
>
> My plan is to ask the user what printer to use for the session, and
> save that printer name in a variable and direct all automated prints
> to that printer.
>
> As we move the laptop from place to place the default printer is not
> always available.  And the available printer changes depending on
> location.
>
> Ideally there would be a variation of the ShellExecute command that
> would let me specify a printer name.
>
> Windows 7 is the predominate windows platform we are using.

OK. Thanks for the fairly clear explanation of the situation. I agree
that a ShellExecute variant which allowed printer selection would be
good, but I don't believe there is one. That's basically because
ShellExecute is really what the user indirectly actions when they
double-click or right-click on a file and choose one of the actions:
there's no scope for additional info within the ShellExecute mechanism.
Obviously a program called in this way is free to do whatever it wishes
in the way of selection dialogs and the like.

One thing which isn't entirely clear to me is whether your ticket-saved-
as-a-temp-file is printer-ready, eg plain text, PS or PCL, or whether
you'd really want to format it further before printing. On the basis
that it's ready to go direct, the following very slight variation should
do what I think you want: (tested only on XP; I'll try to get hold of
a W7 machine to double-check)

<code>
import os, sys
import win32print

printer_info = win32print.EnumPrinters (
   win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS
)
printer_names = [name for (flags, description, name, comment) in 
printer_info]
for i, name in enumerate (printer_names):
   print ("%d) %s" % (i + 1, name))

n_printer = int (input ("Which printer: "))
printer_name = printer_names[n_printer+1]
print ("Using", printer_name)

hPrinter = win32print.OpenPrinter (printer_name)
#
# ... and so on, per the original example
#

</code>

Is this what you're after? Or have I missed the point?

TJG


More information about the Tutor mailing list