使用Python進(jìn)行PDF文檔處理的常見操作
引言
使用 Python 進(jìn)行 PDF 文檔處理可以通過多種庫來實(shí)現(xiàn),包括 PyPDF2、pdfplumber、reportlab、pdfminer 等。這些庫可以處理不同的 PDF 任務(wù),例如 提取文本、拆分合并 PDF、修改 PDF、生成 PDF 等。以下是幾種常見操作及對應(yīng)的庫和代碼示例。
1. 安裝常用庫
首先,安裝常用的 PDF 處理庫:
pip install PyPDF2 pdfplumber reportlab
2. 提取 PDF 文本
PyPDF2 和 pdfplumber 都可以用于提取 PDF 文本。PyPDF2 更輕量,但有時(shí)處理復(fù)雜的 PDF 格式效果較差,而 pdfplumber 更加適合處理表格等復(fù)雜結(jié)構(gòu)的 PDF。
使用 PyPDF2 提取文本
import PyPDF2
# 打開 PDF 文件
with open('sample.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
# 提取每一頁的文本
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
text = page.extract_text()
print(f"第 {page_num + 1} 頁的文本:\n{text}")
使用 pdfplumber 提取文本
pdfplumber 更適合處理結(jié)構(gòu)化數(shù)據(jù),尤其是表格。
import pdfplumber
# 打開 PDF 文件
with pdfplumber.open('sample.pdf') as pdf:
for page_num in range(len(pdf.pages)):
page = pdf.pages[page_num]
text = page.extract_text()
print(f"第 {page_num + 1} 頁的文本:\n{text}")
3. 合并與拆分 PDF 文件
使用 PyPDF2 合并 PDF 文件
可以將多個(gè) PDF 文件合并為一個(gè)文件。
import PyPDF2
pdf_files = ['file1.pdf', 'file2.pdf', 'file3.pdf']
merger = PyPDF2.PdfMerger()
for pdf in pdf_files:
merger.append(pdf)
# 保存合并后的 PDF 文件
with open('merged_output.pdf', 'wb') as output_file:
merger.write(output_file)
使用 PyPDF2 拆分 PDF 文件
將 PDF 文件拆分為單獨(dú)的頁面。
import PyPDF2
# 打開 PDF 文件
with open('sample.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
# 按頁拆分并保存
for page_num in range(len(reader.pages)):
writer = PyPDF2.PdfWriter()
writer.add_page(reader.pages[page_num])
with open(f'split_page_{page_num + 1}.pdf', 'wb') as output_file:
writer.write(output_file)
4. 創(chuàng)建和修改 PDF
使用 ReportLab 創(chuàng)建 PDF 文件
reportlab 是一個(gè)功能強(qiáng)大的庫,可以用來生成新的 PDF 文件,支持插入文本、圖片、圖形等。
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
# 創(chuàng)建 PDF 文件
c = canvas.Canvas("output.pdf", pagesize=letter)
c.drawString(100, 750, "Hello, this is a PDF created with ReportLab!")
# 創(chuàng)建矩形
c.rect(100, 700, 400, 100)
# 保存 PDF
c.showPage()
c.save()
使用 ReportLab 插入圖片
你可以使用 reportlab 插入圖片到 PDF 中。
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas("output_with_image.pdf", pagesize=letter)
c.drawString(100, 750, "This is a PDF with an image:")
# 插入圖片
c.drawImage("image.png", 100, 600, width=200, height=150)
c.showPage()
c.save()
5. 提取 PDF 表格
pdfplumber 提供了提取 PDF 中表格的功能,非常適合處理含有表格的文檔。
import pdfplumber
# 打開 PDF 文件
with pdfplumber.open('table_sample.pdf') as pdf:
first_page = pdf.pages[0]
# 提取表格數(shù)據(jù)
tables = first_page.extract_table()
# 打印提取到的表格數(shù)據(jù)
for row in tables:
print(row)
6. PDF 文檔加密與解密
使用 PyPDF2 加密 PDF 文件
你可以加密 PDF 文件,防止未經(jīng)授權(quán)的訪問。
import PyPDF2
# 打開 PDF 文件
with open('sample.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
writer = PyPDF2.PdfWriter()
for page_num in range(len(reader.pages)):
writer.add_page(reader.pages[page_num])
# 設(shè)置密碼
writer.encrypt(user_password='user123', owner_password='owner123')
with open('encrypted_output.pdf', 'wb') as output_file:
writer.write(output_file)
使用 PyPDF2 解密 PDF 文件
如果 PDF 文件已加密,解密并提取文本的方法如下:
import PyPDF2
# 打開加密的 PDF 文件
with open('encrypted_output.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
# 提供密碼
reader.decrypt('user123')
# 提取文本
for page_num in range(len(reader.pages)):
page = reader.pages[page_num]
print(page.extract_text())
7. PDF 頁面旋轉(zhuǎn)
你可以旋轉(zhuǎn) PDF 的某些頁面,以下是旋轉(zhuǎn)頁面的示例:
import PyPDF2
# 打開 PDF 文件
with open('sample.pdf', 'rb') as file:
reader = PyPDF2.PdfReader(file)
writer = PyPDF2.PdfWriter()
# 旋轉(zhuǎn)每頁 90 度
for page in reader.pages:
page.rotate_clockwise(90)
writer.add_page(page)
# 保存旋轉(zhuǎn)后的 PDF 文件
with open('rotated_output.pdf', 'wb') as output_file:
writer.write(output_file)
總結(jié)
Python 提供了多個(gè)強(qiáng)大的庫來處理 PDF 文檔。根據(jù)具體需求,選擇適合的庫來完成任務(wù):
- PyPDF2:適合基本的 PDF 操作,如合并、拆分、加密、旋轉(zhuǎn)等。
- pdfplumber:適合復(fù)雜的文本和表格提取。
- reportlab:用于生成和修改 PDF 文件,支持文本、圖像和圖形的繪制。
通過這些工具,你可以輕松地處理 PDF 文檔的各種操作,從文本提取到生成和修改文檔。
到此這篇關(guān)于使用Python進(jìn)行PDF文檔處理的常見操作的文章就介紹到這了,更多相關(guān)Python PDF文檔處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch dataloader 取batch_size時(shí)候出現(xiàn)bug的解決方式
今天小編就為大家分享一篇pytorch dataloader 取batch_size時(shí)候出現(xiàn)bug的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python神經(jīng)網(wǎng)絡(luò)Pytorch中Tensorboard函數(shù)使用
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)Pytorch中Tensorboard常用函數(shù)的使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python求兩個(gè)時(shí)間的時(shí)間差(實(shí)例代碼)
我們在用python進(jìn)行分析的時(shí)候,可能會碰到計(jì)算兩個(gè)日期的時(shí)間差。下面為大家介紹一下如何計(jì)算兩個(gè)時(shí)間的時(shí)間差,需要的朋友可以參考下2022-11-11
基于Python編寫一個(gè)根據(jù)姓名測性別的小程序
這篇文章主要為大家介紹了如何利用Python編寫一款根據(jù)中文名能猜測性別的一款界面化的小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03
Python 靜態(tài)導(dǎo)入與動態(tài)導(dǎo)入的實(shí)現(xiàn)示例
Python靜態(tài)導(dǎo)入和動態(tài)導(dǎo)入是指導(dǎo)入模塊或模塊內(nèi)部函數(shù)的兩種方式,本文主要介紹了Python 靜態(tài)導(dǎo)入與動態(tài)導(dǎo)入的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05

