2012-06-15 85 views
1

我正在尝试使用Python win32print模块更改打印机托盘,但没有取得任何成功。打印机支持两个“分档”:7 =自动和4 =手动。我想从'手动'纸箱开始打印作业。这里有一些代码:用pywin32更改打印机托盘

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时会丢失。有任何想法吗?非常感谢。

回答

3

Pywin32的某些旧版本中可能导致该行为的错误。 尝试安装最新版本(217)。

+0

这似乎工作。很好,非常感谢。 – simonhaines