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
반응형
'Python > Application' 카테고리의 다른 글
[파이썬] Low, High, Band Pass filter를 이용한 신호 처리, FFT 변환으로 주파수 영역 확인. (2) | 2021.06.18 |
---|---|
[파이썬 실습] Python 코딩 ... PDF 양면 스캔 !! 교차로 저장하기 !! (0) | 2020.10.08 |
[파이썬 응용] PDF 합치는 프로그램(1) (1) | 2020.07.20 |
[파이썬 응용] QT designer to python code ( .ui -> .py) UI파일을 파이썬 코드로 변환 (0) | 2020.07.14 |
[파이썬 실습] 파이썬으로 알아보는 신호 시간영역과 주파수 영역 (0) | 2020.06.13 |