Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from PyQt4 import QtCore, QtGui
- import sys
- import fitz
- class TaskThread(QtCore.QThread):
- notifyProgress = QtCore.pyqtSignal(int)
- def run(self):
- window.submit_functionality()
- class Window(QtGui.QWidget):
- def __init__(self):
- super(Window, self).__init__()
- dialog = QtGui.QFileDialog()
- dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
- if dialog.exec_() == QtGui.QDialog.Accepted:
- x=dialog.selectedFiles()
- self.Files=[str(name) for name in x][0]
- path = str(self.Files)
- self.segregate_dialog=QtGui.QDialog()
- self.rename=""
- self.segregate_dialog.setWindowFlags(QtCore.Qt.FramelessWindowHint)
- self.dialog_vbox=QtGui.QVBoxLayout(self.segregate_dialog)
- self.formlayout=QtGui.QFormLayout()
- self.label=QtGui.QLabel("FilePath:")
- self.linedit_path=QtGui.QLabel()
- self.linedit_path.setText(str(self.Files))
- self.cancel=QtGui.QPushButton("Cancel")
- self.cancel.clicked.connect(self.segregate_dialog.close)
- self.submit=QtGui.QPushButton("Submit")
- self.progressBar = QtGui.QProgressBar()
- self.progressBar.hide()
- self.myLongTask = TaskThread()
- self.myLongTask.notifyProgress.connect(self.onProgress)
- self.submit.clicked.connect(self.submit_functionality)
- self.pdfsplitter_checkBox2 = QtGui.QCheckBox("PdfSplitter")
- self.pdfsplitter_checkBox2.stateChanged.connect(self.pdfsplitter_checked)
- self.batch_label=QtGui.QLabel("No of Batches:")
- self.dropdown1 = QtGui.QSpinBox()
- self.dropdown1.setMaximum(20)
- self.dropdown1.setValue(1)
- self.dropdown1.setMinimum(1)
- self.prefix_label=QtGui.QLabel("Prefix")
- self.prefix_label.hide()
- self.prefix_filled=QtGui.QLineEdit()
- self.prefix_filled.hide()
- self.formlayout.addRow(self.label,self.linedit_path)
- self.formlayout.addRow(self.prefix_label,self.prefix_filled)
- self.formlayout.addRow(self.pdfsplitter_checkBox2)
- self.formlayout.addRow(self.batch_label,self.dropdown1)
- self.formlayout.addRow(self.progressBar)
- self.formlayout.addRow(self.cancel,self.submit)
- self.dialog_vbox.addLayout(self.formlayout)
- self.segregate_dialog.exec_()
- def onStart(self):
- self.myLongTask.start()
- def onProgress(self, i):
- self.progressBar.setValue(i)
- def pdfsplitter_checked(self):
- if self.pdfsplitter_checkBox2.isChecked():
- self.pdfSplitter = True
- else:
- self.pdfSplitter = False
- def submit_functionality(self):
- try:
- self.progressBar.show()
- except:
- pass
- flPath = str(self.Files)
- batch_value=self.dropdown1.value()
- folderFiles = []
- pdfFiles = []
- pdfToImgFiles = []
- for f in os.listdir(flPath):
- folFilepath = os.path.join(flPath,f)
- if str(f).lower().endswith('.pdf') and self.pdfSplitter:
- pdfFiles.append(folFilepath)
- elif any(str(f).lower().endswith(ext) for ext in ['png','jpg','jpeg','svg','tiff','tif','gif','.pdf']):
- folderFiles.append(folFilepath)
- for i in pdfFiles:
- doc = fitz.open(i)
- for idx in range(len(doc)):
- page = doc.loadPage(idx)
- zoom_x = 2.0
- zoom_y = 2.0
- trans = fitz.Matrix(zoom_x, zoom_y).preRotate(0)
- pix = page.getPixmap(matrix=trans, alpha=False)
- imgName = os.path.basename(i).split('.')[0]+'_'+str(idx)+'.png'
- pix.writePNG(os.path.join(flPath,imgName))
- folderFiles.append(os.path.join(flPath,imgName))
- pdfToImgFiles.append(os.path.join(flPath,imgName))
- totFiles = len(folderFiles)
- filesperfolder = int(totFiles/batch_value)
- dirsList = []
- for i in range(batch_value):
- dirName = os.path.join(flPath,os.path.basename(flPath)+'_OUTPUT_{0}'.format(i))
- try:
- shutil.rmtree(dirName)
- os.makedirs(dirName)
- dirsList.append(dirName)
- except:
- os.makedirs(dirName)
- dirsList.append(dirName)
- if len(dirName)>0:
- foldIdx = 0
- fPageCt = 1
- for idx,i in enumerate(folderFiles):
- if fPageCt>filesperfolder:
- fPageCt = 2
- if foldIdx+1 != len(dirsList):
- foldIdx += 1
- else:
- fPageCt += 1
- destFolder = dirsList[foldIdx]
- flNm = os.path.basename(i)
- shutil.copy(i,os.path.join(destFolder,flNm))
- try:
- if i in pdfToImgFiles:
- os.remove(i)
- except:
- pass
- print 'done successfullyyyyyyyyyyyyyyyyyyyyyyy'
- self.segregate_dialog.close()
- if __name__ == '__main__':
- app = QtGui.QApplication(sys.argv)
- window = Window()
- window.showMaximized()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement