Python使用PyPDF2操作PDF的詳細(xì)教程
安裝PyPDF2
首先,你需要安裝PyPDF2庫。你可以使用pip來安裝它:
pip install PyPDF2
讀取PDF文件
讀取PDF文件并打印其頁數(shù):
import PyPDF2
# 使用PyPDF2讀取pdf文件
if __name__ == '__main__':
# 打開 PDF 文件
with open('example.pdf', 'rb') as file:
# 創(chuàng)建一個 PDF 閱讀器對象
reader = PyPDF2.PdfReader(file)
# 打印 PDF 文件中的頁數(shù)
print(f'Number of pages: {len(reader.pages)}')
提取文本
從PDF文件中提取文本:
import PyPDF2
# 使用PyPDF2讀取pdf文本
if __name__ == '__main__':
# 打開 PDF 文件
with open('example.pdf', 'rb') as file:
# 創(chuàng)建一個 PDF 閱讀器對象
reader = PyPDF2.PdfReader(file)
# 提取第一頁的文本
page = reader.pages[0]
text = page.extract_text()
print(text)
拆分PDF文件
將PDF文件拆分為兩個文件,一個文件包含奇數(shù)頁,一個文件包含偶數(shù)頁:
import PyPDF2
# 使用PyPDF2將PDF文件拆分為兩個文件,一個文件包含奇數(shù)頁,一個文件包含偶數(shù)頁:
if __name__ == '__main__':
# 創(chuàng)建一個 PDF 寫入器對象
odd_writer = PyPDF2.PdfWriter()
even_writer = PyPDF2.PdfWriter()
# 打開要拆分的 PDF 文件
with open('example.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
page_num = 0
for page in reader.pages:
# print(page_num)
if page_num % 2 == 0:
# print(reader.pages[page_num])
odd_writer.add_page(page)
else:
even_writer.add_page(page)
page_num = page_num+1
# 將拆分后的 PDF 寫入到一個新文件
with open('example1.pdf', 'wb') as output_pdf:
odd_writer.write(output_pdf)
# 將拆分后的 PDF 寫入到一個新文件
with open('example2.pdf', 'wb') as output_pdf:
even_writer.write(output_pdf)
合并PDF文件
你可以將多個PDF文件合并為一個:
import PyPDF2
# 使用PyPDF2合并兩個pdf文件
if __name__ == '__main__':
# 打開要合并的 PDF 文件
pdf_files = ['example1.pdf', 'example2.pdf']
# 創(chuàng)建一個 PDF 寫入器對象
merger = PyPDF2.PdfMerger()
for pdf_file in pdf_files:
merger.append(pdf_file)
# 將合并后的 PDF 寫入到一個新文件
with open('merged.pdf', 'wb') as output_pdf:
merger.write(output_pdf)
裁剪PDF頁面
PyPDF2并不直接支持裁剪頁面,但你可以通過提取頁面的部分內(nèi)容并創(chuàng)建一個新的頁面來實現(xiàn)這一功能。以下是一個簡單的示例,演示如何裁剪頁面的上半部分:
import PyPDF2
# 使用PyPDF2裁剪PDF頁面,通過提取頁面的部分內(nèi)容并創(chuàng)建一個新的頁面來實現(xiàn)這一功能
if __name__ == '__main__':
# 打開 PDF 文件
with open('example.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
# 創(chuàng)建一個 PDF 寫入器對象
writer = PyPDF2.PdfWriter()
# 獲取第一頁
page = reader.pages[0]
# 定義裁剪區(qū)域(左下角和右上角的坐標(biāo))
# 表示裁剪區(qū)域的左下角和右上角坐標(biāo)
cropbox = page.cropbox
x_lower_left = cropbox.lower_left[0]
y_lower_left = cropbox.lower_left[1] + (cropbox.upper_right[1] - cropbox.lower_left[1]) / 2
x_upper_right = cropbox.upper_right[0]
y_upper_right = cropbox.upper_right[1]
# 設(shè)置裁剪區(qū)域(左下x, 左下y, 右上x, 右上y)
page.cropbox.lower_left = (x_lower_left, y_lower_left)
page.cropbox.upper_right = (x_upper_right, y_upper_right)
writer.add_page(page)
# 將裁剪后的 PDF 寫入到一個新文件
with open('cropped_example.pdf', 'wb') as output_pdf:
writer.write(output_pdf)
加密PDF
from PyPDF2 import PdfWriter
from PyPDF2 import PdfReader
# 使用PyPDF2加密pdf
if __name__ == '__main__':
writer = PdfWriter()
with open('example.pdf', 'rb') as file:
reader = PdfReader(file)
writer.add_page(reader.pages[0])
# 設(shè)置加密(128位 AES)
writer.encrypt(
user_password="password123",
owner_password=None,
use_128bit=True
)
with open("encrypted.pdf", "wb") as out_file:
writer.write(out_file)
到此這篇關(guān)于Python使用PyPDF2操作PDF的詳細(xì)教程的文章就介紹到這了,更多相關(guān)Python PyPDF2操作PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用django搭建web開發(fā)環(huán)境
這篇文章主要為大家詳細(xì)介紹了Python使用django搭建web開發(fā)環(huán)境,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
python?Pandas之DataFrame索引及選取數(shù)據(jù)
這篇文章主要介紹了python?Pandas之DataFrame索引及選取數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
Anaconda最新版2023安裝教程Spyder安裝教程圖文詳解
這篇文章主要介紹了Anaconda最新版2023安裝教程Spyder安裝教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05
python使用socket高效傳輸視頻數(shù)據(jù)幀(連續(xù)發(fā)送圖片)
本文主要介紹了python使用socket高效傳輸視頻數(shù)據(jù)幀(連續(xù)發(fā)送圖片),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Pytorch中的backward()多個loss函數(shù)用法
這篇文章主要介紹了Pytorch中的backward()多個loss函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
如何用Python 實現(xiàn)全連接神經(jīng)網(wǎng)絡(luò)(Multi-layer Perceptron)
這篇文章主要介紹了如何用Python 實現(xiàn)全連接神經(jīng)網(wǎng)絡(luò)(Multi-layer Perceptron),幫助大家更好的進(jìn)行機器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10

