[python-win32] A little help for a n00b, please

Alan Jump alan.jump at gmail.com
Sun May 6 19:07:14 CEST 2012

(Originally sent just to Tim...serves me right for trying to think
before I've had my second cuppa.)

Much obliged to both you and Vernon. I'm going to be adding quite a few
code snippets to these emails, just to let the various search engines
work their magic in case anyone besides me is running into the same problem.

I tried a couple of the extended commands on the web page, and still got
the same exception message. The altered section is as follows:

        if platform.system() == 'Windows':
                import win32api
                import win32print # new import to allow for direction to
default printer
                print('* FAILED TO LOAD WINDOWS EXTENSION')
                win32api.ShellExecute (0, "printto", filename,
                                       '"%s"' %
win32print.GetDefaultPrinter (),
                                       ".", 0) # altered ShellExecute()
                print('* FAILED TO SEND PRINT TO WINDOWS PRINTER')

I ran a code evaluation in my IDE (Wing IDE, for those keeping score).
The error message:

    pywintypes.error: (31, 'ShellExecute', 'A device attached to the
system is not functioning.')

I double-checked that the interpreter was seeing the printer:

>>> win32print.GetDefaultPrinter()
'Canon iP4800 series'

Taking Vernon's hint, I was also not able to print a file using the
command-line "print" statement. So although the
win32print.GetDefaultPrinter() function is seeing the default printer
correctly, and the printer operates correctly in any actual WinXX
program, it's not being recognized outside WinXX. The problem with using
the "print" command, though, is that it wants to see either the default
PRN device, or a specific output port (USB7:, LPT0:, etc.)

The next troubleshooting step is to see if there are driver updates,
since it's not printing from the command line.

Thanks again.

73 de N5ILN/6

On 5/6/2012 1:50 AM, Tim Golden wrote:
> On 06/05/2012 02:24, Alan Jump wrote:
>> Trying to massage an older package (not written by me) into functioning
>> order. The issue I'm having is getting printing functionality to work.
>> The code snippet in question:
>>          if platform.system() == 'Windows':
>>              try:
>>                  import win32api
>>              except:
>>                  print('* FAILED TO LOAD WINDOWS EXTENSION')
>>                  return
>>              try:
>>                  win32api.ShellExecute (0, "print", filename, None,
>> ".", 0)
>>              except:
>>                  print('* FAILED TO SEND PRINT TO WINDOWS PRINTER')
>>                  return
>> The output file is generated in a separate code section and the filename
>> being passed has been verified, but I invariably get the message in the
>> second exception. That tells me the win32api is being properly imported,
>> but for some reason, it's throwing an exception when trying to write to
>> the print device.
> Some general advice for a start: you almost *never* want to
> use a general except: clause (ie without a specific exception
> class): it catches *everything* - including Ctrl-C. So, in
> the try-except around "import win32api" you just want to
> catch ImportErrors -- at least, I assume so.
> However, you've circumvented that particular issue by removing
> the except clause from your ShellExecute command. Contrary to
> Vernon's suggestion, that won't actually be using the DOS
> "print" command but rather passing the "print" verb along to
> the application which is registered against the filename in
> question via the Shell verb handling mechanism.
> It would be interesting to see the exact error message you
> get but permissions or UAC is definitely a possibility. Also,
> and this is where it gets messier, the "print" verb will, in
> most cases, go to the *default* printer on the machine you're
> on -- and with the default settings. It's possible that this
> isn't the printer you think it is. Some applications keep their
> own track of which printer you last used, so you might fail to
> realise which one is actually the default.
> If that is the case you might want to look at this:
>   http://timgolden.me.uk/python/win32_how_do_i/print.html#shellexecute
> for a possible way to specify a printer.
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32

More information about the python-win32 mailing list