如何使用 python & 更改打印队列中作业的用户名win32打印

发布于 2025-01-16 21:44:01 字数 1515 浏览 3 评论 0 原文

我正在尝试更改队列中打印作业的用户,因为我想在服务帐户上创建它,但将该作业发送到另一个用户跟随我的打印队列。我在 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

I am trying to change the user of a print job in the queue, as I want to create it on a service account but send the job to another users follow-me printing queue. I'm using the win32 module in python. Here is an example of my code:

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)
    

The problem is I get an error at the win32print.SetJob() line. If JOB_INFO_LEVEL is set to 1, then I get the following error:

(1804, 'SetJob', 'The specified datatype is invalid.')

This is a known bug to do with how the C++ works in the background (Issue here).

If JOB_INFO_LEVEL is set to 2, then I get the following error:

(1798, 'SetJob', 'The print processor is unknown.')

However, this is the processor that came from win32print.GetJob(). Without trying to change the user this prints fine, so I'm not sure what is wrong.

Any help would be hugely appreciated! :)

EDIT:

Using Python 3.8.5 and Pywin32 303

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2025-01-23 21:44:01

一开始我以为这是一个误会(我对bug报告也有点怀疑),主要是因为下面一段(显然似乎是错误的)来自[MS.Docs]:SetJob 函数强调是我的):

JOB_INFO_1, JOB_INFO_2,或JOB_INFO_4< /a> 结构在调用SetJob时被忽略JobIdpPrinterNamepMachineNamepUserNamepDrivername大小已提交< /strong>、时间总页数


但我做了一些测试并遇到了问题。问题如错误中所述:用 char* 数据填充 JOB_INFO_* 字符串成员(即 LPTSTR)。

已提交 [GitHub]:mhammond/pywin32 - 修复:win32print.SetJob 将 ANSI 发送到 UNICODE API (并且两个错误都没有弹出)。它已于 220331 合并到 main

在测试修复时,我能够更改现有作业的各种属性,令我惊讶的是它不必是有效数据(如下所示),我有点好奇看到执行作业时会发生什么(因为现在我没有连接到打印机):

  • pUserName 更改为 str(random.randint(0, 10000) ) 以确保它在每次脚本运行时都会发生变化(PrintScreen单独拍摄并在Paint中组装):

    “img0”

进一步的方法:

  1. 等待新的 PyWin32 版本(包含此修复)待发布。 这是推荐的方法,但也需要更多时间(而且还不清楚什么时候会发生)

  2. 获取源代码:

  • 来自 main

  • 来自b303(最后一个稳定分支),并应用(上面的)补丁(1)

    构建模块 (.pyd) 并将其复制到 PythonPyWin32site-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):

The following members of a JOB_INFO_1, JOB_INFO_2, or JOB_INFO_4 structure are ignored on a call to SetJob: JobId, pPrinterName, pMachineName, pUserName, pDrivername, Size, Submitted, Time, and TotalPages.

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):

    img0

Ways to go further:

  1. 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)

  2. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文