Python-如何与打印机通信 [英] Python- How to communicate with printer

查看:285
本文介绍了Python-如何与打印机通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找有关如何改进为工作而编写的程序的几天.该程序使用python 3.6,仅将预先格式化的.doc文件发送到打印机,但是我似乎无法找到一种方法来通知信号一旦打印机用尽了该功能就停止发送文件以进行打印纸.打印的文档数量各不相同,所以我不能只使用简单的范围功能.

I've been searching for a few days on how to improve a program I wrote for work. The program, which uses python 3.6, simply sends pre-formatted .doc files to the printer, but I can't seem to find a way on how to signal the function to stop sending the file to print once the printer has run out of paper. The number of documents printed varies, so I can't just use a simple range function.

我研究了win32print模块和pycups模块(无论我尝试什么都不会pip安装).信号模块中是否有某些东西可以捕获来自其他应用程序的外部错误消息?理想情况下,最棒的是,python脚本是否可以在发送文件之前先检查打印队列的状态.

I've looked into the win32print module and the pycups module (which won't pip install not matter what I try). Is there something in the signal module that captures external error messages from other apps? Ideally, what would be great, is if the python script could check the status of the print queue first, before sending the file.

我发现下面的代码使用win32print模块可以完成此操作,但似乎不起作用.

I found that the code below, using the win32print module, that should accomplish this, but it doesn't seem to work.

import win32print

p = win32print.OpenPrinter("Canon Inkjet iP1800 series")

raw = win32print.EnumJobs(p, 0, 999)

def main():
    while len(raw) > 0:
        #does some function

    if #receives error from printer:
        break

main()

win32print.ClosePrinter(p)

推荐答案

旁注:我必须说 pywin32 是基于 Win 的,而 pycups (或任何 cups 包装器)是基于 Ux 的,因此,除非有一些 cups 端口胜利,我不知道,您应该选择上述2个选项之一(最好匹配您所使用的平台/架构).

Side note: I must say that pywin32 is Win based, while pycups (or any cups wrapper) is Ux based so, unless there's some cups port for Win that I'm not aware of, you should pick one of the above 2 (preferably, matching the platform/arch that you're on).

我已经进行了一些研究,并且不必遍历所有打印机作业来检查打印机是否处于错误状态.另外,将作业从0循环到999并不能保证检查所有所有作业,因此上述逻辑(即使它是正确的-并非如此)也不会成立.

I've done some research, and it's not necessary to iterate trough all the printer jobs, in order to check whether the printer is in an erroneous state. Also, looping over jobs from 0 to 999, doesn't guarantee that all jobs are checked, so the logic above (even if it was correct - which is not) would not stand.

这是一个示例实现(我可以随意添加比问题中指定的错误更多的错误:纸张用完(win32print.PRINTER_STATUS_PAPER_OUT)并将其放入 PRINTER_ERROR_STATES(注释掉那些您看不到的错误):

Here's a sample implementation (I took the liberty of adding more errors than specified in the question: running out of paper(win32print.PRINTER_STATUS_PAPER_OUT) and placed them in PRINTER_ERROR_STATES (comment out the ones that you don't see as errors):

import win32print

PRINTER_ERROR_STATES = (
    win32print.PRINTER_STATUS_NO_TONER,
    win32print.PRINTER_STATUS_NOT_AVAILABLE,
    win32print.PRINTER_STATUS_OFFLINE,
    win32print.PRINTER_STATUS_OUT_OF_MEMORY,
    win32print.PRINTER_STATUS_OUTPUT_BIN_FULL,
    win32print.PRINTER_STATUS_PAGE_PUNT,
    win32print.PRINTER_STATUS_PAPER_JAM,
    win32print.PRINTER_STATUS_PAPER_OUT,
    win32print.PRINTER_STATUS_PAPER_PROBLEM,
)


def printer_errorneous_state(printer, error_states=PRINTER_ERROR_STATES):
    prn_opts = win32print.GetPrinter(printer)
    status_opts = prn_opts[18]
    for error_state in error_states:
        if status_opts & error_state:
            return error_state
    return 0


def main():
    printer_name = "Canon Inkjet iP1800 series" # or get_printer_names()[0]
    prn = win32print.OpenPrinter(printer_name)
    error = printer_errorneous_state(prn)
    if error:
        print("ERROR occurred: ", error)
    else:
        print("Printer OK...")
        #  Do the real work

    win32print.ClosePrinter(prn)


if __name__ == "__main__":
    main()

注意:显然, Win 不会存储在打印机上设置的打印机名称.就我而言,我有一台名为 EPSON ****** 的打印机.但是在 Win 中,其名称为 EPSON ******(WF-7610系列).这就是为什么我不得不编写一些额外的代码(此处未包括在内)来枚举所有可用的打印机并获取其名称的原因.

Note: Apparently, Win doesn't store the printer names as set on printers. In my case, I have a printer called EPSON******. However in Win, its name is EPSON****** (WF-7610 Series). That's why I had to write some additional code (that I didn't include here) to enumerate all available printers and get their names.

这篇关于Python-如何与打印机通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