本篇内容介绍了“基于PyQt5如何完成pdf转word功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
PyQt是Qt框架的Python语言实现,是最强大的GUI库之一。PyQt提供了一个设计良好的窗口控件集合,具有更方便的操作性。学过VB的同学会知道,相比与VB的使用,在界面设计上元素更丰富。
先看界面:
一、界面PdfZ.ui可参考之前的文章如何实现,程序代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets import base64 import os from return_png import img as retu # 引入img变量,赋别名为pdf_cf class Ui_PdfZobject): def setupUiself, PdfZ): tmp = open'return.png', 'wb') # 创建临时的文件 tmp.writebase64.b64decoderetu)) # 把这个one图片解码出来,写入文件中去。 tmp.close) PdfZ.setObjectName"PdfZ") PdfZ.resize1400, 814) PdfZ.setMinimumSizeQtCore.QSize1400, 814)) PdfZ.setMaximumSizeQtCore.QSize1400, 814)) palette = QtGui.QPalette) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) PdfZ.setPalettepalette) self.Reback = QtWidgets.QToolButtonPdfZ) self.Reback.setGeometryQtCore.QRect10, 10, 51, 41)) palette = QtGui.QPalette) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush) self.Reback.setPalettepalette) icon = QtGui.QIcon) icon.addPixmapQtGui.QPixmap"return.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off) self.Reback.setIconicon) self.Reback.setIconSizeQtCore.QSize100, 100)) self.Reback.setAutoRaiseTrue) self.Reback.setObjectName"Reback") self.label = QtWidgets.QLabelPdfZ) self.label.setGeometryQtCore.QRect88, 8, 171, 41)) font = QtGui.QFont) font.setFamily"微软雅黑") font.setPointSize16) font.setBoldTrue) font.setWeight75) self.label.setFontfont) self.label.setObjectName"label") self.frame = QtWidgets.QFramePdfZ) self.frame.setGeometryQtCore.QRect20, 60, 1351, 161)) palette = QtGui.QPalette) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.frame.setPalettepalette) self.frame.setAutoFillBackgroundTrue) self.frame.setFrameShapeQtWidgets.QFrame.Box) self.frame.setFrameShadowQtWidgets.QFrame.Plain) self.frame.setLineWidth1) self.frame.setMidLineWidth0) self.frame.setObjectName"frame") self.scwd = QtWidgets.QPushButtonself.frame) self.scwd.setGeometryQtCore.QRect570, 30, 191, 61)) palette = QtGui.QPalette) brush = QtGui.QBrushQtGui.QColor0, 217, 159)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Text, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255, 128)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrushQtGui.QColor0, 217, 159)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Text, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255, 128)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrushQtGui.QColor0, 217, 159)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor120, 120, 120)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Text, brush) brush = QtGui.QBrushQtGui.QColor120, 120, 120)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrushQtGui.QColor0, 0, 0, 128)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush) self.scwd.setPalettepalette) font = QtGui.QFont) font.setFamily"微软雅黑") font.setPointSize16) font.setBoldTrue) font.setWeight75) self.scwd.setFontfont) self.scwd.setAutoFillBackgroundTrue) self.scwd.setAutoDefaultFalse) self.scwd.setDefaultFalse) self.scwd.setFlatTrue) self.scwd.setObjectName"scwd") self.label_2 = QtWidgets.QLabelself.frame) self.label_2.setGeometryQtCore.QRect620, 100, 131, 21)) font = QtGui.QFont) font.setFamily"微软雅黑") self.label_2.setFontfont) self.label_2.setObjectName"label_2") self.frame_2 = QtWidgets.QFramePdfZ) self.frame_2.setGeometryQtCore.QRect1050, 280, 321, 301)) palette = QtGui.QPalette) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Window, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Window, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Base, brush) brush = QtGui.QBrushQtGui.QColor245, 255, 250)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Window, brush) self.frame_2.setPalettepalette) self.frame_2.setAutoFillBackgroundTrue) self.frame_2.setFrameShapeQtWidgets.QFrame.NoFrame) self.frame_2.setFrameShadowQtWidgets.QFrame.Plain) self.frame_2.setLineWidth1) self.frame_2.setMidLineWidth0) self.frame_2.setObjectName"frame_2") self.label_3 = QtWidgets.QLabelself.frame_2) self.label_3.setGeometryQtCore.QRect20, 20, 191, 31)) font = QtGui.QFont) font.setFamily"微软雅黑") font.setPointSize14) font.setBoldFalse) font.setWeight50) self.label_3.setFontfont) self.label_3.setObjectName"label_3") self.xzwd = QtWidgets.QPushButtonself.frame_2) self.xzwd.setGeometryQtCore.QRect30, 200, 271, 61)) palette = QtGui.QPalette) brush = QtGui.QBrushQtGui.QColor0, 217, 159)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.Text, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255, 128)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Active, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrushQtGui.QColor0, 217, 159)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.Text, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrushQtGui.QColor255, 255, 255, 128)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Inactive, QtGui.QPalette.PlaceholderText, brush) brush = QtGui.QBrushQtGui.QColor0, 217, 159)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Button, brush) brush = QtGui.QBrushQtGui.QColor120, 120, 120)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.Text, brush) brush = QtGui.QBrushQtGui.QColor120, 120, 120)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush) brush = QtGui.QBrushQtGui.QColor0, 0, 0, 128)) brush.setStyleQtCore.Qt.SolidPattern) palette.setBrushQtGui.QPalette.Disabled, QtGui.QPalette.PlaceholderText, brush) self.xzwd.setPalettepalette) font = QtGui.QFont) font.setFamily"微软雅黑") font.setPointSize14) font.setBoldTrue) font.setWeight75) self.xzwd.setFontfont) self.xzwd.setAutoFillBackgroundTrue) self.xzwd.setAutoDefaultFalse) self.xzwd.setDefaultFalse) self.xzwd.setFlatTrue) self.xzwd.setObjectName"xzwd") self.label_4 = QtWidgets.QLabelself.frame_2) self.label_4.setGeometryQtCore.QRect30, 70, 141, 21)) font = QtGui.QFont) font.setFamily"微软雅黑") self.label_4.setFontfont) self.label_4.setObjectName"label_4") self.textEdit = QtWidgets.QTextEditself.frame_2) self.textEdit.setGeometryQtCore.QRect60, 110, 61, 41)) font = QtGui.QFont) font.setFamily"微软雅黑") font.setPointSize12) font.setBoldTrue) font.setWeight75) self.textEdit.setFontfont) self.textEdit.setObjectName"textEdit") self.label_5 = QtWidgets.QLabelself.frame_2) self.label_5.setGeometryQtCore.QRect150, 120, 31, 21)) font = QtGui.QFont) font.setFamily"微软雅黑") self.label_5.setFontfont) self.label_5.setObjectName"label_5") self.textEdit_2 = QtWidgets.QTextEditself.frame_2) self.textEdit_2.setGeometryQtCore.QRect200, 110, 61, 41)) font = QtGui.QFont) font.setFamily"微软雅黑") font.setPointSize12) font.setBoldTrue) font.setWeight75) self.textEdit_2.setFontfont) self.textEdit_2.setObjectName"textEdit_2") self.label_7 = QtWidgets.QLabelPdfZ) self.label_7.setGeometryQtCore.QRect450, 250, 71, 21)) font = QtGui.QFont) font.setFamily"微软雅黑") self.label_7.setFontfont) self.label_7.setObjectName"label_7") self.label_6 = QtWidgets.QLabelPdfZ) self.label_6.setGeometryQtCore.QRect270, 290, 461, 511)) self.label_6.setFrameShapeQtWidgets.QFrame.Box) self.label_6.setFrameShadowQtWidgets.QFrame.Plain) self.label_6.setLineWidth1) self.label_6.setMidLineWidth0) self.label_6.setText"") self.label_6.setObjectName"label_6") self.retranslateUiPdfZ) QtCore.QMetaObject.connectSlotsByNamePdfZ) os.remove"return.png") def retranslateUiself, PdfZ): _translate = QtCore.QCoreApplication.translate PdfZ.setWindowTitle_translate"PdfZ", "PDFZWORD")) self.Reback.setText_translate"PdfZ", "...")) self.label.setText_translate"PdfZ", "PDFZWORD")) self.scwd.setText_translate"PdfZ", "+ 上传文档")) self.label_2.setText_translate"PdfZ", "上传文档至此")) self.label_3.setText_translate"PdfZ", "操作")) self.xzwd.setText_translate"PdfZ", "开始转换")) self.label_4.setText_translate"PdfZ", "页码选择请勿输0):")) self.label_5.setText_translate"PdfZ", "到")) self.label_7.setText_translate"PdfZ", "PDF预览"))
二、main.py实现主要功能程序如下:
class pdwwindowQtWidgets.QWidget,Ui_PdfZ): def __init__self): superpdwwindow, self).__init__) self.setupUiself) def re_backself): self.close) def sctpself): fileName, fileType = QtWidgets.QFileDialog.getOpenFileNameself, "选取文件", os.getcwd),"自定义*.pdf;*);;All Files*)") self.f = strfileName) self.p, self.s =self.f.split".") try: if self.s == "pdf": # 打开文件 doc = fitz.openself.f) # 读取一页 0代表第1页 page_one = doc.loadPage0) # 将第一页转换为Pixmap page_pixmap = page_one.getPixmap) # 将Pixmap转换为QImage image_format = QImage.Format_RGBA8888 if page_pixmap.alpha else QImage.Format_RGB888 page_image = QImagepage_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.fromImagepage_image) # 设置图片大小自适应标签 self.label_6.setScaledContentsTrue) # 给标签设置图像 self.label_6.setPixmappix) else: msg_box = QMessageBoxQMessageBox.Warning,"错误", "文件不是 pdf 文件,请重新选择!") msg_box.exec_) except Exception as e: pass def xzwjself): docx_file = "{}_pdfZword.docx".formatself.p) str1 = self.textEdit.toPlainText) num1 = intstr1) str2 = self.textEdit_2.toPlainText) num2 = intstr2) if num1==num2: num1=num1-1 cv = Converterself.f) cv.convertdocx_file, start=num1, end=num2) cv.close) root = tkinter.Tk) root.withdraw) tkinter.messagebox.showinfo"提示", "成功下载到源文件所在文件夹")
说明如下:
1) def sctpself): 其中的sctp为按钮的objectName属性名字;def xzwjself):其中的xzwj为按钮的objectName属性名字;
2) 可通过点击按钮实现本地文件上传;
fileName, fileType = QtWidgets. QFileDialog.getOpenFileNameself, "选取文件", os.getcwd), "自定义*.jpg;*.png;*.jpeg;*);;All Files*)")
3) 增加了防错功能,当上传的文档格式不是pdf时,将不执行程序,同时提示错误;
4) 将pdf第一页转为图片,并用label组件显示;
5) 在def xzwjself):程序中可实现按页数提取word;
6) 借助converter模块实现转化word。
三、最后在主程序中加入以上子程序。
if __name__ =="__main__": import sys app = QtWidgets.QApplicationsys.argv) win = mywindow) pw = pdwwindow) win.show) win.pdf_word.clicked.connectwin.pic_main) win.pdf_word.clicked.connectpw.show) pw.Reback.clicked.connectpw.re_back) pw.Reback.clicked.connectwin.show) pw.scwd.clicked.connectpw.sctp) pw.xzwd.clicked.connectpw.xzwj) sys.exitapp.exec_))