Add batchprint.pyw
This commit is contained in:
parent
39226cfc97
commit
917cfaa4a1
204
batchprint.pyw
Normal file
204
batchprint.pyw
Normal file
@ -0,0 +1,204 @@
|
||||
#!/usr/bin/env python3
|
||||
# requires: pywin32, pillow, qtpy, a module compatible with qtpy (try pyqt5)
|
||||
import sys
|
||||
import ctypes
|
||||
import traceback
|
||||
import win32clipboard
|
||||
oldexcepthook = sys.excepthook
|
||||
def newexcepthook(type,value,tb):
|
||||
excText = "".join(traceback.format_exception(type,value,tb))
|
||||
try:
|
||||
for window in windows:
|
||||
try:
|
||||
window.close()
|
||||
except:
|
||||
pass
|
||||
except:
|
||||
pass
|
||||
|
||||
output = ctypes.windll.user32.MessageBoxW(None, u"" + excText + "\nThe program must close. Copy exception to clipboard?", u"Unhandled exception - batchprint", 0x00000114)
|
||||
if output == 6:
|
||||
copyString(excText)
|
||||
oldexcepthook(type,value,tb)
|
||||
sys.excepthook = newexcepthook
|
||||
|
||||
def copyString(s):
|
||||
win32clipboard.OpenClipboard()
|
||||
win32clipboard.EmptyClipboard()
|
||||
win32clipboard.SetClipboardText(s, win32clipboard.CF_UNICODETEXT)
|
||||
win32clipboard.CloseClipboard()
|
||||
|
||||
import os
|
||||
p = os.path.join
|
||||
pUp = os.path.dirname
|
||||
s = False
|
||||
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
|
||||
s = os.path.realpath(sys.executable)
|
||||
else:
|
||||
s = os.path.realpath(__file__)
|
||||
sp = pUp(s)
|
||||
|
||||
import winreg
|
||||
import subprocess
|
||||
import win32print
|
||||
import win32ui
|
||||
import PIL.Image
|
||||
import PIL.ImageWin
|
||||
import time
|
||||
|
||||
import qtpy
|
||||
import qtpy.QtGui as QtGui
|
||||
from qtpy.QtGui import *
|
||||
from qtpy.QtWidgets import *
|
||||
from qtpy.QtCore import *
|
||||
|
||||
PHYSICALWIDTH = 110
|
||||
PHYSICALHEIGHT = 111
|
||||
|
||||
def getPrinters():
|
||||
hive = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)
|
||||
key = winreg.OpenKey(hive,r"SYSTEM\CurrentControlSet\Control\Print\Printers")
|
||||
printers = []
|
||||
index = 0
|
||||
while True:
|
||||
try:
|
||||
printers.append(winreg.EnumKey(key,index))
|
||||
index += 1
|
||||
except OSError:
|
||||
break
|
||||
winreg.CloseKey(key)
|
||||
hive.Close()
|
||||
return printers
|
||||
|
||||
def printerOpenProperties(printerName):
|
||||
subprocess.Popen(["rundll32","printui.dll,PrintUIEntry","/e","/n" + printerName]).wait()
|
||||
|
||||
def printerOpenQueue(printerName):
|
||||
subprocess.Popen(["rundll32","printui.dll,PrintUIEntry","/o","/n" + printerName])
|
||||
|
||||
class printerWindow(QMainWindow):
|
||||
def __init__(self,printers,*args,**kwargs):
|
||||
super().__init__(*args,**kwargs)
|
||||
self.cTitle = "Choose a printer - batchprint"
|
||||
self.setWindowTitle(self.cTitle)
|
||||
|
||||
self.cWidth = 320
|
||||
self.cHeight = 82
|
||||
self.resize(self.cWidth,self.cHeight)
|
||||
|
||||
self.cPrinters = printers
|
||||
|
||||
self.cCreateElements()
|
||||
|
||||
def cCreateElements(self):
|
||||
self.cLabel = QLabel(self)
|
||||
self.cLabel.setText("Choose a printer:")
|
||||
self.cLabel.setAlignment(Qt.AlignCenter)
|
||||
self.cDropdown = QComboBox(self)
|
||||
self.cDropdown.addItems(printers)
|
||||
self.cButton = QPushButton(self)
|
||||
self.cButton.setText("OK")
|
||||
self.cButton.clicked.connect(self.cOpenList)
|
||||
|
||||
self.cResizeElements()
|
||||
self.show()
|
||||
|
||||
def cResizeElements(self):
|
||||
self.cLabel.move(0,5)
|
||||
self.cLabel.resize(self.cWidth,15)
|
||||
self.cDropdown.move(10,25)
|
||||
self.cDropdown.resize(self.cWidth - 20,22)
|
||||
self.cButton.move(self.cWidth - 64 - 5,self.cHeight - 22 - 5)
|
||||
self.cButton.resize(64,22)
|
||||
|
||||
def resizeEvent(self,event):
|
||||
self.cWidth = self.width()
|
||||
self.cHeight = self.height()
|
||||
self.cResizeElements()
|
||||
|
||||
def closeEvent(self,event):
|
||||
windows.remove(self)
|
||||
|
||||
def cOpenList(self):
|
||||
printer = self.cDropdown.currentText()
|
||||
self.close()
|
||||
printerOpenProperties(printer)
|
||||
printerOpenQueue(printer)
|
||||
windows.append(queueWindow(printer))
|
||||
|
||||
class queueWindow(QMainWindow):
|
||||
def __init__(self,printer,*args,**kwargs):
|
||||
super().__init__(*args,**kwargs)
|
||||
self.cTitle = printer+ " - batchprint"
|
||||
self.setWindowTitle(self.cTitle)
|
||||
self.setAcceptDrops(True)
|
||||
|
||||
self.cWidth = 240
|
||||
self.cHeight = 200
|
||||
self.resize(self.cWidth,self.cHeight)
|
||||
|
||||
self.cPrinter = printer
|
||||
self.cDC = win32ui.CreateDC()
|
||||
self.cDC.CreatePrinterDC(printer)
|
||||
self.cPrinterSize = self.cDC.GetDeviceCaps(PHYSICALWIDTH),self.cDC.GetDeviceCaps(PHYSICALHEIGHT)
|
||||
|
||||
self.cCreateElements()
|
||||
|
||||
def cCreateElements(self):
|
||||
self.cLabel = QLabel(self)
|
||||
self.cLabel.setText("Drag & Drop images here")
|
||||
self.cLabel.setAlignment(Qt.AlignCenter)
|
||||
self.cLabel.setWordWrap(True)
|
||||
|
||||
self.cResizeElements()
|
||||
self.show()
|
||||
|
||||
def cResizeElements(self):
|
||||
self.cLabel.move(0,0)
|
||||
self.cLabel.resize(self.cWidth,self.cHeight)
|
||||
|
||||
def resizeEvent(self,event):
|
||||
self.cWidth = self.width()
|
||||
self.cHeight = self.height()
|
||||
self.cResizeElements()
|
||||
|
||||
def dragEnterEvent(self,event):
|
||||
self.activateWindow()
|
||||
if event.mimeData().hasUrls():
|
||||
self.cLabel.setStyleSheet("font-weight: bold")
|
||||
event.accept()
|
||||
|
||||
def dragLeaveEvent(self,event):
|
||||
self.cLabel.setStyleSheet("")
|
||||
|
||||
def dropEvent(self,event):
|
||||
self.cLabel.setStyleSheet("")
|
||||
for url in event.mimeData().urls():
|
||||
path = str(url.toLocalFile()).replace("/",os.path.sep)
|
||||
self.cLabel.setText(path.rsplit(os.path.sep,1)[-1]+ " ...")
|
||||
self.repaint()
|
||||
bmp = PIL.Image.open(path)
|
||||
if bmp.size[1] > bmp.size[0]:
|
||||
if self.cPrinterSize[0] > self.cPrinterSize[1]:
|
||||
bmp = bmp.rotate(90)
|
||||
elif bmp.size[0] > bmp.size[1]:
|
||||
if self.cPrinterSize[1] > self.cPrinterSize[0]:
|
||||
bmp = bmp.rotate(90)
|
||||
|
||||
self.cDC.StartDoc(path)
|
||||
self.cDC.StartPage()
|
||||
dib = PIL.ImageWin.Dib(bmp)
|
||||
dib.draw(self.cDC.GetHandleOutput(),(0,0,self.cPrinterSize[0],self.cPrinterSize[1]))
|
||||
self.cDC.EndPage()
|
||||
self.cDC.EndDoc()
|
||||
self.cLabel.setText("Drag & Drop images here")
|
||||
|
||||
def closeEvent(self,event):
|
||||
self.cDC.DeleteDC()
|
||||
windows.remove(self)
|
||||
|
||||
windows = []
|
||||
printers = getPrinters()
|
||||
app = QApplication(sys.argv)
|
||||
windows.append(printerWindow(printers))
|
||||
app.exec_()
|
Loading…
Reference in New Issue
Block a user