Python-02 PDF文件合成小工具

Python-02 PDF文件合成小工具


工具目的:有些公司受软件版权限制,只要PDF阅读器无法编辑合成PDF文件。在缺乏PDF编辑器的条件下,将两个PDF文件打包并合成新命名为一个PDF文件。

功能介绍:

1.点击按钮“第一个文件”选择需要合并的第一个PDF文件。

2.点击按钮“第二个文件”选择需要合并的第二个PDF文件。

3.在New File Name后输入新合成的PDF文件名字。

4.点击按钮“合成文件”打开将第一个PDF文件和第二个PDF文件合成并命名为步骤3的新PDF文件。

实现该功能主要运行PyPDF2第三方库中的PdfFileReader、PdfFileMerger两个函数功能。步骤4,为了点击按钮打开显示PDF文件,运用了os.startfile(filename)函数,笔者刚开始的时候在这里曾使用os.system(filename)和os.popen(filenam)运行后打开PDF文件都比较慢而且容易出现程序卡顿等现象。

主功能PDFCombi.py函数:

if __name__ == "__main__":

    app = QApplication(sys.argv)

    app.setStyle(QStyleFactory.create("Windows"))

    Win_PDF = PDFMain()

    Win_PDF.show()

    sys.exit(app.exec_())

global_PDF.py定义以下6个全局变量。

global file_address1

global file_address2

global current_dir1

global current_dir2

global fname

global fullname

还有一种运用全局变量的方法,就是在主函数开始的时候声明该变量为全局变量,等到该定义函数需要运用该变量的时候再次声明该变量为全局变量。如:

global fileaddress

calss Main(a,b)

def function(self):

 global fileaddress

 fileaddress =1


其次,就是利用PyQt5设计小程序的GUI运行界面。


from PyQt5import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):

def setupUi(self, MainWindow):

MainWindow.setObjectName("MainWindow")

MainWindow.resize(306, 220)

sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)

sizePolicy.setHorizontalStretch(0)

sizePolicy.setVerticalStretch(0)

sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())

MainWindow.setSizePolicy(sizePolicy)

self.centralwidget = QtWidgets.QWidget(MainWindow)

self.centralwidget.setObjectName("centralwidget")

self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)

self.gridLayout.setObjectName("gridLayout")

self.label = QtWidgets.QLabel(self.centralwidget)

self.label.setEnabled(True)

font = QtGui.QFont()

font.setFamily("Cambria")

font.setPointSize(16)

font.setBold(True)

font.setItalic(False)

font.setWeight(75)

self.label.setFont(font)

self.label.setAlignment(QtCore.Qt.AlignCenter)

self.label.setObjectName("label")

self.gridLayout.addWidget(self.label, 0, 1, 1, 2)

self.pushButton_F1 = QtWidgets.QPushButton(self.centralwidget)

self.pushButton_F1.setEnabled(True)

font = QtGui.QFont()

font.setFamily("Cambria")

font.setPointSize(12)

self.pushButton_F1.setFont(font)

self.pushButton_F1.setObjectName("pushButton_F1")

self.gridLayout.addWidget(self.pushButton_F1, 1, 1, 1, 1)

self.pushButton_F2 = QtWidgets.QPushButton(self.centralwidget)

self.pushButton_F2.setEnabled(True)

font = QtGui.QFont()

font.setFamily("Cambria")

font.setPointSize(12)

self.pushButton_F2.setFont(font)

self.pushButton_F2.setObjectName("pushButton_F2")

self.gridLayout.addWidget(self.pushButton_F2, 1, 2, 1, 1)

self.label_4 = QtWidgets.QLabel(self.centralwidget)

font = QtGui.QFont()

font.setFamily("Adobe Devanagari")

font.setPointSize(12)

font.setItalic(True)

self.label_4.setFont(font)

self.label_4.setAlignment(QtCore.Qt.AlignCenter)

self.label_4.setObjectName("label_4")

self.gridLayout.addWidget(self.label_4, 2, 0, 1, 1)

self.lineEdit_F1 = QtWidgets.QLineEdit(self.centralwidget)

self.lineEdit_F1.setEnabled(False)

self.lineEdit_F1.setObjectName("lineEdit_F1")

self.gridLayout.addWidget(self.lineEdit_F1, 2, 1, 1, 2)

