将DeviceCapabilities与pywin32一起使用

时间:2016-01-21 10:15:00

标签: python printing pywin32

我想使用python更改打印机的托盘。我尝试使用以下代码来检索有关打印机及其值的信息:

import win32print
x = win32print.DeviceCapabilities('Name of my printer', '192.168.x.x', DC_BINS)
print (x)

DC_BINS应该给我一个int序列。每个都属于不同的托盘。但是,当我尝试运行时,该程序会说" DC_BINS"没有定义。我究竟做错了什么?我对python很新。

1 个答案:

答案 0 :(得分:2)

您的代码已更正:

import win32print
import win32con
x = win32print.DeviceCapabilities('Name of my printer', '192.168.x.x', win32con.DC_BINS)
print (x)

代码列出了所有打印机,本地和远程以及他们的功能:

import win32print
from win32con import *

DC_CONSTANTS = [
    DC_BINNAMES, DC_BINS, DC_COLLATE, DC_COLORDEVICE, DC_COPIES, DC_DRIVER,
    DC_DUPLEX, DC_ENUMRESOLUTIONS, DC_EXTRA, DC_FIELDS,
    DC_FILEDEPENDENCIES, DC_MAXEXTENT, DC_MEDIAREADY, DC_MEDIATYPENAMES,
    DC_MEDIATYPES, DC_MINEXTENT, DC_ORIENTATION, DC_NUP, DC_PAPERNAMES,
    DC_PAPERS, DC_PAPERSIZE, DC_PERSONALITY, DC_PRINTERMEM, DC_PRINTRATE, DC_PRINTRATEPPM,
    DC_PRINTRATEUNIT, DC_SIZE, DC_STAPLE, DC_TRUETYPE, DC_VERSION,
]


def DC_INFO(constant):
    for a_global in globals().keys():
        if a_global.startswith("DC_") and globals().get(a_global) == constant:
            return a_global
    return "DC_UNKONWN"


for printer in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS):
    print printer
    for constant in DC_CONSTANTS:
        try:
            x = win32print.DeviceCapabilities(printer[2], '', constant)
            print "\t", DC_INFO(constant), x
        except:
            pass

我建议使用IDE进行python,好的IDE会标记未知符号并建议从哪里导入(如PyCharm那样)。