[GitHub]: ActiveState/code - (master) code/recipes/Python/305690_Enumerate_printer_job/recipe-305690.py(在您的情况下引发ctypes.ArgumentError)过于陈旧,其中一些部分仅仅是凭运气运行成功的,而有些甚至从未工作过(因为流程从未到达这些部分)。
我提交了[GitHub]: ActiveState/code - Fixes and updates,解决了主要问题(还有一些问题未解决,但代码可以运行)。
基于此,我尝试为此问题量身定做,并针对其中的某些部分进行了修改。由于该问题被标记为[GitHub]: mhammond/pywin32 - pywin32,因此我使用了它,以便编写更短且更友好(用Python语言)的代码。
code00.py:
import sys
import time
import win32con as wcon
import win32print as wprn
from pprint import pprint as pp
JOB_INFO_RAW_KEYS_DICT = {
"JobId": "Id",
"Position": "Index",
"pPrinterName": "Printer",
"pUserName": "User",
"pDocument": "Document",
"TotalPages": "Pages",
"Submitted": "Created",
}
def generate_constant_strings(header, mod=None):
header_len = len(header)
ret = {}
for k, v in (globals() if mod is None else mod.__dict__).items():
if k.startswith(header):
ret[v] = k[header_len:].capitalize()
return ret
JOBSTATUS_DICT = generate_constant_strings("JOB_STATUS_", mod=wcon)
DMCOLOR_DICT = generate_constant_strings("DMCOLOR_", mod=wcon)
DMPAPER_DICT = generate_constant_strings("DMPAPER_", mod=wcon)
def printer_jobs(name, level=2):
p = wprn.OpenPrinter(wprn.GetDefaultPrinter() if name is None else name, None)
jobs = wprn.EnumJobs(p, 0, -1, level)
wprn.ClosePrinter(p)
return jobs
def job_data(job, raw_keys_dict=JOB_INFO_RAW_KEYS_DICT):
ret = {}
for k, v in job.items():
if k in raw_keys_dict:
ret[raw_keys_dict[k]] = v
ret["Format"] = DMPAPER_DICT.get(job["pDevMode"].PaperSize)
ret["Color"] = DMCOLOR_DICT.get(job["pDevMode"].Color)
ret["Status"] = JOBSTATUS_DICT.get(job["Status"])
return ret
def main(*argv):
printer_name = None
interval = 3
while 1:
try:
jobs = printer_jobs(printer_name)
for job in jobs:
data = job_data(job)
pp(data, indent=2, sort_dicts=0)
time.sleep(interval)
except KeyboardInterrupt:
break
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.")
sys.exit(rc)
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q070103258]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" code00.py
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] 064bit on win32
{ 'Id': 9,
'Printer': 'WF-7610 Series(Network)',
'User': 'cfati',
'Document': '*Untitled - Notepad',
'Index': 1,
'Pages': 1,
'Created': pywintypes.datetime(2021, 12, 3, 22, 52, 22, 923000, tzinfo=TimeZoneInfo('GMT Standard Time', True)),
'Format': 'A4',
'Color': 'Color',
'Status': None}
{ 'Id': 13,
'Printer': 'WF-7610 Series(Network)',
'User': 'cfati',
'Document': 'e:\\Work\\Dev\\StackOverflow\\q070103258\\code00.py',
'Index': 2,
'Pages': 4,
'Created': pywintypes.datetime(2021, 12, 3, 23, 10, 40, 430000, tzinfo=TimeZoneInfo('GMT Standard Time', True)),
'Format': 'A3',
'Color': 'Monochrome',
'Status': None}
Done.
图示:
![Img0](https://istack.dev59.com/PXFNr.webp)
注:
ctypes
库并深入研究相关的Windows SDK。你是否至少尝试过链接中的解决方案并调试它以查看是否有更多信息可用? - Random Davis