本文介绍了如何在 win32print 中使用 SetJob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Python 清除或删除打印作业.但是我怎样才能获得JobID?

I want to clear or delete print jobs using Python.But how can I get JobID?

win32print.SetJob(hPrinter, JobID, Level, JobInfo, Command)

我怎样才能运行这段代码?

How could I run this code?

jobs = []
for p in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL,None, 1):
    flags, desc, name, comment = p

    pHandle = win32print.OpenPrinter(name)
    print = list(win32print.EnumJobs(pHandle, 0, -1, 1))
    jobs.extend(print)
    SetJob(pHandle, id, 1,JOB_CONTROL_DELETE)
    #where should i get id from?
    win32print.ClosePrinter(pHandle)

推荐答案

从您的代码开始,我设法创建了一个小脚本,该脚本可以删除任何(本地)打印机上的任何打印作业(我已经对其进行了测试,并且有效).

Starting from your code, I've managed to create a small script that deletes any print job on any (local) printer (I've tested it and it works).

在这里(我用 Py35 运行它):

Here it is (I've run it with Py35):

import win32print

if __name__ == "__main__":

    printer_info_level = 1

    for printer_info in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL, None, printer_info_level):
        name = printer_info[2]
        #print(printer_info)
        printer_handle = win32print.OpenPrinter(name)
        job_info_level = 1
        job_info_tuple = win32print.EnumJobs(printer_handle, 0, -1, job_info_level)
        #print(type(job_info_tuple), len(job_info_tuple))
        for job_info in job_info_tuple:
            #print("\t", type(job_info), job_info, dir(job_info))
            win32print.SetJob(printer_handle, job_info["JobId"], job_info_level, job_info, win32print.JOB_CONTROL_DELETE)
        win32print.ClosePrinter(printer_handle)

注意事项:

  • 我在评论中所说的(关于迭代打印机)仍然有效,但我认为这超出了这个问题的范围
  • 我稍微改进了脚本:
    • 为变量赋予(更多)有意义的名称
    • 使用变量代替普通数字来提高代码可读性
    • 其他小的更正
    • 可能,它可以使用一些异常处理
    • EnumJobs 返回 字典元组(其中每个字典都包含一个 [MSDN]:JOB_INFO_1 结构 - 用于 job_info_level = 1),或者(显然)一个空元组,如果打印机没有排队的作业
    • 如何将来自 EnumJobs 的信息传递给 SetJob:
      • JobID 参数(您询问的)是 job_info["JobId"](检查上一个项目符号)
      • 还要注意接下来的 2 个参数:LevelJobInfo
      • EnumJobs returning a tuple of dictionaries (where each dictionary wraps an [MSDN]: JOB_INFO_1 structure - for job_info_level = 1), or (obviously) an empty tuple if there are no queued jobs for the printer
      • How the information from EnumJobs is passed to SetJob:
        • The JobID argument (that you asked about) is job_info["JobId"] (check previous bullet)
        • Also notice the next 2 arguments: Level and JobInfo

        这篇关于如何在 win32print 中使用 SetJob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 00:26