import sys
import os
from PyPDF2.pdf import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MainWidget(QWidget):
def __init__(self):
super().__init__()
manual_label = [QLabel("<b>PDF Application"), QLabel("Created by JS")]
for i in range(0, 2):
manual_label[i].setStyleSheet("Color : gray")
manual_label[i].setFont(QFont("", 40 - (12 * i)))
hbox = [QHBoxLayout(), QHBoxLayout()]
for i in range(0, 2):
hbox[i].addStretch(1)
hbox[i].addWidget(manual_label[i])
hbox[i].addStretch(1)
vbox = QVBoxLayout()
vbox.addStretch(1)
vbox.addLayout(hbox[0])
vbox.addLayout(hbox[1])
vbox.addStretch(1)
self.setLayout(vbox)
class ListboxWidget(QListWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(Qt.CopyAction)
event.accept()
links = []
for i in event.mimeData().urls():
if str(i).find(".pdf") > 0:
if i.isLocalFile():
links.append(str(i.toLocalFile()))
else:
links.append(str(i.toString()))
self.addItems(links)
else:
event.ignore()
class PDFMerge(QWidget):
def __init__(self):
super().__init__()
self.pushButton = QPushButton(self)
self.pushButton.setGeometry(QRect(20, 440, 451, 31))
self.pushButton.setText("ํฉ์น๊ธฐ")
self.groupBox = QGroupBox(self)
self.groupBox.setGeometry(QRect(20, 20, 451, 410))
self.groupBox.setFlat(False)
self.groupBox.setCheckable(False)
self.groupBox.setTitle("<PDF ๋ฆฌ์คํธ>")
self.listWidget = ListboxWidget(self.groupBox)
self.listWidget.setGeometry(QRect(20, 30, 411, 250))
self.pushButton_4 = QPushButton(self.groupBox)
self.pushButton_4.setGeometry(QRect(20, 310, 93, 28))
self.pushButton_4.setText("์ ์ด๋")
self.pushButton_5 = QPushButton(self.groupBox)
self.pushButton_5.setGeometry(QRect(20, 350, 93, 28))
self.pushButton_5.setText("์๋ ์ด๋")
self.pushButton_6 = QPushButton(self.groupBox)
self.pushButton_6.setGeometry(QRect(180, 310, 93, 71))
self.pushButton_6.setText("์ถ ๊ฐ")
self.pushButton_7 = QPushButton(self.groupBox)
self.pushButton_7.setGeometry(QRect(340, 310, 93, 71))
self.pushButton_7.setText("์ ๊ฑฐ")
self.pushButton_6.clicked.connect(self.add_PDF)
self.pushButton_7.clicked.connect(self.del_PDF)
self.pushButton_4.clicked.connect(self.up_PDF)
self.pushButton_5.clicked.connect(self.down_PDF)
self.pushButton.clicked.connect(self.merge_PDF)
def add_PDF(self):
fname = QFileDialog.getOpenFileName(self, "open file", "", "PDF File(*.pdf)")
self.listWidget.addItem(QListWidgetItem(fname[0]))
def set_PDF(self):
dirname = QFileDialog.getExistingDirectory(None)
self.listWidget_2.clear()
self.listWidget_2.addItem(QListWidgetItem(dirname))
def del_PDF(self):
self.listWidget.takeItem(self.listWidget.currentRow())
def up_PDF(self):
row = self.listWidget.currentRow()
if row > 0:
item = self.listWidget.takeItem(row)
self.listWidget.insertItem(row - 1, item)
self.listWidget.setCurrentRow(row - 1)
def down_PDF(self):
row = self.listWidget.currentRow()
if row < self.listWidget.count() - 1:
item = self.listWidget.takeItem(row)
self.listWidget.insertItem(row + 1, item)
self.listWidget.setCurrentRow(row + 1)
def merge_PDF(self):
count = self.listWidget.count()
if count == 0:
QMessageBox.information(self, '๊ฒฝ๊ณ ', "์ ์ฅํ ํ์ผ์ด ์์ต๋๋ค.", QMessageBox.Yes)
else:
down_path, _ = QFileDialog.getSaveFileName(self, "Open", "", "PDF(*.pdf)")
if down_path:
output = PdfFileWriter()
for x in range(0, count):
src = self.listWidget.item(x)
inputs = PdfFileReader(open(src.text(), "rb"))
for j in range(inputs.getNumPages()):
output.addPage(inputs.getPage(j))
dest = down_path
with open(dest, "wb") as outstream:
output.write(outstream)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# menu bar widget ๋ชจ์
menu = self.menuBar()
menu_pdf = menu.addMenu(' PDF ')
self.pdf_merge = QAction('PDF ํฉ์น๊ธฐ', self)
menu_pdf.addAction(self.pdf_merge) # menu bar ์ lpf ๋ฅผ ๋ถ์
self.pdf_merge.triggered.connect(lambda x: self.applyfunc('merge')) # ๋ก์ฐํจ์คํํฐ๋ฅผ ๋๋ฅด๋ฉด applyDock ์ฐ๊ฒฐ
self.widget = MainWidget()
self.setCentralWidget(self.widget)
self.setEnabled(True)
self.resize(500, 500)
self.setWindowTitle("PDF Application") # Program Title
self.setFixedSize(500, 550)
self.show()
def applyfunc(self, function):
if function == "merge":
merge_widget = PDFMerge()
self.setCentralWidget(merge_widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
728x90
๋ฐ์ํ