Python自制一個(gè)PDF轉(zhuǎn)PNG圖片小工具
使用PyQt5應(yīng)用程序制作PDF轉(zhuǎn)換成圖片的小工具,可以導(dǎo)入PDF文檔后一鍵生成對應(yīng)的PNG圖片。
PDF圖片轉(zhuǎn)換小工具使用的中間件:
- python版本:3.6.8
- UI應(yīng)用版本:PyQt5
- PDF文件操作非標(biāo)準(zhǔn)庫:PyPDF2
- PNG圖片生成庫:PyMuPDF
pip?install?PyQt5 pip?install?PyPDF2 pip?install?PyMuPDF==1.18.17
將需要使用到的python標(biāo)準(zhǔn)庫或非標(biāo)準(zhǔn)庫全部導(dǎo)入到我們的代碼塊中進(jìn)入開發(fā)環(huán)節(jié)。
#?Importing?all?the?classes?from?the?PyQt5.QtGui?module. from?PyQt5.QtGui?import?* #?Importing?all?the?classes?from?the?PyQt5.QtCore?module. from?PyQt5.QtCore?import?* #?Importing?all?the?classes?from?the?PyQt5.QtWidgets?module. from?PyQt5.QtWidgets?import?* #?Importing?the?`fitz`?module. import?fitz #?Importing?the?PyPDF2?module. import?PyPDF2 #?Importing?the?`sys`?module. import?sys #?Importing?the?os?module. import?os #?Importing?the?traceback?module. import?traceback
接下來直接進(jìn)入正題,首先創(chuàng)建名稱為PdfToPngUI的python類,將UI組件及布局和相關(guān)的槽函數(shù)都寫入到這個(gè)類中。
#?This?class?is?a?widget?that?contains?a?button?and?a?text?box.?When?the?button?is?clicked,?the?text?box?is?populated?with
#?the?path?to?the?converted?file
class?PdfToPngUI(QWidget):
????def?__init__(self):
????????"""
????????A?constructor.?It?is?called?when?an?object?is?created?from?a?class?and?it?allows?the?class?to?initialize?the
????????attributes?of?a?class.
????????"""
????????super(PdfToPngUI,?self).__init__()
????????self.init_ui()
????def?init_ui(self):
????????"""
????????This?function?initializes?the?UI.
????????"""
????????self.setWindowTitle('PDF圖片轉(zhuǎn)換工具?公眾號:Python 集中營')
????????self.setWindowIcon(QIcon('analysis.ico'))
????????self.resize(600,?400)
????????self.source_pdf_path?=?QLineEdit()
????????self.source_pdf_path.setPlaceholderText('PDF文件路徑')
????????self.source_pdf_path.setReadOnly(True)
????????self.source_pdf_btn?=?QPushButton()
????????self.source_pdf_btn.setText('導(dǎo)入')
????????self.source_pdf_btn.clicked.connect(self.source_pdf_btn_click)
????????self.target_png_path?=?QLineEdit()
????????self.target_png_path.setPlaceholderText('目標(biāo)圖片存儲路徑')
????????self.target_png_path.setReadOnly(True)
????????self.target_png_btn?=?QPushButton()
????????self.target_png_btn.setText('路徑')
????????self.target_png_btn.clicked.connect(self.target_png_btn_click)
????????self.start_btn?=?QPushButton()
????????self.start_btn.setText('PDF一鍵生成PNG圖片')
????????self.start_btn.clicked.connect(self.start_btn_click)
????????self.brower?=?QTextBrowser()
????????self.brower.setReadOnly(True)
????????self.brower.setFont(QFont('宋體',?8))
????????self.brower.setPlaceholderText('日志處理過程區(qū)域...')
????????self.brower.ensureCursorVisible()
????????grid?=?QGridLayout()
????????grid.addWidget(self.source_pdf_path,?0,?0,?1,?2)
????????grid.addWidget(self.source_pdf_btn,?0,?2,?1,?1)
????????grid.addWidget(self.target_png_path,?1,?0,?1,?2)
????????grid.addWidget(self.target_png_btn,?1,?2,?1,?1)
????????grid.addWidget(self.start_btn,?2,?0,?1,?3)
????????grid.addWidget(self.brower,?3,?0,?1,?3)
????????self.pdf_thread?=?WorkThread(self)
????????self.pdf_thread.message.connect(self.show_message)
????????self.pdf_thread.finished.connect(self.finished)
????????self.setLayout(grid)
????def?show_message(self,?text):
????????"""
????????It?shows?a?message
????????:param?text:?The?text?to?be?displayed
????????"""
????????cursor?=?self.brower.textCursor()
????????cursor.movePosition(QTextCursor.End)
????????self.brower.append(text)
????????self.brower.setTextCursor(cursor)
????????self.brower.ensureCursorVisible()
????def?source_pdf_btn_click(self):
????????"""
????????It?opens?a?file?dialog?box?to?select?the?source?PDF?file.
????????"""
????????source_pdf_path?=?QFileDialog.getOpenFileName(self,?"選取文件",?os.getcwd(),?"PDF?File?(*.pdf)")
????????self.source_pdf_path.setText(source_pdf_path[0])
????def?target_png_btn_click(self):
????????"""
????????A?function?that?is?called?when?the?target_png_btn?is?clicked.
????????"""
????????target_png_path?=?QFileDialog.getExistingDirectory(self,?'選擇文件夾',?os.getcwd())
????????self.target_png_path.setText(target_png_path)
????def?start_btn_click(self):
????????"""
????????A?function?that?is?called?when?the?start?button?is?clicked.
????????"""
????????self.pdf_thread.start()
????????self.start_btn.setEnabled(False)
????def?finished(self,?finished):
????????"""
????????A?function?that?is?called?when?the?target_png_btn?is?clicked
????????"""
????????if?finished?is?True:
????????????self.start_btn.setEnabled(True)
通過上面的PdfToPngUI類處理,這個(gè)時(shí)候UI組件及布局和槽函數(shù)已經(jīng)開發(fā)完成了,應(yīng)用的頁面效果如下。

然后,我們開始業(yè)務(wù)邏輯的開發(fā)。這里將業(yè)務(wù)邏輯使用單獨(dú)的子線程開發(fā)避免和頁面的主線程發(fā)生阻塞。
創(chuàng)建一個(gè)子線程的python類WorkThread并繼承自QThread子線程,將PDF圖片轉(zhuǎn)換的過程寫到里面。
#?It's?a?QThread?that?runs?a?function?in?a?separate?thread
class?WorkThread(QThread):
????message?=?pyqtSignal(str)
????finished?=?pyqtSignal(bool)
????def?__init__(self,?parent=None):
????????"""
????????A?constructor?that?initializes?the?class.
????????:param?parent:?The?parent?widget
????????"""
????????super(WorkThread,?self).__init__(parent)
????????self.working?=?True
????????self.parent?=?parent
????def?__del__(self):
????????"""
????????A?destructor.?It?is?called?when?the?object?is?destroyed.
????????"""
????????self.working?=?False
????def?run(self):
????????"""
??????? PDF轉(zhuǎn)換圖片的業(yè)務(wù)函數(shù)。
????????"""
????????try:
????????????source_pdf_path?=?self.parent.source_pdf_path.text().strip()
????????????target_png_path?=?self.parent.target_png_path.text().strip()
????????????if?source_pdf_path?==?''?or?target_png_path?==?'':
????????????????self.message.emit('來源文件路徑或目標(biāo)存儲路徑不能為空!')
????????????????self.finished.emit(True)
????????????????return
????????????self.message.emit('源文件路徑:{}'.format(source_pdf_path))
????????????self.message.emit('目標(biāo)文件路徑:{}'.format(target_png_path))
????????????pdf_?=?fitz.open(source_pdf_path)
????????????self.message.emit('成功打開PDF文件對象!')
????????????reader?=?PyPDF2.PdfFileReader(source_pdf_path)
????????????self.message.emit('PDF文件流處理完成!')
????????????page_num?=?reader.getNumPages()
????????????self.message.emit('PDF文件頁數(shù)讀取完成!')
????????????for?n?in?range(0,?page_num):
????????????????page?=?pdf_.load_page(n)
????????????????pix_?=?page.get_pixmap()
????????????????pix_.save(os.path.join(target_png_path,?str(n)?+?'.png'))
????????????????self.message.emit('圖片保存成功:{}'.format(os.path.join(target_png_path,?str(n)?+?'.png')))
????????????self.message.emit('PNG圖片全部轉(zhuǎn)換完成!')
????????????self.finished.emit(True)
????????except:
????????????traceback.print_exc()
????????????self.message.emit('程序運(yùn)行出現(xiàn)錯(cuò)誤,請檢查參數(shù)是否設(shè)置正確!')
????????????self.finished.emit(True)
經(jīng)過上述的UI界面組件以及業(yè)務(wù)線程的開發(fā),功能已經(jīng)實(shí)現(xiàn)了,下面使用main函數(shù)調(diào)起整個(gè)應(yīng)用就OK了。
if?__name__?==?'__main__': ????app?=?QApplication(sys.argv) ????main?=?PdfToPngUI() ????main.show() ????sys.exit(app.exec_())

