PyQt5是怎样制作PDF拆分功能的
Admin 2022-08-11 群英技术资讯 230 次浏览
本文就是基于PyQt5完成的PDF拆分功能,先看界面:
import os from PyQt5 import QtCore, QtGui, QtWidgets import base64 from return_png import img as retu # 引入img变量,赋别名为pdf_cf class Ui_PdfC(object): def setupUi(self, PdfC): tmp = open('return.png', 'wb') # 创建临时的文件 tmp.write(base64.b64decode(retu)) # 把这个one图片解码出来,写入文件中去。 tmp.close() PdfC.setObjectName("PdfC") PdfC.setWindowModality(QtCore.Qt.NonModal) PdfC.resize(1400, 814) PdfC.setMinimumSize(QtCore.QSize(1400, 814)) PdfC.setMaximumSize(QtCore.QSize(1400, 814)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) PdfC.setPalette(palette) self.Reback = QtWidgets.QToolButton(PdfC) self.Reback.setGeometry(QtCore.QRect(10, 10, 51, 41)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush) self.Reback.setPalette(palette) icon = QtGui.QIcon() icon.addPixmap(QtGui.QPixmap("return.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.Reback.setIcon(icon) self.Reback.setIconSize(QtCore.QSize(100, 100)) self.Reback.setAutoRaise(True) self.Reback.setObjectName("Reback") self.label = QtWidgets.QLabel(PdfC) self.label.setGeometry(QtCore.QRect(88, 8, 171, 41)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(16) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setObjectName("label") self.frame = QtWidgets.QFrame(PdfC) self.frame.setGeometry(QtCore.QRect(20, 60, 1351, 161)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.frame.setPalette(palette) self.frame.setAutoFillBackground(True) self.frame.setFrameShape(QtWidgets.QFrame.Box) self.frame.setFrameShadow(QtWidgets.QFrame.Plain) self.frame.setLineWidth(1) self.frame.setMidLineWidth(0) self.frame.setObjectName("frame") self.scwd = QtWidgets.QPushButton(self.frame) self.scwd.setGeometry(QtCore.QRect(570, 30, 191, 61)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush) self.scwd.setPalette(palette) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(16) font.setBold(True) font.setWeight(75) self.scwd.setFont(font) self.scwd.setAutoFillBackground(True) self.scwd.setAutoDefault(False) self.scwd.setDefault(False) self.scwd.setFlat(True) self.scwd.setObjectName("scwd") self.label_2 = QtWidgets.QLabel(self.frame) self.label_2.setGeometry(QtCore.QRect(620, 100, 131, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_2.setFont(font) self.label_2.setObjectName("label_2") self.frame_2 = QtWidgets.QFrame(PdfC) self.frame_2.setGeometry(QtCore.QRect(1050, 280, 321, 301)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrush(QtGui.QColor(245, 255, 250)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.frame_2.setPalette(palette) self.frame_2.setAutoFillBackground(True) self.frame_2.setFrameShape(QtWidgets.QFrame.NoFrame) self.frame_2.setFrameShadow(QtWidgets.QFrame.Plain) self.frame_2.setLineWidth(1) self.frame_2.setMidLineWidth(0) self.frame_2.setObjectName("frame_2") self.label_3 = QtWidgets.QLabel(self.frame_2) self.label_3.setGeometry(QtCore.QRect(20, 20, 191, 31)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(14) font.setBold(False) font.setWeight(50) self.label_3.setFont(font) self.label_3.setObjectName("label_3") self.xzwd = QtWidgets.QPushButton(self.frame_2) self.xzwd.setGeometry(QtCore.QRect(30, 190, 271, 61)) palette = QtGui.QPalette() brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(255, 255, 255, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 217, 159)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush) brush = QtGui.QBrush(QtGui.QColor(120, 120, 120)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrush(QtGui.QColor(0, 0, 0, 128)) brush.setStyle(QtCore.Qt.SolidPattern) palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush) self.xzwd.setPalette(palette) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(14) font.setBold(True) font.setWeight(75) self.xzwd.setFont(font) self.xzwd.setAutoFillBackground(True) self.xzwd.setAutoDefault(False) self.xzwd.setDefault(False) self.xzwd.setFlat(True) self.xzwd.setObjectName("xzwd") self.label_5 = QtWidgets.QLabel(self.frame_2) self.label_5.setGeometry(QtCore.QRect(30, 60, 131, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_5.setFont(font) self.label_5.setObjectName("label_5") self.textEdit = QtWidgets.QTextEdit(self.frame_2) self.textEdit.setGeometry(QtCore.QRect(50, 110, 61, 51)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.textEdit.setFont(font) self.textEdit.setObjectName("textEdit") self.label_6 = QtWidgets.QLabel(self.frame_2) self.label_6.setGeometry(QtCore.QRect(140, 130, 41, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_6.setFont(font) self.label_6.setObjectName("label_6") self.textEdit_2 = QtWidgets.QTextEdit(self.frame_2) self.textEdit_2.setGeometry(QtCore.QRect(200, 110, 61, 51)) font = QtGui.QFont() font.setFamily("微软雅黑") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.textEdit_2.setFont(font) self.textEdit_2.setObjectName("textEdit_2") self.label_7 = QtWidgets.QLabel(PdfC) self.label_7.setGeometry(QtCore.QRect(480, 250, 71, 21)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label_7.setFont(font) self.label_7.setObjectName("label_7") self.label_4 = QtWidgets.QLabel(PdfC) self.label_4.setGeometry(QtCore.QRect(270, 290, 461, 511)) self.label_4.setFrameShape(QtWidgets.QFrame.Box) self.label_4.setFrameShadow(QtWidgets.QFrame.Plain) self.label_4.setLineWidth(1) self.label_4.setText("") self.label_4.setObjectName("label_4") self.retranslateUi(PdfC) QtCore.QMetaObject.connectSlotsByName(PdfC) os.remove("return.png") def retranslateUi(self, PdfC): _translate = QtCore.QCoreApplication.translate PdfC.setWindowTitle(_translate("PdfC", "PDF拆分")) self.Reback.setText(_translate("PdfC", "...")) self.label.setText(_translate("PdfC", "PDF拆分")) self.scwd.setText(_translate("PdfC", "+ 上传文档")) self.label_2.setText(_translate("PdfC", "上传文档至此")) self.label_3.setText(_translate("PdfC", "操作")) self.xzwd.setText(_translate("PdfC", "开始拆分")) self.label_5.setText(_translate("PdfC", "输入页数(请勿输0)")) self.label_6.setText(_translate("PdfC", "到")) self.label_7.setText(_translate("PdfC", "PDF预览"))
class pdcwindow(QtWidgets.QWidget,Ui_PdfC): def __init__(self): super(pdcwindow, self).__init__() self.setupUi(self) def re_back(self): self.close() def sctp(self): fileName, fileType = QtWidgets.QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(),"自定义(*.pdf;*);;All Files(*)") self.f = str(fileName) self.p, self.s =self.f.split(".") try: if self.s == "pdf": # 打开文件 doc = fitz.open(self.f) # 读取一页 0代表第1页 page_one = doc.loadPage(0) # 将第一页转换为Pixmap page_pixmap = page_one.getPixmap() # 将Pixmap转换为QImage image_format = QImage.Format_RGBA8888 if page_pixmap.alpha else QImage.Format_RGB888 page_image = QImage(page_pixmap.samples, page_pixmap.width, page_pixmap.height, page_pixmap.stride,image_format) #width = page_image.width() #height = page_image.height() # QImage 转为QPixmap pix = QPixmap.fromImage(page_image) # 设置图片大小自适应标签 self.label_4.setScaledContents(True) # 给标签设置图像 self.label_4.setPixmap(pix) else: msg_box = QMessageBox(QMessageBox.Warning, "错误", "文件不是 pdf 文件,请重新选择!") msg_box.exec_() except Exception as e: pass def xzwj(self): pdf_file = PdfFileReader(self.f) output = PdfFileWriter() str1 = self.textEdit.toPlainText() num1 = int(str1) str2 = self.textEdit_2.toPlainText() num2 = int(str2) if num1 == num2: num1 = num1 - 1 for i in range(num1, num2): output.addPage(pdf_file.getPage(i)) # 写出pdf pdf_out = "{}_pdf拆分.{}".format(self.p,self.s) with open(pdf_out, 'ab') as out_pdf: output.write(out_pdf) root = tkinter.Tk() root.withdraw() tkinter.messagebox.showinfo("提示", "成功下载到源文件所在文件夹")
说明如下:
(1) def sctp(self): 其中的sctp为按钮的objectName属性名字;def xzwj(self):其中的xzwj为按钮的objectName属性名字;
(2) 可通过点击按钮实现本地文件上传;
fileName, fileType = QtWidgets. QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), "自定义(*.jpg;*.png;*.jpeg;*);;All Files(*)")
(3) 增加了防错功能,当上传的文档格式不是pdf时,将不执行程序,同时提示错误;
(4) 将pdf第一页转为图片,并用label组件显示;
(5) 在def xzwj(self):程序中可实现按页数拆分pdf;
if __name__ =="__main__": import sys app = QtWidgets.QApplication(sys.argv) win = mywindow() pc = pdcwindow() win.show() win.pdf_cf.clicked.connect(win.pic_main) win.pdf_cf.clicked.connect(pc.show) pc.Reback.clicked.connect(pc.re_back) pc.Reback.clicked.connect(win.show) pc.scwd.clicked.connect(pc.sctp) pc.xzwd.clicked.connect(pc.xzwj) sys.exit(app.exec_())
本文介绍的pdf拆分功能还有一些待完善地方,例如可增加预览功能,实现每页预览,以及如何实现多条件拆分。当然我们可以在后续阶段逐渐完善。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
相信大家对进度条一定不陌生了,很多安装或者下载都会出现进度条,本文主要介绍了Python实现实时显示进度条的6种方法,具有一定的参考价值,感兴趣的可以了解一下
这篇文章主要介绍了python中的函数嵌套和嵌套调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
大家好,本篇文章主要讲的是Python不同格式打印九九乘法表示例,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下哦,方便下次浏览
字符串截取,也叫字符串切片,使用方括号[ ]来截取字符串。字符串[开始索引:结束索引:步长];正索引代表从前往后索引,默认从0开始;负索引代表从后往前索引;字符串截取遵循“左闭右开”原则,也叫“包左不包右”。
python 的集合类型和 其他语言类似, 是一个无序不重复元素集,我在之前学过的其他的语言好像没有见过这个类型,基本功能包括关系测试和消
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008