반응형
import sys
from PySide2.QtWidgets import (QApplication, QWidget, QVBoxLayout, QPushButton, QLabel,
QListWidget, QFileDialog, QMessageBox, QInputDialog)
from PySide2.QtGui import QPixmap
from PyPDF2 import PdfMerger, PdfReader, PdfWriter
class PDFMergerApp(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('PDF Merger')
self.setGeometry(100, 100, 400, 300)
self.layout = QVBoxLayout()
self.button_add = QPushButton('Add PDF', self)
self.button_add.clicked.connect(self.add_pdf)
self.layout.addWidget(self.button_add)
self.button_up = QPushButton('Move Up', self)
self.button_up.clicked.connect(self.move_up)
self.layout.addWidget(self.button_up)
self.button_down = QPushButton('Move Down', self)
self.button_down.clicked.connect(self.move_down)
self.layout.addWidget(self.button_down)
self.button_remove = QPushButton('Remove PDF', self)
self.button_remove.clicked.connect(self.remove_pdf)
self.layout.addWidget(self.button_remove)
self.button_merge = QPushButton('Merge PDFs', self)
self.button_merge.clicked.connect(self.merge_pdfs)
self.layout.addWidget(self.button_merge)
self.button_delete_page = QPushButton('Delete Page', self) # 페이지 삭제 버튼 추가
self.button_delete_page.clicked.connect(self.delete_page)
self.layout.addWidget(self.button_delete_page)
self.list_widget = QListWidget(self)
self.layout.addWidget(self.list_widget)
self.setLayout(self.layout)
def add_pdf(self):
file_paths, _ = QFileDialog.getOpenFileNames(self, "Add PDF Files", "", "PDF Files (*.pdf)")
if file_paths:
for file_path in file_paths:
self.list_widget.addItem(file_path)
def remove_pdf(self):
for item in self.list_widget.selectedItems():
self.list_widget.takeItem(self.list_widget.row(item))
def move_up(self):
current_row = self.list_widget.currentRow()
if current_row > 0:
item = self.list_widget.takeItem(current_row)
self.list_widget.insertItem(current_row - 1, item)
self.list_widget.setCurrentRow(current_row - 1)
def move_down(self):
current_row = self.list_widget.currentRow()
if current_row < self.list_widget.count() - 1:
item = self.list_widget.takeItem(current_row)
self.list_widget.insertItem(current_row + 1, item)
self.list_widget.setCurrentRow(current_row + 1)
def merge_pdfs(self):
if self.list_widget.count() > 0:
merger = PdfMerger()
for index in range(self.list_widget.count()):
merger.append(self.list_widget.item(index).text())
output_file, _ = QFileDialog.getSaveFileName(self, "Save Merged PDF", "", "PDF Files (*.pdf)")
if output_file:
merger.write(output_file)
merger.close()
QMessageBox.information(self, "Success", "PDFs merged successfully!")
else:
QMessageBox.warning(self, "Error", "Please select PDFs to merge.")
def delete_page(self):
file_path, _ = QFileDialog.getOpenFileName(self, "Select PDF File", "", "PDF Files (*.pdf)")
if file_path:
reader = PdfReader(file_path)
page_count = len(reader.pages)
items = [str(i+1) for i in range(page_count)]
item, ok = QInputDialog.getItem(self, "Delete Page", "Select page number to delete:", items, 0, False)
if ok and item:
page_number = int(item)
writer = PdfWriter()
for i in range(page_count):
if i != page_number - 1: # 삭제할 페이지 번호를 제외하고 추가
writer.add_page(reader.pages[i])
output_file, _ = QFileDialog.getSaveFileName(self, "Save PDF", "", "PDF Files (*.pdf)")
if output_file:
with open(output_file, "wb") as f:
writer.write(f)
QMessageBox.information(self, "Success", "Page deleted successfully!")
else:
QMessageBox.warning(self, "Error", "No page selected.")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = PDFMergerApp()
ex.show()
sys.exit(app.exec_())
pdf 파일의 페이지를 합치거나 삭제하는 예제...
반응형
'프로그램&회로' 카테고리의 다른 글
pyinstaller 고급지게 사용하기 (0) | 2025.01.13 |
---|---|
touchgfx snprintfFloat, snprintfFloats (0) | 2024.12.16 |
touchGFX (0) | 2024.12.10 |
python - pc - pyserial (1) | 2024.11.20 |
역률 (0) | 2024.11.14 |