<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
PyQt是Qt框架的Python語言實現,是最強大的GUI庫之一。PyQt提供了一個設計良好的視窗控制元件集合,具有更方便的操作性。學過VB的同學會知道,相比與VB的使用,在介面設計上元素更豐富。
本文就是基於PyQt5完成的pdf轉word功能,先看介面:
from PyQt5 import QtCore, QtGui, QtWidgets import base64 import os from return_png import img as retu # 引入img變數,賦別名為pdf_cf class Ui_PdfZ(object): def setupUi(self, PdfZ): tmp = open('return.png', 'wb') # 建立臨時的檔案 tmp.write(base64.b64decode(retu)) # 把這個one圖片解碼出來,寫入檔案中去。 tmp.close() PdfZ.setObjectName("PdfZ") PdfZ.resize(1400, 814) PdfZ.setMinimumSize(QtCore.QSize(1400, 814)) PdfZ.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) PdfZ.setPalette(palette) self.Reback = QtWidgets.QToolButton(PdfZ) 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(PdfZ) 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(PdfZ) 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(PdfZ) 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, 200, 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_4 = QtWidgets.QLabel(self.frame_2) self.label_4.setGeometry(QtCore.QRect(30, 70, 141, 21)) font = QtGui.QFont() font.setFamily("微軟雅黑") self.label_4.setFont(font) self.label_4.setObjectName("label_4") self.textEdit = QtWidgets.QTextEdit(self.frame_2) self.textEdit.setGeometry(QtCore.QRect(60, 110, 61, 41)) font = QtGui.QFont() font.setFamily("微軟雅黑") font.setPointSize(12) font.setBold(True) font.setWeight(75) self.textEdit.setFont(font) self.textEdit.setObjectName("textEdit") self.label_5 = QtWidgets.QLabel(self.frame_2) self.label_5.setGeometry(QtCore.QRect(150, 120, 31, 21)) font = QtGui.QFont() font.setFamily("微軟雅黑") self.label_5.setFont(font) self.label_5.setObjectName("label_5") self.textEdit_2 = QtWidgets.QTextEdit(self.frame_2) self.textEdit_2.setGeometry(QtCore.QRect(200, 110, 61, 41)) 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(PdfZ) self.label_7.setGeometry(QtCore.QRect(450, 250, 71, 21)) font = QtGui.QFont() font.setFamily("微軟雅黑") self.label_7.setFont(font) self.label_7.setObjectName("label_7") self.label_6 = QtWidgets.QLabel(PdfZ) self.label_6.setGeometry(QtCore.QRect(270, 290, 461, 511)) self.label_6.setFrameShape(QtWidgets.QFrame.Box) self.label_6.setFrameShadow(QtWidgets.QFrame.Plain) self.label_6.setLineWidth(1) self.label_6.setMidLineWidth(0) self.label_6.setText("") self.label_6.setObjectName("label_6") self.retranslateUi(PdfZ) QtCore.QMetaObject.connectSlotsByName(PdfZ) os.remove("return.png") def retranslateUi(self, 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預覽"))
class pdwwindow(QtWidgets.QWidget,Ui_PdfZ): def __init__(self): super(pdwwindow, 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_6.setScaledContents(True) # 給標籤設定影象 self.label_6.setPixmap(pix) else: msg_box = QMessageBox(QMessageBox.Warning,"錯誤", "檔案不是 pdf 檔案,請重新選擇!") msg_box.exec_() except Exception as e: pass def xzwj(self): docx_file = "{}_pdfZword.docx".format(self.p) str1 = self.textEdit.toPlainText() num1 = int(str1) str2 = self.textEdit_2.toPlainText() num2 = int(str2) if num1==num2: num1=num1-1 cv = Converter(self.f) cv.convert(docx_file, start=num1, end=num2) cv.close() 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):程式中可實現按頁數提取word;
(6) 藉助converter模組實現轉化word。
if __name__ =="__main__": import sys app = QtWidgets.QApplication(sys.argv) win = mywindow() pw = pdwwindow() win.show() win.pdf_word.clicked.connect(win.pic_main) win.pdf_word.clicked.connect(pw.show) pw.Reback.clicked.connect(pw.re_back) pw.Reback.clicked.connect(win.show) pw.scwd.clicked.connect(pw.sctp) pw.xzwd.clicked.connect(pw.xzwj) sys.exit(app.exec_())
本文介紹的pdf轉word功能還有一些待完善地方,例如可增加預覽功能,實現每頁預覽。當然我們可以在後續階段逐漸完善。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45