관리 메뉴

웹개발자의 기지개

[Python] 프린터 정보 얻기 - win32print 본문

python

[Python] 프린터 정보 얻기 - win32print

http://portfolio.wonpaper.net 2021. 4. 24. 21:40

파이썬으로 현재 컴퓨터에 연결되어 있는 프린터 관련 정보를 얻어 보자.

 

먼저 아래와 같이 win32print 모듈을 다운받기 전에 pip 버전을 확인하고 가상환경 설치파일 버전도 확인하자.

필자는 F:\python_win32 폴더를 미리 만들어 두었다.

 

1
2
3
4
5
F:\python_win32>pip3 --version
pip 20.2.3 from c:\users\wonpa\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)
 
F:\python_win32>virtualenv --version
virtualenv 20.4.0 from c:\users\wonpa\appdata\local\programs\python\python39\lib\site-packages\virtualenv\__init__.py
cs

 

# 가상환경 설정

printer_venv 라는 이름으로 가상환경 설정한다.

1
2
3
4
5
6
7
F:\python_win32>virtualenv printer_venv
 
created virtual environment CPython3.9.1.final.0-64 in 6942ms
  creator CPython3Windows(dest=F:\python_win32\printer_venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\wonpa\AppData\Local\pypa\virtualenv)
    added seed packages: pip==20.3.3, setuptools==51.3.3, wheel==0.36.2
  activators BashActivator,BatchActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
cs

 

# 가상환경 실행하기

F:\python_win32\printer_venv\Scripts>.\activate.bat

 

그런다음 dir 을 눌러 현재 작업폴더 위치를 확인한다.

print_test1.py 가 필자가 만든 프린터 정보얻기 예제 파일명이다. 

 

(printer_venv) F:\python_win32>dir
 F 드라이브의 볼륨: 새 볼륨
 볼륨 일련 번호: 48CB-8007

 F:\python_win32 디렉터리

2021-03-26  오후 07:14    <DIR>          .
2021-03-26  오후 07:14    <DIR>          ..
2021-03-26  오후 07:14    <DIR>          printer_venv
2021-03-26  오후 07:16                89 print_test1.py
               1개 파일                  89 바이트
               3개 디렉터리  750,848,716,800 바이트 남음

 

# win32printing 모듈 설치한다.

(printer_venv) F:\python_win32>pip3 install win32printing 
Collecting win32printing
  Downloading win32printing-0.1.3.tar.gz (4.9 kB)
Collecting pywin32
  Downloading pywin32-300-cp39-cp39-win_amd64.whl (9.2 MB)
     |████████████████████████████████| 9.2 MB 6.4 MB/s
Building wheels for collected packages: win32printing
  Building wheel for win32printing (setup.py) ... done
  Created wheel for win32printing: filename=win32printing-0.1.3-py3-none-any.whl size=4846 sha256=5b978023a44aba2d053b0cd394cb70fcc434cb47e91b9a63661f3932f4c4d2cc
  Stored in directory: c:\users\wonpa\appdata\local\pip\cache\wheels\48\60\2d\9762ddc48e2a7861d2f6d6e47f0d5f33005b78d5acf3af904b
Successfully built win32printing
Installing collected packages: pywin32, win32printing
Successfully installed pywin32-300 win32printing-0.1.3
WARNING: You are using pip version 20.3.3; however, version 21.0.1 is available.
You should consider upgrading via the 'F:\python_win32\printer_venv\Scripts\python.exe -m pip install --upgrade pip' command.

 

이제 win32print 라이브러리를 이용하기 위한 준비과정은 끝이 났다.

본론으로 들어가서 프린터 관련 정보를 땡겨보도록 하자.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import win32print
import sys
 
# 전체 설치되어 있는 프린트정보 얻기
printers = win32print.EnumPrinters(2)
print(printers)
 
"""
((8388608, 'Samsung M267x 287x Series (USB001),Samsung Universal Print Driver 3,', 'Samsung M267x 287x Series (USB001)', ''), (8388608, 'Samsung M267x 287x Series,Samsung M267x 287x Series Class Driver,', 'Samsung M267x 287x Series', ''), (8388608, 'OneNote for Windows 10,Microsoft Software Printer Driver,', 'OneNote for Windows 10', ''), (8388608, 'Microsoft XPS Document Writer,Microsoft XPS Document Writer v4,', 'Microsoft XPS Document Writer', ''), (8388608, 'Microsoft Print to PDF,Microsoft Print To PDF,', 'Microsoft Print to PDF', ''), (8388608, 'Fax,Microsoft Shared Fax Driver,', 'Fax', ''))
"""
 
# 기본 프린터명 - Samsung M267x 287x Series (USB001) (리턴값 string)
print_name = win32print.GetDefaultPrinter()
 
# 특정 프린터를 기본프린터로 지정하기
# win32print.SetDefaultPrinter('EPSON Stylus C86 Series')
 
print(print_name)
"""
Samsung M267x 287x Series (USB001)
"""
 
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter(print_name,PRINTER_DEFAULTS)
# print(pHandle)  # <PyPrinterHANDLE:2404262182136>
 