以上就是Python自制一個(gè)PDF轉(zhuǎn)PNG圖片小工具的詳細(xì)內(nèi)容,更多關(guān)于Python PDF轉(zhuǎn)PNG的資料請關(guān)注腳本之家其它相關(guān)文章!
- 詳解python實(shí)現(xiàn)多張多格式圖片轉(zhuǎn)PDF并打包成exe
- Python自動(dòng)化辦公之圖片轉(zhuǎn)PDF的實(shí)現(xiàn)
- Python實(shí)現(xiàn)文字pdf轉(zhuǎn)換圖片pdf效果
- Python把圖片轉(zhuǎn)化為pdf代碼實(shí)例
- 利用python將圖片版PDF轉(zhuǎn)文字版PDF
- Python 將pdf轉(zhuǎn)成圖片的方法
- windows下Python實(shí)現(xiàn)將pdf文件轉(zhuǎn)化為png格式圖片的方法
- 用python 制作圖片轉(zhuǎn)pdf工具
- Python的pdfplumber庫將pdf轉(zhuǎn)為圖片的實(shí)現(xiàn)
相關(guān)文章
Python中實(shí)現(xiàn)高效的列表過濾多種方法示例
這篇文章主要給大家介紹了關(guān)于Python中實(shí)現(xiàn)高效的列表過濾的多種方法,包括基礎(chǔ)的for循環(huán)、列表推導(dǎo)式、filter函數(shù)、itertools模塊,以及高級的pandas和numpy庫,我們還討論了生成器的使用,以及在實(shí)際場景中的應(yīng)用,需要的朋友可以參考下2024-12-12
python tensorflow基于cnn實(shí)現(xiàn)手寫數(shù)字識別
這篇文章主要為大家詳細(xì)介紹了python tensorflow基于cnn實(shí)現(xiàn)手寫數(shù)字識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
Python 執(zhí)行矩陣與線性代數(shù)運(yùn)算
這篇文章主要介紹了Python 執(zhí)行矩陣與線性代數(shù)運(yùn)算,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08
keras小技巧——獲取某一個(gè)網(wǎng)絡(luò)層的輸出方式
這篇文章主要介紹了keras小技巧——獲取某一個(gè)網(wǎng)絡(luò)層的輸出方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
使用Django啟動(dòng)命令行及執(zhí)行腳本的方法
今天小編就為大家分享一篇使用Django啟動(dòng)命令行及執(zhí)行腳本的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
詳解numpy矩陣的創(chuàng)建與數(shù)據(jù)類型
這篇文章主要介紹了詳解numpy矩陣的創(chuàng)建與數(shù)據(jù)類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
解決python ThreadPoolExecutor 線程池中的異常捕獲問題
這篇文章主要介紹了解決python ThreadPoolExecutor 線程池中的異常捕獲問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04

