如何使用 python & 更改打印队列中作业的用户名win32打印
我正在尝试更改队列中打印作业的用户,因为我想在服务帐户上创建它,但将该作业发送到另一个用户跟随我的打印队列。我在 python 中使用 win32 模块。这是我的代码示例:
from win32 import win32print
JOB_INFO_LEVEL = 2
pclExample = open("sample.pcl")
printer_name = win32print.GetDefaultPrinter()
hPrinter = win32print.OpenPrinter(printer_name)
try:
jobID = win32print.StartDocPrinter(hPrinter, 1, ("PCL Data test", None, "RAW"))
# Here we try to change the user by extracting the job and then setting it again
jobInfoDict = win32print.GetJob(hPrinter, jobID , JOB_INFO_LEVEL )
jobInfoDict["pUserName"] = "exampleUser"
win32print.SetJob(hPrinter, jobID , JOB_INFO_LEVEL , jobInfoDict , win32print.JOB_CONTROL_RESUME )
try:
win32print.StartPagePrinter(hPrinter)
win32print.WritePrinter(hPrinter, pclExample)
win32print.EndPagePrinter(hPrinter)
finally:
win32print.EndDocPrinter(hPrinter)
finally:
win32print.ClosePrinter(hPrinter)
问题是我在 win32print.SetJob() 行收到错误。如果 JOB_INFO_LEVEL 设置为 1,则会出现以下错误:
(1804, 'SetJob', 'The specified datatype is invalid.')
这是一个与 C++ 在后台工作方式有关的已知错误 (问题在这里)。
如果 JOB_INFO_LEVEL 设置为 2,则会出现以下错误:
(1798, 'SetJob', 'The print processor is unknown.')
但是,这是来自 win32print.GetJob() 的处理器。如果不尝试更改用户,则打印效果很好,所以我不确定出了什么问题。
任何帮助将不胜感激! :)
编辑:
使用Python 3.8.5和Pywin32 303
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一开始我以为这是一个误会(我对bug报告也有点怀疑),主要是因为下面一段(显然似乎是错误的)来自[MS.Docs]:SetJob 函数 (强调是我的):
但我做了一些测试并遇到了问题。问题如错误中所述:用 char* 数据填充 JOB_INFO_* 字符串成员(即 LPTSTR)。
已提交 [GitHub]:mhammond/pywin32 - 修复:win32print.SetJob 将 ANSI 发送到 UNICODE API (并且两个错误都没有弹出)。它已于 220331 合并到 main。
在测试修复时,我能够更改现有作业的各种属性,令我惊讶的是它不必是有效数据(如下所示),我有点好奇看到执行作业时会发生什么(因为现在我没有连接到打印机):
将 pUserName 更改为
str(random.randint(0, 10000) )
以确保它在每次脚本运行时都会发生变化(PrintScreen单独拍摄并在Paint中组装):进一步的方法:
等待新的 PyWin32 版本(包含此修复)待发布。 这是推荐的方法,但也需要更多时间(而且还不清楚什么时候会发生)
获取源代码:
来自 main
来自b303(最后一个稳定分支),并应用(上面的)补丁(1)
构建模块 (.pyd) 并将其复制到 PythonPyWin32 的 site-packages 目录中< /em> 安装。更快,但需要一些更深层次的知识,维护可能会成为一场噩梦
脚注
At the beginning I thought it was a misunderstanding (I was also a bit skeptical about the bug report), mainly because of the following paragraph (which apparently seems to be wrong) from [MS.Docs]: SetJob function (emphasis is mine):
But I did some tests and ran into the problem. The problem is as described in the bug: filling JOB_INFO_* string members (which are LPTSTRs) with char* data.
Submitted [GitHub]: mhammond/pywin32 - Fix: win32print.SetJob sending ANSI to UNICODE API (and none of the 2 errors pops up). It was merged to main on 220331.
When testing the fix, I was able to change various properties of an existing job, I was amazed that it didn't have to be valid data (like below), I'm a bit curious to see what would happen when the job would be executed (as now I don't have a connection to a printer):
Change pUserName to
str(random.randint(0, 10000))
to make sure it changes on each script run (PrintScreens taken separately and assembled in Paint):Ways to go further:
Wait for a new PyWin32 version (containing this fix) to be released. This is the recommended approach, but it will also take more time (and it's unclear when it will happen)
Get the sources, either:
from main
from b303 (last stable branch), and apply the (above) patch(1)
build the module (.pyd) and copy it in the PyWin32's site-packages directory on your Python installation(s). Faster, but it requires some deeper knowledge, and maintenance might become a nightmare
Footnotes