properties = win32print.GetPrinter(pHandle, 2
print(properties)
"""
{'pServerName': None, 'pPrinterName': 'Samsung M267x 287x Series (USB001)', 'pShareName': 'Samsung M267x 287x Series', 'pPortName': 'USB001', 'pDriverName': 'Samsung Universal Print Driver 3', 'pComment': '', 'pLocation': '', 'pDevMode': <PyDEVMODEW object at 0x0000020A2EC9CAD0>, 'pSepFile': '', 'pPrintProcessor': 'us015PC', 'pDatatype': 'RAW', 'pParameters': '', 'pSecurityDescriptor': <PySECURITY_DESCRIPTOR object at 0x0000020A2EC97D00>, 'Attributes': 3648, 'Priority': 1, 'DefaultPriority': 0, 'StartTime': 0, 'UntilTime': 0, 'Status': 0, 'cJobs': 0, 'AveragePPM': 0}
"""
 
# 프린터 고급옵션 설정정보
pDevMode = properties["pDevMode"]
print(pDevMode)
"""
<PyDEVMODEW object at 0x000001A15D693490>
"""
 
print(properties["pDevMode"].Copies)  # 인쇄할 용지수
"""
1
"""
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
for i in dir(pDevMode):
  print("%s\t%s" % (i,getattr(pDevMode,i)))
 
"""
BitsPerPel      0
Clear   <built-in method Clear of PyDEVMODEW object at 0x0000014F423F2C10>
Collate 1
Color   0
Copies  1
DefaultSource   7
DeviceName      Samsung M267x 287x Series (USB0
DisplayFixedOutput      39321607
DisplayFlags    0
DisplayFrequency        0
DisplayOrientation      65636
DitherType      0
DriverData      b'SECD\x00\x00\x00\'
DriverExtra     10020
DriverVersion   1024
Duplex  1
Fields  58840847
FormName        A4
ICMIntent       2
ICMMethod       1
LogPixels       0
MediaType       0
Nup     0
Orientation     1
PanningHeight   0
PanningWidth    0
PaperLength     2970
PaperSize       9
PaperWidth      2100
PelsHeight      0
PelsWidth       0
Position_x      589825
Position_y      137628570
PrintQuality    600
Reserved1       0
Reserved2       0
Scale   100
Size    220
SpecVersion     1025
TTOption        3
YResolution     0
__class__       <class 'PyDEVMODEW'>
__delattr__     <method-wrapper '__delattr__' of PyDEVMODEW object at 0x0000014F423F2C10>
__dir__ <built-in method __dir__ of PyDEVMODEW object at 0x0000014F423F2C10>
__doc__ None
__eq__  <method-wrapper '__eq__' of PyDEVMODEW object at 0x0000014F423F2C10>
__format__      <built-in method __format__ of PyDEVMODEW object at 0x0000014F423F2C10>
__ge__  <method-wrapper '__ge__' of PyDEVMODEW object at 0x0000014F423F2C10>
__getattribute__        <method-wrapper '__getattribute__' of PyDEVMODEW object at 0x0000014F423F2C10>
__gt__  <method-wrapper '__gt__' of PyDEVMODEW object at 0x0000014F423F2C10>
__hash__        <method-wrapper '__hash__' of PyDEVMODEW object at 0x0000014F423F2C10>
__init__        <method-wrapper '__init__' of PyDEVMODEW object at 0x0000014F423F2C10>
__init_subclass__       <built-in method __init_subclass__ of type object at 0x0000000073F3F660>
__le__  <method-wrapper '__le__' of PyDEVMODEW object at 0x0000014F423F2C10>
__lt__  <method-wrapper '__lt__' of PyDEVMODEW object at 0x0000014F423F2C10>
__ne__  <method-wrapper '__ne__' of PyDEVMODEW object at 0x0000014F423F2C10>
__new__ <built-in method __new__ of type object at 0x0000000073F3F660>
__reduce__      <built-in method __reduce__ of PyDEVMODEW object at 0x0000014F423F2C10>
__reduce_ex__   <built-in method __reduce_ex__ of PyDEVMODEW object at 0x0000014F423F2C10>
__repr__        <method-wrapper '__repr__' of PyDEVMODEW object at 0x0000014F423F2C10>
__setattr__     <method-wrapper '__setattr__' of PyDEVMODEW object at 0x0000014F423F2C10>
__sizeof__      <built-in method __sizeof__ of PyDEVMODEW object at 0x0000014F423F2C10>
__str__ <method-wrapper '__str__' of PyDEVMODEW object at 0x0000014F423F2C10>
__subclasshook__        <built-in method __subclasshook__ of type object at 0x0000000073F3F660>
"""
 
# 인쇄지 한장에 표시되는 글자수
print(pDevMode.PaperLength)
 
# 프린터 닫기
win32print.ClosePrinter(pHandle)
cs

properties["pDevMode"] 가 핵심이다. 

각각의 속성이름을 바탕으로 프린터 고급 세부 설정내용을 살펴 볼 수 있다.

 

참고1 : pythonq.com/so/python/689567

참고2 : timgolden.me.uk/pywin32-docs/win32print.html

참고3 : stackoverflow.com/questions/5555453/python-win32print-changing-advanced-printer-options

참고4 : support.microsoft.com/en-us/office/prtdevmode-property-f87eebdc-a13e-484a-83ed-2e2beeb9d699

Comments