self.label_5 = QtWidgets.QLabel(self.centralwidget)

font = QtGui.QFont()

font.setFamily("Adobe Devanagari")

font.setPointSize(12)

font.setItalic(True)

self.label_5.setFont(font)

self.label_5.setAlignment(QtCore.Qt.AlignCenter)

self.label_5.setObjectName("label_5")

self.gridLayout.addWidget(self.label_5, 3, 0, 1, 1)

self.lineEdit_F2 = QtWidgets.QLineEdit(self.centralwidget)

self.lineEdit_F2.setEnabled(False)

self.lineEdit_F2.setObjectName("lineEdit_F2")

self.gridLayout.addWidget(self.lineEdit_F2, 3, 1, 1, 2)

self.label_7 = QtWidgets.QLabel(self.centralwidget)

font = QtGui.QFont()

font.setFamily("Adobe Devanagari")

font.setPointSize(12)

font.setItalic(True)

self.label_7.setFont(font)

self.label_7.setAlignment(QtCore.Qt.AlignCenter)

self.label_7.setObjectName("label_7")

self.gridLayout.addWidget(self.label_7, 4, 0, 1, 1)

self.pushButton_CB = QtWidgets.QPushButton(self.centralwidget)

font = QtGui.QFont()

font.setFamily("Comic Sans MS")

font.setPointSize(16)

self.pushButton_CB.setFont(font)

self.pushButton_CB.setObjectName("pushButton_CB")

self.gridLayout.addWidget(self.pushButton_CB, 5, 1, 1, 1)

self.label_3 = QtWidgets.QLabel(self.centralwidget)

self.label_3.setEnabled(True)

font = QtGui.QFont()

font.setFamily("Comic Sans MS")

font.setPointSize(9)

font.setBold(False)

font.setItalic(False)

font.setWeight(50)

self.label_3.setFont(font)

self.label_3.setTextFormat(QtCore.Qt.RichText)

self.label_3.setAlignment(QtCore.Qt.AlignCenter)

self.label_3.setObjectName("label_3")

self.gridLayout.addWidget(self.label_3, 6, 2, 1, 1)

self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)

self.lineEdit.setEnabled(True)

self.lineEdit.setObjectName("lineEdit")

self.gridLayout.addWidget(self.lineEdit, 4, 1, 1, 1)

self.label_6 = QtWidgets.QLabel(self.centralwidget)

self.label_6.setEnabled(True)

font = QtGui.QFont()

font.setFamily("Comic Sans MS")

font.setPointSize(9)

font.setBold(False)

font.setItalic(False)

font.setWeight(50)

self.label_6.setFont(font)

self.label_6.setLayoutDirection(QtCore.Qt.LeftToRight)

self.label_6.setTextFormat(QtCore.Qt.RichText)

self.label_6.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignVCenter)

self.label_6.setObjectName("label_6")

self.gridLayout.addWidget(self.label_6, 4, 2, 1, 1)

MainWindow.setCentralWidget(self.centralwidget)

self.menubar = QtWidgets.QMenuBar(MainWindow)

self.menubar.setGeometry(QtCore.QRect(0, 0, 306, 18))

self.menubar.setObjectName("menubar")

MainWindow.setMenuBar(self.menubar)

self.statusbar = QtWidgets.QStatusBar(MainWindow)

self.statusbar.setObjectName("statusbar")

MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)

QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):

_translate = QtCore.QCoreApplication.translate

MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

self.label.setText(_translate("MainWindow", "PDF文件合成小工具"))

self.pushButton_F1.setText(_translate("MainWindow", "第一个文件"))

self.pushButton_F2.setText(_translate("MainWindow", "第二个文件"))

self.label_4.setText(_translate("MainWindow", "File1 Path:"))

self.label_5.setText(_translate("MainWindow", "File2 Path:"))

self.label_7.setText(_translate("MainWindow", "New File Name:"))

self.pushButton_CB.setText(_translate("MainWindow", "文件合成"))

self.label_3.setText(_translate("MainWindow", "X.Yu @ 2020-08-21"))

self.label_6.setText(_translate("MainWindow", ".pdf"))


欢迎感兴趣的小伙伴对PDF文件操作问题的留言探讨~

2020-09-18

你可能感兴趣的:(Python-02 PDF文件合成小工具)