我正在尝试使用Python win32print模块更改打印机托盘而没有任何成功。打印机支持两个“箱”:7 =自动,4 =手动。我想从“手动”垃圾箱开始打印作业。这是一些代码:
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)
显然,PyDEVMODE结构没有更新,或者某种程度上驱动程序拒绝了我的更改。
如果以下行:
win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_BUFFER | DM_OUT_BUFFER)
更改为:
win32print.DocumentProperties(None, handle, device_name, devmode, devmode, DM_IN_PROMPT | DM_IN_BUFFER | DM_OUT_BUFFER)
然后显示“打印”对话框,我可以从那里更改纸张来源。然后将这些更改正确地复制到devmode结构,并按照预期从手动进纸盘进行打印。
所以我认为我的问题是对PyDEVMODE结构的更改没有被重新编组,因此当结构重新提交到DocumentProperties时会丢失。有任何想法吗?非常感谢。
答案 0 :(得分:3)
某些较旧版本的Pywin32中存在一个可能导致该行为的错误。 尝试安装最新版本(217)。