windows 用python的win32print模块打印PDF文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1462842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:10:55  来源:igfitidea点击:

Print PDF document with python's win32print module?

pythonwindowspdfwinapipostscript

提问by slypete

I'm trying to print a PDF document with the win32print module. Apparently this module can only accept PCL or raw text. Is that correct?

我正在尝试使用 win32print 模块打印 PDF 文档。显然这个模块只能接受 PCL 或原始文本。那是对的吗?

If so, is there a module available to convert a PDF document into PCL?

如果是这样,是否有可用于将 PDF 文档转换为 PCL 的模块?

I contemplated using ShellExecute; however, this is not an option since it only allows printing to the default printer. I need to print to a variety of printers on servers across various networks.

我考虑使用 ShellExecute;但是,这不是一个选项,因为它只允许打印到默认打印机。我需要打印到跨各种网络的服务器上的各种打印机。

Thanks for your help, Pete

谢谢你的帮助,皮特

回答by slypete

I ended up using Ghostscriptto accomplish this task. There is a command line tool that relies on Ghostscript called gsprint.

我最终使用Ghostscript来完成这项任务。有一个命令行工具依赖于名为gsprint 的Ghostscript 。

You don't even need Acrobat installed to print PDFs in this fashion which is quite nice.

您甚至不需要安装 Acrobat 来以这种方式打印 PDF,这非常好。

Here is an example:

下面是一个例子:

on the command line:

在命令行上:

gsprint -printer \server\printer "test.pdf"

from python:

来自蟒蛇:

win32api.ShellExecute(0, 'open', 'gsprint.exe', '-printer "\\' + self.server + '\' + self.printer_name + '" ' + file, '.', 0)

Note that I've added to my PATH variable in these examples, so I don't have to include the entire path when calling the executable.

请注意,在这些示例中,我已将 PATH 变量添加到我的 PATH 变量中,因此在调用可执行文件时不必包含整个路径。

There is one downside, however. The code is licensed under the GPL, so it's no very useful in commercial software.

然而,有一个缺点。该代码是根据 GPL 许可的,因此它在商业软件中不是很有用。

Hope this helps someone, Pete

希望这对某人有所帮助,皮特

回答by Craig Smith

I was already using the win32api.ShellExecute approach and needed to print to a non-default printer. The best way I could work out was to temporarily change the default printer. So right before I do the print I store what the current default printer is, change it, and then set it back after printing. Something like:

我已经在使用 win32api.ShellExecute 方法并且需要打印到非默认打印机。我能解决的最好方法是暂时更改默认打印机。因此,在我进行打印之前,我会存储当前默认打印机的内容,更改它,然后在打印后将其设置回来。就像是:

tempprinter = "\\server01\printer01"
currentprinter = win32print.GetDefaultPrinter()

win32print.SetDefaultPrinter(tempprinter)
win32api.ShellExecute(0, "print", filename, None,  ".",  0)
win32print.SetDefaultPrinter(currentprinter)

I'm not going to claim it's pretty, but it worked and it allowed me to leave my other code untouched.

我不会声称它很漂亮,但它确实有效,而且它允许我保留其他代码不变。

回答by John Paulett

I am not sure how to specifically get win32print to work, but there might be a couple of other options. Reportlabis often mentioned when creating PDFs from Python. If you are already invested in your approach, maybe using PyXor pypsgto generate the Postscript files and then feeding that into win32print would work.

我不确定如何专门让 win32print 工作,但可能还有其他几个选项。 从 Python 创建 PDF 时经常提到Reportlab。如果您已经对自己的方法进行了投资,也许使用PyXpypsg来生成 Postscript 文件,然后将其输入到 win32print 中即可。