尝试使用纸杯打印到打印机时出现“禁止”错误

2024-09-29 19:22:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我更新了这个问题,现在我知道(多亏了Brody CritchLow),Gtk的打印机产品已经停产了。我发现了一个名为cups的模块,我正试图使用它来打印,但在尝试设置作业时遇到了一个错误。这是我的密码:

import cups
import cupshelpers

connection = cups.Connection()
printers = cupshelpers.getPrinters(connection)
for printer in printers:
    # print(cupshelpers.Printer("asdf", "asdf").setJobSheets("Hello!", "Boodoaefrw"))
    print(printer)
    print(printers[printer].setJobSheets("Hello!", "Boodoaefrw"))
    printers[printer].setAccepting(True)

错误是:

Traceback (most recent call last):
  File "/home/sam/programs/python/testing/problem.py", line 9, in <module>
    print(printers[printer].setJobSheets("Hello!", "Boodoaefrw"))
  File "/usr/lib/python3/dist-packages/cupshelpers/cupshelpers.py", line 313, in setJobSheets
    self.connection.setPrinterJobSheets(self.name, start, end)
cups.IPPError: (1025, 'Forbidden')

有人能告诉我我做错了什么吗?如果有帮助的话,我正在Ubuntu 20.04上使用Python 3.8


Tags: inpyimporthello错误connectionprinterfile
2条回答

我知道了!使用cups模块,您可以获得可用打印机的列表,选择一个,然后将文件打印到其中!以下是一个例子:

import cups
import cupshelpers

# Create a new connection and the list of available printers
connection = cups.Connection()
printers = cupshelpers.getPrinters(connection)

# Select the first printer in the list...
for p in printers:
    if p is not None:
        printer = printers[p]
        break

# ...and print to it
printer.connection.printFile(
    p,
    "printer_test.txt",
    "Test this printer",
    {}
)

我假设你也可以打印PDF和其他东西,但是文本文件对我来说很有用。如果我在printer.connection.printFile()函数中找到有关options字典的任何信息,我将更新此答案

注意:不保证这在Windows上有效。如果你有一个窗口,你可能想看看Brody Critchlow的答案

你不能用win32print吗

你要做的就是

import os, sys import win32print

p = win32print.OpenPrinter(printer_name)
job = win32print.StartDocPrinter(p, 1, ("test of raw data", None, "RAW")) 

win32print.StartPagePrinter(p) 
win32print.WritePrinter(p, "data to print") 
win32print.EndPagePrinter(p)

虽然如果您需要使用GTK,我认为所有的打印选项都已停止

推理:
enter image description here

编辑:

GTK文档上唯一有效的打印操作是https://athenajc.gitbooks.io/python-gtk-3-api/content/gtk-group/gtkprintsettings.html

相关问题 更多 >

    热门问题