당신은 pywin32의 win32print.EnumPrinters()
(편리), 또는이 ctypes
모듈 (낮은 의존성)를 통해 EnumPrinters()
API를 호출 할 수 있습니다.
오류 확인 기능이없는 ctypes
버전이 있습니다.
# Use EnumPrintersW to list local printers with their names and descriptions.
# Tested with CPython 2.7.10 and IronPython 2.7.5.
import ctypes
from ctypes.wintypes import BYTE, DWORD, LPCWSTR
winspool = ctypes.WinDLL('winspool.drv') # for EnumPrintersW
msvcrt = ctypes.cdll.msvcrt # for malloc, free
# Parameters: modify as you need. See MSDN for detail.
PRINTER_ENUM_LOCAL = 2
Name = None # ignored for PRINTER_ENUM_LOCAL
Level = 1 # or 2, 4, 5
class PRINTER_INFO_1(ctypes.Structure):
_fields_ = [
("Flags", DWORD),
("pDescription", LPCWSTR),
("pName", LPCWSTR),
("pComment", LPCWSTR),
]
# Invoke once with a NULL pointer to get buffer size.
info = ctypes.POINTER(BYTE)()
pcbNeeded = DWORD(0)
pcReturned = DWORD(0) # the number of PRINTER_INFO_1 structures retrieved
winspool.EnumPrintersW(PRINTER_ENUM_LOCAL, Name, Level, ctypes.byref(info), 0,
ctypes.byref(pcbNeeded), ctypes.byref(pcReturned))
bufsize = pcbNeeded.value
buffer = msvcrt.malloc(bufsize)
winspool.EnumPrintersW(PRINTER_ENUM_LOCAL, Name, Level, buffer, bufsize,
ctypes.byref(pcbNeeded), ctypes.byref(pcReturned))
info = ctypes.cast(buffer, ctypes.POINTER(PRINTER_INFO_1))
for i in range(pcReturned.value):
print info[i].pName, '=>', info[i].pDescription
msvcrt.free(buffer)
IronPython 2.7에서 ctypes를 사용하여 EnumPrinters()를 호출하는 예제를 제공 할 수 있습니까? 나는 ctypes.EnumPrinters()를 호출 해보고 오류가 발생했습니다. 고맙습니다,. – konrad
물론입니다. 'EnumPrintersA' 또는'EnumPrintersW'로 조건부로 정의 된'EnumPrinters'를 직접 호출 할 수 없다는 것에주의하십시오. 위의 편집 된 대답은'EnumPrintersW'를 사용합니다. – wdscxsj
와우! 이것은 꽤 놀랍습니다. 이렇게 정교한 대답을 해주셔서 정말 고마워요. 그것은 위대한 작품. 한 번 더 질문 할 수 있으면 내가 갈 것입니다. 이제는 사용 가능한 프린터 정보가 생겼으니, ADOBE PDF 인쇄 환경 설정과 특히 모든 인쇄물이 덤프되는 Adobe PDF 출력 폴더를 변경하는 중 하나가 더 있습니다. MSDN에서 PrinterProperties() 메서드를 발견했습니다. 그게 내가 필요한 걸 얻을 수 있을까? – konrad