0

In Python, if I want to open/launch the default browser using shell, I can import os and execute run "www.google.com". Is there any similar way to open the print pictures dialog for a given image.jpg as if right clicking it and selecting print in the context menu that I can use to call this routine from python???

Thank you.

relima
  • 3,462
  • 5
  • 34
  • 53

2 Answers2

3

You can call win32api.ShellExecute with the "print" verb. An example: http://timgolden.me.uk/python/win32_how_do_i/print.html

Neil
  • 54,642
  • 8
  • 60
  • 72
  • Printing is a responsibility of a specific program. Whatever the registry says. Invoking the shell is the right way to have the right program attempt to print the file at hand. – Apalala Mar 10 '11 at 00:06
2

At least on my Windows system (Windows 7), there seems to be no trivial way to achieve this. I found that a naive call to ShellExecute appeared to do nothing. Then I tried this:

import time, win32api
win32api.ShellExecute(0, "print", "test.jpg", None, None, 0)
time.sleep(5)

With the addition of the call to sleep() the dialog appeared, but when the python process terminated, the print dialog closed too.

Since the print dialog runs in-process I don't see an easy wait to wait on it. If it were to run as a separate process then it would be trivial to wait on that process.

I suppose a hacky solution would be to run this through pythonw so that no console appeared, and put in a long sleep. You might end up with some stray Python processes that were aimlessly sleeping, but that might not matter.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490