【问题标题】:Programmatically Print a PDF File - Specifying Printer以编程方式打印 PDF 文件 - 指定打印机
【发布时间】:2011-02-22 03:29:09
【问题描述】:

我需要从 Python 脚本打印现有的 PDF 文件。

我需要能够在脚本中指定打印机。它在 Windows XP 上运行。

有什么我可以做的想法吗?

This method 看起来可以,只是我无法指定打印机:

win32api.ShellExecute (
  0,
  "print",
  filename,
  None,
  ".",
  0
)

【问题讨论】:

标签: python pdf printing


【解决方案1】:

有一个未充分记录的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
)

来自Ja8zyjitslink的sn-p

【讨论】:

    【解决方案2】:

    看起来 blish 对此发表了评论,但没有留下答案。

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

    然后使用问题中的命令

    Print PDF document with python's win32print module?

    【讨论】:

      【解决方案3】:

      请参考link了解更多详情

      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
      )
      

      这应该可行,请参阅提供的link 以获取更多详细信息。

      【讨论】:

      • 我曾多次尝试使用带有/d: 参数的print 动词来指定不同的打印机,但我从未让它工作。使用带有打印机名称的printto 动词作为参数,正如应该接受的答案中提到的那样对我有用。
      猜你喜欢
      • 2010-09-14
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多