용지에 프린트를 할 때 파일이 좀 많을 경우 일일이 눌러가며 다 출력하기 부담스러운 경우가 있다. 이때 프린트를 자동으로 해주는 스크립트 코드를 사용한다면 시간을 절약할 수 있을 것이다. 같은 경로에서 [a-zA-Z0-9] + 특수문자로된 이름의 zip파일이 있고 거기에 pdf 파일이 담겨있다면 모두 출력해준다.
$ python -m pip install pathlib
$ python -m pip install PyWin32
$ python-silent-print-pdf-to-specific-printer : gswin32, gsprint 다운 + 위치된 경로 맞추기
같은 폴더에 놓고 파일 실행
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 | import win32print import win32api import zipfile import os import pathlib #-*-coding: utf-8 dir_path = os.path.dirname(os.path.realpath(__file__)) GHOSTSCRIPT_PATH = "C:\\Users\\soicem\\Downloads\\GHOSTSCRIPT\\bin\\gswin32.exe" GSPRINT_PATH = "C:\\Users\\soicem\\Downloads\\GSPRINT\\gsprint.exe" currentDirectory = pathlib.Path('.') currentPattern = "*.zip" print(currentDirectory) def printPDF(cDir): # YOU CAN PUT HERE THE NAME OF YOUR SPECIFIC PRINTER INSTEAD OF DEFAULT currentprinter = win32print.GetDefaultPrinter() cDir += '"' win32api.ShellExecute(0, 'open', GSPRINT_PATH, '-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "'+currentprinter+'" ".\\' + cDir, '.', 0) def main(): for currentFile in currentDirectory.glob(currentPattern): cFile = str(currentFile) print(cFile) with zipfile.ZipFile(cFile, "r") as zip_ref: targetFolder = cFile.split('.')[0] zip_ref.extractall(targetFolder) subDirectory = pathlib.Path(targetFolder) print(subDirectory) for subFile in subDirectory.iterdir(): sFile = str(subFile).replace("\\", "\\\\") print(sFile) printPDF(sFile) main() #currentprinter = win32print.GetDefaultPrinter() #cDir = "6\\142.pdf" + '"' #print('-ghostscript "'+GHOSTSCRIPT_PATH+'" -printer "'+currentprinter+'" ".\\' + cDir) | cs |
'* Computer Science > py' 카테고리의 다른 글
git bash에서 파이썬으로 git untracked files 제거 (0) | 2018.08.15 |
---|---|
collections (0) | 2018.07.09 |
Asterisk (0) | 2018.07.09 |
zip, enumurate, lambda, map reduce (0) | 2018.07.09 |
join & traslate & title (0) | 2018.03.04 |