从 Python 打印到标准打印机?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12723818/
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-08-18 11:42:33  来源:igfitidea点击:

Print to standard printer from Python?

pythonprintingcpython

提问by Prof. Falken contract breached

Is there a reasonably standard and cross platform way to print text (or even PS/PDF) to the system defined printer?

是否有合理的标准和跨平台方式将文本(甚至 PS/PDF)打印到系统定义的打印机?

Assuming CPythonhere, not something clever like using Jython and the Java printing API.

假设这里使用CPython,不像使用 Jython 和Java 打印 API那样聪明。

采纳答案by Anuj Gupta

Unfortunately, there is no standard way to print using Python on all platforms. So you'll need to write your own wrapper function to print.

不幸的是,没有在所有平台上使用 Python 进行打印的标准方法。所以你需要编写自己的包装函数来打印。

You need to detect the OSyour program is running on, then:

您需要检测运行程序的操作系统,然后:

For Linux -

对于 Linux -

import subprocess
lpr =  subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
lpr.stdin.write(your_data_here)

For Windows: http://timgolden.me.uk/python/win32_how_do_i/print.html

对于 Windows:http: //timgolden.me.uk/python/win32_how_do_i/print.html

More resources:

更多资源:

Print PDF document with python's win32print module?

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

How do I print to the OS's default printer in Python 3 (cross platform)?

如何在 Python 3(跨平台)中打印到操作系统的默认打印机?

回答by user1719655

To print to any printer on the network you can send a PJL/PCL print job directly to a network printer on port 9100.

要打印到网络上的任何打印机,您可以将 PJL/PCL 打印作业直接发送到端口 9100 上的网络打印机。

Please have a look at the below link that should give a good start:

请查看以下链接,这应该是一个良好的开端:

http://frank.zinepal.com/printing-directly-to-a-network-printer

http://frank.zinepal.com/printing-directly-to-a-network-printer

Also, If there is a way to call Windows cmd you can use FTP put to print your page on 9100. Below link should give you details, I have used this method for HP printers but I believe it will work for other printers.

另外,如果有办法调用 Windows cmd,您可以使用 FTP put 在 9100 上打印您的页面。下面的链接应该为您提供详细信息,我已将这种方法用于 HP 打印机,但我相信它适用于其他打印机。

http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?objectID=bpj06165

http://h20000.www2.hp.com/bizsupport/TechSupport/Document.jsp?objectID=bpj06165

回答by user1719655

This has only been tested on Windows:

这仅在 Windows 上测试过:

You can do the following:

您可以执行以下操作:

import os

os.startfile("C:/Users/TestFile.txt", "print")

This will start the file, in its default opener, with the verb 'print', which will print to your default printer.Only requires the osmodule which comes with the standard library

这将在其默认打开器中启动文件,并使用动词“print”,它将打印到您的默认打印机。只需要os标准库附带的模块

回答by Admdebian

You can try wx library. It's a cross platform UI library. Here you can find the printing tutorial: https://web.archive.org/web/20160619163747/http://wiki.wxpython.org/Printing

你可以试试 wx 库。它是一个跨平台的 UI 库。在这里你可以找到打印教程:https: //web.archive.org/web/20160619163747/http: //wiki.wxpython.org/Printing

回答by Bobort

I find this to be the superior solution, at least when dealing with web applications. The idea is this: convert the HTML page to a PDF document and send that to a printer via gsprint.

我发现这是最好的解决方案,至少在处理 Web 应用程序时是这样。想法是这样的:将 HTML 页面转换为 PDF 文档,然后通过gsprint.

Even though gsprintis no longer in development, it works really, really well. You can choose the printer and the page orientation and size among several other options.

尽管gsprint不再处于开发阶段,但它确实非常非常好用。您可以在其他几个选项中选择打印机、页面方向和尺寸。

I convert the web page to PDF using Puppeteer, Chrome's headless browser. But you need to pass in the session cookie to maintain credentials.

我使用 Chrome 的无头浏览器 Puppeteer 将网页转换为 PDF。但是您需要传入会话 cookie 以维护凭据。