Python Win32print作业状态

时间:2018-12-13 16:00:26

标签: python-3.x windows printing

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

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

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。

我试图做的是检查作业完成的时间。

2 个答案:

答案 0 :(得分:1)

您可以从这里开始:

此脚本使您可以查看打印作业队列。如果要查看特定作业的信息,可以使用Get Job documentation自定义它。

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()

脚本取自this post

答案 1 :(得分:0)

返回的状态是位掩码,例如here中所述。可以对多个值进行或运算,因此您的值8208(hex 0x00002010)表示打印机具有(全部)状态。

  • 十六进制0x00002000 = PRINTER_STATUS_WAITING
  • 十六进制0x00000010 = PRINTER_STATUS_PAPER_OUT