簡體   English   中英

使用pywin32更改打印機托盤

[英]Change printer tray with pywin32

我正在嘗試使用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時會丟失。 有任何想法嗎? 非常感謝。

某些舊版本的Pywin32中存在一個可能導致該行為的錯誤。 嘗試安裝最新版本(217)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM