简体   繁体   English

使用pywin32更改打印机托盘

[英]Change printer tray with pywin32

I'm trying to change the printer tray using the Python win32print module without any success. 我正在尝试使用Python win32print模块更改打印机托盘而没有任何成功。 The printer supports two 'bins': 7=Auto and 4=Manual. 打印机支持两个“箱”:7 =自动,4 =手动。 I want to start a print job from the 'manual' bin. 我想从“手动”垃圾箱开始打印作业。 Here's some code: 这是一些代码:

import win32print
import win32gui

# Constants from wingdi.h
DM_OUT_BUFFER = 0x02
DM_IN_BUFFER = 0x08
DM_IN_PROMPT = 0x04
DM_DEFAULT_SOURCE = 0x200

# Get a handle for the default printer
device_name = win32print.GetDefaultPrinter()
handle = win32print.OpenPrinter(device_name)

# Get the default properties for the printer
properties = win32print.GetPrinter(handle, 2)
devmode = properties['pDevMode']

# Print the default paper source (prints '7' for 'Automatically select')
print(devmode.DefaultSource)

# Change the default paper source to '4' for 'Manual feed'
devmode.DefaultSource = 4
devmode.Fields = devmode.Fields | DM_DEFAULT_SOURCE

# Write these changes back to the printer
win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)

# Confirm the changes were updated
print(devmode.DefaultSource)  # Aaargh! Prints '7' again!

# Start printing with the device
hdc = win32gui.CreateDC('', device_name, devmode)
win32print.StartDoc(hdc, ('Test', None, None, 0))
win32print.StartPage(hdc)

# ... GDI drawing commands ...

win32print.EndPage(hdc)
win32print.EndDoc(hdc)

Obviously either the PyDEVMODE structure was not updated, or somehow the driver rejected my changes. 显然,PyDEVMODE结构没有更新,或者某种程度上驱动程序拒绝了我的更改。

If the following line: 如果以下行:

win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)

Is changed to: 改为:

win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_PROMPT | DM_IN_BUFFER | DM_OUT_BUFFER)

Then a 'Print' dialog is shown and I can change the paper source from there. 然后显示“打印”对话框,我可以从那里更改纸张来源。 These changes are then correctly copied out to the devmode structure and printing works as expected from the manual feed tray. 然后将这些更改正确地复制到devmode结构,并按照预期从手动进纸盘进行打印。

So I think my problem is that changes to the PyDEVMODE structure are not re-marshalled and so are lost when the structure is re-submitted to DocumentProperties. 所以我认为我的问题是对PyDEVMODE结构的更改不会被重新编组,因此在重新提交给DocumentProperties时会丢失。 Any ideas? 有任何想法吗? Many thanks. 非常感谢。

There was a bug in some older versions of Pywin32 that could cause that behaviour. 某些旧版本的Pywin32中存在一个可能导致该行为的错误。 Try installing the most recent build (217). 尝试安装最新版本(217)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM