c/c++语言开发共享基于PyQt5完成的PDF拆分功能

pyqt是qt框架的python语言实现,是最强大的gui库之一。pyqt提供了一个设计良好的窗口控件集合,具有更方便的操作性。学过vb的同学会知道,相比与vb的使用,在界面设计上元素更丰富。本文就是

pyqt是qt框架的python语言实现,是最强大的gui库之一。pyqt提供了一个设计良好的窗口控件集合,具有更方便的操作性。学过vb的同学会知道,相比与vb的使用,在界面设计上元素更丰富。

c/c++开发分享基于PyQt5完成的PDF拆分功能就是基于pyqt5完成的pdf拆分功能,先看界面:

基于PyQt5完成的PDF拆分功能

 一、界面pdfc.ui可参考上文如何实现,程序代码如下:

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预览"))

二、main.py实现主要功能程序如下:

  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_())

四、总结

c/c++开发分享基于PyQt5完成的PDF拆分功能介绍的pdf拆分功能还有一些待完善地方,例如可增加预览功能,实现每页预览,以及如何实现多条件拆分。当然我们可以在后续阶段逐渐完善。

到此这篇关于基于pyqt5完成的pdf拆分功能的文章就介绍到这了,更多相关pyqt5 pdf拆分内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享基于PyQt5完成的PDF拆分功能,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1238031.html

(0)
上一篇 2022年9月11日
下一篇 2022年9月11日

精彩推荐