Python對(duì)PDF文件的常用操作方法詳解
工具
python3.7
Pycharm
PyPDF2
reportlab
從PDF中提取文本
PyPDF2沒有辦法從PDF文檔中提取圖像、圖表或其他媒體,但它可以提取文本,并將其返回為Python字符串。
import PyPDF2 reader = PyPDF2.PdfFileReader('test.pdf') page = reader.getPage(0) print(page.extractText())
旋轉(zhuǎn)和疊加頁面
上面的代碼中通過創(chuàng)建PdfFileReader對(duì)象的方式來讀取PDF文檔,該對(duì)象的getPage方法可以獲得PDF文檔的指定頁并得到一個(gè)PageObject對(duì)象,通過PageObject對(duì)象的rotateClockwise和rotateCounterClockwise方法可以實(shí)現(xiàn)頁面的順時(shí)針和逆時(shí)針方向旋轉(zhuǎn),通過PageObject對(duì)象的addBlankPage方法可以添加一個(gè)新的空白頁,代碼如下所示。
import PyPDF2 from PyPDF2.pdf import PageObject # 創(chuàng)建一個(gè)讀PDF文件的Reader對(duì)象 reader = PyPDF2.PdfFileReader('resources/xxx.pdf') # 創(chuàng)建一個(gè)寫PDF文件的Writer對(duì)象 writer = PyPDF2.PdfFileWriter() # 對(duì)PDF文件所有頁進(jìn)行循環(huán)遍歷 for page_num in range(reader.numPages): # 獲取指定頁碼的Page對(duì)象 current_page = reader.getPage(page_num) # type: PageObject if page_num % 2 == 0: # 奇數(shù)頁順時(shí)針旋轉(zhuǎn)90度 current_page.rotateClockwise(90) else: # 偶數(shù)頁反時(shí)針旋轉(zhuǎn)90度 current_page.rotateCounterClockwise(90) writer.addPage(current_page) # 最后添加一個(gè)空白頁并旋轉(zhuǎn)90度 page = writer.addBlankPage() # type: PageObject page.rotateClockwise(90) # 通過Writer對(duì)象的write方法將PDF寫入文件 with open('resources/xxx.pdf', 'wb') as file: writer.write(file)
加密PDF文件
使用PyPDF2中的PdfFileWrite對(duì)象可以為PDF文檔加密,如果需要給一系列的PDF文檔設(shè)置統(tǒng)一的訪問口令,使用Python程序來處理就會(huì)非常的方便。
import PyPDF2 reader = PyPDF2.PdfFileReader('resources/XGBoost.pdf') writer = PyPDF2.PdfFileWriter() for page_num in range(reader.numPages): writer.addPage(reader.getPage(page_num)) # 通過encrypt方法加密PDF文件,方法的參數(shù)就是設(shè)置的密碼 writer.encrypt('foobared') with open('resources/XGBoost-encrypted.pdf', 'wb') as file: writer.write(file)
創(chuàng)建PDF文件
創(chuàng)建PDF文檔需要三方庫reportlab的支持,使用 pip install reportlab 命令安裝
from reportlab.lib.pagesizes import A4 from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.pdfgen import canvas pdf_canvas = canvas.Canvas('resources/python創(chuàng)建.pdf', pagesize=A4) width, height = A4 # 繪圖 image = canvas.ImageReader('resources/xxx.jpg') pdf_canvas.drawImage(image, 20, height - 395, 250, 375) # 顯示當(dāng)前頁 pdf_canvas.showPage() # 注冊(cè)字體文件 pdfmetrics.registerFont(TTFont('Font1', 'resources/fonts/Vera.ttf')) pdfmetrics.registerFont(TTFont('Font2', 'resources/fonts/青呱石頭體.ttf')) # 寫字 pdf_canvas.setFont('Font2', 40) pdf_canvas.setFillColorRGB(0.9, 0.5, 0.3, 1) pdf_canvas.drawString(width // 2 - 120, height // 2, '你好,世界!') pdf_canvas.setFont('Font1', 40) pdf_canvas.setFillColorRGB(0, 1, 0, 0.5) pdf_canvas.rotate(18) pdf_canvas.drawString(250, 250, 'hello, world!') # 保存 pdf_canvas.save()
補(bǔ)充
合并PDF
from PyPDF2 import PdfFileReader, PdfFileWriter def merge_pdfs(paths, output): pdf_writer = PdfFileWriter() for path in paths: pdf_reader = PdfFileReader(path) for page in range(pdf_reader.getNumPages()): # 將每頁添加到writer對(duì)象 pdf_writer.addPage(pdf_reader.getPage(page)) # 寫入合并的pdf with open(output, 'wb') as out: pdf_writer.write(out) if __name__ == '__main__': paths = ['document1.pdf', 'document2.pdf'] merge_pdfs(paths, output='merged.pdf')
添加水印
from PyPDF2 import PdfFileWriter, PdfFileReader def create_watermark(input_pdf, output, watermark): watermark_obj = PdfFileReader(watermark) watermark_page = watermark_obj.getPage(0) pdf_reader = PdfFileReader(input_pdf) pdf_writer = PdfFileWriter() # 給所有頁面添加水印 for page in range(pdf_reader.getNumPages()): page = pdf_reader.getPage(page) page.mergePage(watermark_page) pdf_writer.addPage(page) with open(output, 'wb') as out: pdf_writer.write(out) if __name__ == '__main__': create_watermark( input_pdf='Jupyter_Notebook_An_Introduction.pdf', output='watermarked_notebook.pdf', watermark='watermark.pdf')
到此這篇關(guān)于Python對(duì)PDF文件的常用操作方法詳解的文章就介紹到這了,更多相關(guān)Python操作PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)PID溫控算法的示例代碼
PID算法是一種常用的控制算法,用于調(diào)節(jié)和穩(wěn)定控制系統(tǒng)的輸出,這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)pid溫控算法,需要的可以參考下2024-01-01pyinstaller打包python3.6和PyQt5中各種錯(cuò)誤的解決方案匯總
pyinstaller是打包python很方便的一個(gè)套件,我們可以很輕易地使用他,下面這篇文章主要給大家介紹了關(guān)于pyinstaller打包python3.6和PyQt5中各種錯(cuò)誤解決的相關(guān)資料,需要的朋友可以參考下2022-08-08Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解
這篇文章主要介紹了Python3中正則模塊re.compile、re.match及re.search函數(shù)用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了re模塊 中re.compile、re.match及re.search函數(shù)的功能、參數(shù)、具體使用技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06Python結(jié)合Selenium簡(jiǎn)單實(shí)現(xiàn)Web自動(dòng)化測(cè)試
這篇文章是入門級(jí)別的應(yīng)用Python + Selenium進(jìn)行自動(dòng)化測(cè)試,包括環(huán)境搭建及簡(jiǎn)單的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09pycharm遠(yuǎn)程開發(fā)項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要介紹了pycharm遠(yuǎn)程開發(fā)項(xiàng)目的實(shí)現(xiàn)步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01使用Python實(shí)現(xiàn)查找PDF中的指定文本并高亮顯示
在處理大量PDF文檔時(shí),有時(shí)我們需要快速找到特定的文本信息,本文將提供三個(gè)Python示例來幫助你在PDF文件中快速查找并高亮指定的文本,希望對(duì)大家有所幫助2024-03-03Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析
這篇文章主要介紹了Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09