Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. import os
  2. from PyQt4 import QtCore, QtGui
  3. import sys
  4. import fitz
  5. class TaskThread(QtCore.QThread):
  6. notifyProgress = QtCore.pyqtSignal(int)
  7. def run(self):
  8. window.submit_functionality()
  9. class Window(QtGui.QWidget):
  10. def __init__(self):
  11. super(Window, self).__init__()
  12. dialog = QtGui.QFileDialog()
  13. dialog.setFileMode(QtGui.QFileDialog.DirectoryOnly)
  14. if dialog.exec_() == QtGui.QDialog.Accepted:
  15. x=dialog.selectedFiles()
  16. self.Files=[str(name) for name in x][0]
  17. path = str(self.Files)
  18. self.segregate_dialog=QtGui.QDialog()
  19. self.rename=""
  20. self.segregate_dialog.setWindowFlags(QtCore.Qt.FramelessWindowHint)
  21. self.dialog_vbox=QtGui.QVBoxLayout(self.segregate_dialog)
  22. self.formlayout=QtGui.QFormLayout()
  23. self.label=QtGui.QLabel("FilePath:")
  24. self.linedit_path=QtGui.QLabel()
  25. self.linedit_path.setText(str(self.Files))
  26. self.cancel=QtGui.QPushButton("Cancel")
  27. self.cancel.clicked.connect(self.segregate_dialog.close)
  28. self.submit=QtGui.QPushButton("Submit")
  29. self.progressBar = QtGui.QProgressBar()
  30. self.progressBar.hide()
  31. self.myLongTask = TaskThread()
  32. self.myLongTask.notifyProgress.connect(self.onProgress)
  33. self.submit.clicked.connect(self.submit_functionality)
  34. self.pdfsplitter_checkBox2 = QtGui.QCheckBox("PdfSplitter")
  35. self.pdfsplitter_checkBox2.stateChanged.connect(self.pdfsplitter_checked)
  36. self.batch_label=QtGui.QLabel("No of Batches:")
  37. self.dropdown1 = QtGui.QSpinBox()
  38. self.dropdown1.setMaximum(20)
  39. self.dropdown1.setValue(1)
  40. self.dropdown1.setMinimum(1)
  41. self.prefix_label=QtGui.QLabel("Prefix")
  42. self.prefix_label.hide()
  43. self.prefix_filled=QtGui.QLineEdit()
  44. self.prefix_filled.hide()
  45. self.formlayout.addRow(self.label,self.linedit_path)
  46. self.formlayout.addRow(self.prefix_label,self.prefix_filled)
  47. self.formlayout.addRow(self.pdfsplitter_checkBox2)
  48. self.formlayout.addRow(self.batch_label,self.dropdown1)
  49. self.formlayout.addRow(self.progressBar)
  50. self.formlayout.addRow(self.cancel,self.submit)
  51. self.dialog_vbox.addLayout(self.formlayout)
  52. self.segregate_dialog.exec_()
  53. def onStart(self):
  54. self.myLongTask.start()
  55. def onProgress(self, i):
  56. self.progressBar.setValue(i)
  57. def pdfsplitter_checked(self):
  58. if self.pdfsplitter_checkBox2.isChecked():
  59. self.pdfSplitter = True
  60. else:
  61. self.pdfSplitter = False
  62. def submit_functionality(self):
  63. try:
  64. self.progressBar.show()
  65. except:
  66. pass
  67. flPath = str(self.Files)
  68. batch_value=self.dropdown1.value()
  69. folderFiles = []
  70. pdfFiles = []
  71. pdfToImgFiles = []
  72. for f in os.listdir(flPath):
  73. folFilepath = os.path.join(flPath,f)
  74. if str(f).lower().endswith('.pdf') and self.pdfSplitter:
  75. pdfFiles.append(folFilepath)
  76. elif any(str(f).lower().endswith(ext) for ext in ['png','jpg','jpeg','svg','tiff','tif','gif','.pdf']):
  77. folderFiles.append(folFilepath)
  78. for i in pdfFiles:
  79. doc = fitz.open(i)
  80. for idx in range(len(doc)):
  81. page = doc.loadPage(idx)
  82. zoom_x = 2.0
  83. zoom_y = 2.0
  84. trans = fitz.Matrix(zoom_x, zoom_y).preRotate(0)
  85. pix = page.getPixmap(matrix=trans, alpha=False)
  86. imgName = os.path.basename(i).split('.')[0]+'_'+str(idx)+'.png'
  87. pix.writePNG(os.path.join(flPath,imgName))
  88. folderFiles.append(os.path.join(flPath,imgName))
  89. pdfToImgFiles.append(os.path.join(flPath,imgName))
  90. totFiles = len(folderFiles)
  91. filesperfolder = int(totFiles/batch_value)
  92. dirsList = []
  93. for i in range(batch_value):
  94. dirName = os.path.join(flPath,os.path.basename(flPath)+'_OUTPUT_{0}'.format(i))
  95. try:
  96. shutil.rmtree(dirName)
  97. os.makedirs(dirName)
  98. dirsList.append(dirName)
  99. except:
  100. os.makedirs(dirName)
  101. dirsList.append(dirName)
  102. if len(dirName)>0:
  103. foldIdx = 0
  104. fPageCt = 1
  105. for idx,i in enumerate(folderFiles):
  106. if fPageCt>filesperfolder:
  107. fPageCt = 2
  108. if foldIdx+1 != len(dirsList):
  109. foldIdx += 1
  110. else:
  111. fPageCt += 1
  112. destFolder = dirsList[foldIdx]
  113.  
  114. flNm = os.path.basename(i)
  115. shutil.copy(i,os.path.join(destFolder,flNm))
  116. try:
  117. if i in pdfToImgFiles:
  118. os.remove(i)
  119. except:
  120. pass
  121. print 'done successfullyyyyyyyyyyyyyyyyyyyyyyy'
  122. self.segregate_dialog.close()
  123. if __name__ == '__main__':
  124. app = QtGui.QApplication(sys.argv)
  125. window = Window()
  126. window.showMaximized()
  127. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement