Python Win32print 作业状态 [英] Python Win32print Job Status

查看:184
本文介绍了Python Win32print 作业状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Python 中的 win32print 获取打印作业的状态.

I have been trying to get the status of a print job using win32print in Python.

win32print 提供的状态代码似乎与相关作业的状态代码不匹配.我在不同的打印机上尝试过,但总是得到相同的结果.

The status codes provided by win32print don't seem to match the status code for the job in question. I have tried it on different printers but always get the same results.

import win32print
import subprocess

printer_name = win32print.GetDefaultPrinter()

print("Printer: %s" % (printer_name))

hPrinter = win32print.OpenPrinter(printer_name)

try:
    hJob = win32print.StartDocPrinter(hPrinter, 1, ("test of raw data", None, "RAW"))
    try:
        f = open("test2.ps", "r")
        win32print.StartPagePrinter(hPrinter)
        win32print.WritePrinter(hPrinter, bytes(f.read(), "utf-8"))
        win32print.EndPagePrinter(hPrinter)
    finally:
        win32print.EndDocPrinter(hPrinter)
finally:
    print("Printing: %s" % (hJob))
    win32print.ClosePrinter(hPrinter)

hPrinter = win32print.OpenPrinter(printer_name)
past_status_code = -0
while True:
    try:
        job = win32print.GetJob(hPrinter, hJob, win32print.JOB_INFO_1)
    except:
        break
    status_code = job['Status']

    if (status_code != past_status_code):
        print("Job Status Code: %s" % (status_code))

    past_status_code = status_code

    if (job['Status'] == win32print.JOB_STATUS_BLOCKED_DEVQ):
        print("BLOCKED_DEVQ")

    if (job['Status'] == win32print.JOB_STATUS_DELETED):
        print("Deleted")

    if (job['Status'] == win32print.JOB_STATUS_DELETING):
        print("Deleting")

    if (job['Status'] == win32print.JOB_STATUS_ERROR):
        print("Error")

    if (job['Status'] == win32print.JOB_STATUS_OFFLINE):
        print("Offline")

    if (job['Status'] == win32print.JOB_STATUS_PAPEROUT):
        print("PaperOut")

    if (job['Status'] == win32print.JOB_STATUS_PAUSED):
        print("Paused")

    if (job['Status'] == win32print.JOB_STATUS_RESTART):
        print("Restart")

    if (job['Status'] == win32print.JOB_STATUS_USER_INTERVENTION):
        print("User intervention")

    if (job['Status'] == win32print.JOB_STATUS_SPOOLING):
        print("Spooling")

    if (job['Status'] == win32print.JOB_STATUS_PRINTING):
        print("Printing")

    if (job['Status'] == win32print.JOB_STATUS_PRINTED):
        print("Printed")

    if (job['Status'] == win32print.JOB_STATUS_COMPLETE):
        print("Complete")

运行上面的脚本后,我总是得到相同的结果/状态代码,我似乎得到 8208,然后是 148.

After running the script above I always get the same results/ status codes I seem to get 8208 and then a 148.

我试图做的是检查作业何时完成.

What I'm tried to do is check when a job has completed.

推荐答案

您可以从这里开始:

此脚本可让您查看打印作业队列.如果您想查看信息,可以使用 获取工作文档 对其进行自定义特定工作.

This script allows you to see your printing job queue. You can customize it using the Get Job documentation if you want to see the information of a specific Job.

import time
import win32print

#----------------------------------------------------------------------
def print_job_checker():
    """
    Prints out all jobs in the print queue every 5 seconds
    """
    jobs = [1]
    while jobs:
        jobs = []
        for p in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL,
                                         None, 1):
            flags, desc, name, comment = p

            phandle = win32print.OpenPrinter(name)
            print_jobs = win32print.EnumJobs(phandle, 0, -1, 1)
            if print_jobs:
                jobs.extend(list(print_jobs))
            for job in print_jobs:
                print "printer name => " + name
                document = job["pDocument"]
                print "Document name => " + document
            win32print.ClosePrinter(phandle)

        time.sleep(5)
    print "No more jobs!"

#----------------------------------------------------------------------
if __name__ == "__main__":
    print_job_checker()

脚本取自这篇文章

这篇关于Python Win32print 作业状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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