Mal*_*imi 8 python windows printing winapi pywin32
在假脱机期间最初在本地计算机上请求打印作业时,我已成功设法触发回调.但是,无论如何有win32print
或类似的东西可以让我处理将打印作业传输到打印服务器或USB打印机的事件?
################################################################################
# Imports ######################################################################
################################################################################
from os.path import *
from printer import *
from watcher import *
from statvar import *
################################################################################
# Event Callback ###############################################################
################################################################################
def callback(code, event):
num = splitext(event)[0]
ext = splitext(event)[1]
if code == 1 and ext == '.SPL':
main(num.lstrip('0'))
################################################################################
# wx Event Handler #############################################################
################################################################################
def handling(*args):
wx.CallAfter(callback, *args)
################################################################################
# Create Listener ##############################################################
################################################################################
# listens to the spool directory for files
watch = Watcher(SPOOL_DIRECTORY, handling)
# set the appropriate flags for a listener
watch.flags = FILE_NOTIFY_CHANGE_FILE_NAME
################################################################################
# Start Listener ###############################################################
################################################################################
watch.start()
################################################################################
# Start wx App #################################################################
################################################################################
app = wx.App()
wx.Frame(None)
app.MainLoop()
################################################################################
################################################################################
################################################################################
Run Code Online (Sandbox Code Playgroud)
这是一个在我的计算机(Windows 8)上有效的想法。这不是完全成熟的代码,但它可能会让您继续前进。您需要利用这些功能FindFirstPrinterChangeNotification
,FindNextPrinterChangeNotification
这些功能包含在winspool.drv
客户端(令人恼火的是,您可以发现它们被记录为在其中spoolSS.dll
,但这是服务器端 -该图可以澄清)。
可以从MSDN 获取可以侦听的事件列表(以及重要的是它们的标志设置)。最初我以为你想要PRINTER_CHANGE_ADD_JOB
(0x00000100
),但我认为你实际上可能想要PRINTER_CHANGE_WRITE_JOB
(0x00000800
)。这不会在作业开始假脱机时立即触发,但不幸的是,在将一个文档发送到网络打印机的示例中,它似乎确实被多次触发。
不幸的是,这些 API 并未在win32print
库中公开。我认为,因此你必须深入研究ctypes
。在这里,我没有注册这样的回调,而是监听通知,当触发时,我调用该函数并开始在无限循环中再次监听。监听过程中进程停止。如果您需要传统的回调功能,您可以在其自己的线程中运行此脚本,或者这个答案可能不适合您的需求。
注意 - 这只是侦听正在请求的打印作业,然后调用一个函数。如果你想提取有关正在触发的作业的信息,代码将变得可怕。进一步注意 - 它将触发打印作业开始并随后取消,但我想这很好。
from ctypes import *
from ctypes.wintypes import HANDLE, LPSTR
def add_job_callback():
print('A job has just been sent to the printer this script is monitoring')
spl = windll.LoadLibrary('winspool.drv')
printer_name = 'KONICA MINOLTA PS Color Laser Class Driver'
# Put the name of your printer here - can be networked or any installed on your computer. Alternatively, set it to None to use the local printer server
#printer_name = None
hPrinter = HANDLE()
if printer_name:
spl.OpenPrinterA(c_char_p(printer_name), byref(hPrinter),None)
else:
spl.OpenPrinterA(None, byref(hPrinter),None)
print(hPrinter)
hjob = spl.FindFirstPrinterChangeNotification(hPrinter,0x00000100,0, None)
# 0x00000100 is a flags setting to set watch for only PRINTER_CHANGE_ADD_JOB
while True:
windll.kernel32.WaitForSingleObject(hjob,-1)
#When this function returns, the change that you're monitoring for has been observed, trigger the function
add_job_callback()
spl.FindNextPrinterChangeNotification(hjob, None, None, None)
Run Code Online (Sandbox Code Playgroud)
c_char_p
ctype
请注意,Python 2.7 和 Python 3 之间存在一些细微差别 - 例如,从字符串初始化。我在这里提供了最简单的版本 - 它适用于 2.7。
后记
我做了所有繁重的工作,然后找到了这个答案,这是重复的。它有更好的代码来处理 unicode 打印机名称等,但只查看默认的本地打印服务器。