Python自制一個(gè)PDF轉(zhuǎn)PNG圖片小工具
使用PyQt5應(yīng)用程序制作PDF轉(zhuǎn)換成圖片的小工具,可以導(dǎo)入PDF文檔后一鍵生成對(duì)應(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)換工具?公眾號(hào):Python 集中營(yíng)') ????????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)圖片存儲(chǔ)路徑') ????????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)存儲(chǔ)路徑不能為空!') ????????????????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文件對(duì)象!') ????????????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ò)誤,請(qǐng)檢查參數(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的資料請(qǐng)關(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)簡(jiǎn)單成績(jī)錄入系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單成績(jī)錄入系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09python請(qǐng)求域名requests.(url = 地址)報(bào)錯(cuò)
本文主要介紹了python請(qǐng)求域名requests.(url = 地址)報(bào)錯(cuò),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python使用OpenCV和K-Means聚類對(duì)畢業(yè)照進(jìn)行圖像分割
圖像分割是將圖像分割成多個(gè)不同區(qū)域(或片段)的過程。目標(biāo)是將圖像的表示變成更容易和更有意義的圖像。在這篇博客中,我們?cè)敿?xì)的介紹了使用方法,感興趣的可以了解一下2021-06-06Python私有pypi源注冊(cè)自定義依賴包Windows詳解
這篇文章主要介紹了Python私有pypi源注冊(cè)自定義依賴包Windows,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11python程序主動(dòng)退出進(jìn)程的五種方式
對(duì)于如何結(jié)束一個(gè)Python程序或者用Python操作去結(jié)束一個(gè)進(jìn)程等,Python本身給出了好幾種方法,而這些方式也存在著一些區(qū)別,對(duì)相關(guān)的幾種方法看了并實(shí)踐了下,同時(shí)也記錄下,需要的朋友可以參考下2024-02-02