以编程方式打印PDF文件-指定打印机

5
我有一个需求,需要在Python脚本中打印现有的PDF文件。
我需要能够在脚本中指定打印机,它运行在Windows XP上。
你有什么想法吗?
这种方法看起来很不错,但是我无法指定打印机:这个方法
win32api.ShellExecute (
  0,
  "print",
  filename,
  None,
  ".",
  0
)

我认为你可以在这篇类似的帖子中找到一个合适的答案。 - bluish
3个回答

3

有一个文档不全的printto动词,它以打印机名称作为参数(如果包含空格,则用引号括起来)

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
)

以下是Ja8zyjits链接中的代码片段:


1

请参考此链接以获取更多详细信息

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
)

这应该有效,请参考提供的链接以获取更多详细信息。


我曾多次尝试使用print动词和/d:参数来指定不同的打印机,但从未成功。对我有效的是使用printto动词,并将打印机名称作为参数,正如应被接受的答案中所提到的那样。 - Bobort

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接