我正在尝试使用 Python win32print 模块更改打印机托盘,但没有成功。打印机支持两个“bin”:7=Auto 和 4=Manual。我想从“手动”纸盒开始打印作业。这是一些代码:
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 时会丢失。有任何想法吗?非常感谢。