5

I have a requirement to print an existing PDF file from a Python script.

I need to be able to specify the printer in the script. It's running on Windows XP.

Any ideas what I could do?

This method looks like it would work except that I cannot specify the printer:

win32api.ShellExecute (
  0,
  "print",
  filename,
  None,
  ".",
  0
)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Greg
  • 45,306
  • 89
  • 231
  • 297
  • I think you can find a suitable answer in [this similar post](http://stackoverflow.com/questions/1462842/print-pdf-document-with-pythons-win32print-module) – bluish Dec 16 '10 at 16:45

3 Answers3

3

There's an underdocumented printto verb which takes the printer name as a parameter (enclosed in quotes if it contains spaces).

import tempfile
import win32api
import win32print

filename = tempfile.mktemp (".txt")
open (filename, "w").write ("This is a test")
win32api.ShellExecute (
  0,
  "printto",
  filename,
  '"%s"' % win32print.GetDefaultPrinter (),
  ".",
  0
)

snippet from Ja8zyjits's link

Community
  • 1
  • 1
alexandre-rousseau
  • 2,321
  • 26
  • 33
1

Looks like bluish left a comment about this but did not leave an answer.

Install Ghostprint http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm

Then use the command in the question

Print PDF document with python's win32print module?

Community
  • 1
  • 1
Bachmann
  • 748
  • 6
  • 12
1

please refer this link for further details

import tempfile
import win32api
import win32print

filename = tempfile.mktemp (".txt")
open (filename, "w").write ("This is a test")
win32api.ShellExecute (
  0,
  "print",
  filename,
  #
  # If this is None, the default printer will
  # be used anyway.
  #
  '/d:"%s"' % win32print.GetDefaultPrinter (),
  ".",
  0
)

This shall work please refer the link provided for further details.

Ja8zyjits
  • 1,433
  • 16
  • 31
  • I have tried on numerous occasions to use the `print` verb with the `/d:` parameter to specify a different printer, but I have never gotten it to work. Using the `printto` verb with the printer name as the parameter as mentioned in what should be the accepted answer is what worked for me. – Bobort Mar 30 '21 at 14:22