问题
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)
回答1:
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).
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)
Notes:
- What I said in my comment (about iterating over printers) still stands, but I suppose that is beyond the scope of this question
- I've improved the script a little bit:
- Give (more) meaningful names to variables
- Use variables instead of plain numbers to increase code readability
- Other small corrections
- Probably, it could use some exception handling
- The secret of the script consist of:
EnumJobs
returning a tuple of dictionaries (where each dictionary wraps an [MSDN]: JOB_INFO_1 structure - forjob_info_level = 1
), or (obviously) an empty tuple if there are no queued jobs for the printer- How the information from
EnumJobs
is passed toSetJob
:- The
JobID
argument (that you asked about) isjob_info["JobId"]
(check previous bullet) - Also notice the next 2 arguments:
Level
andJobInfo
- The
来源:https://stackoverflow.com/questions/44109985/how-can-i-use-setjob-in-win32print