Programmatically Print a PDF File - Specifying Printer

泪湿孤枕 提交于 2021-02-07 15:57:21

问题


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
)

回答1:


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




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/2878616/programmatically-print-a-pdf-file-specifying-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!