Python主流处理PDF的库总结对比(包含详细示例)

库名主要用途主要功能安装命令
PyPDF2操作和合并PDF拆分、合并、旋转、裁剪PDF页面,提取文本和元数据pip install PyPDF2
pdfminer.six提取PDF文本高精度提取文本、图像和表格,支持复杂布局和字体pip install pdfminer.six
ReportLab生成PDF文件创建包含文本、图像、图形和表格的复杂PDF文档pip install reportlab
PyMuPDF读取和操作PDF提取文本和图像,处理页面、注释和书签,渲染PDF页面pip install PyMuPDF
pdfplumber提取表格和文本高精度提取和分析PDF中的表格和文本pip install pdfplumber
Camelot提取PDF表格高精度表格检测和提取,导出为CSV、Excel、JSON等格式pip install camelot-py[cv]
tabula-py提取PDF表格基于Java库tabula,提取表格为DataFrame或CSVpip install tabula-py
Slate提取PDF文本基于pdfminer的简单文本提取工具pip install slate
pdfquery高级文本提取结合PDFMiner和lxml,支持复杂查询和文本提取pip install pdfquery
PDFKitHTML转PDF将HTML文档转换为PDF,基于wkhtmltopdf工具pip install pdfkit
pdf2imagePDF转图像使用poppler将PDF页面转换为PIL图像对象pip install pdf2image

详细示例

1. PyPDF2

用途: 操作和合并PDF

import PyPDF2

# 读取PDF文件
with open('sample.pdf', 'rb') as file:
    reader = PyPDF2.PdfFileReader(file)
    # 提取第一页文本
    page = reader.getPage(0)
    print(page.extract_text())

# 合并PDF文件
merger = PyPDF2.PdfFileMerger()
merger.append('sample1.pdf')
merger.append('sample2.pdf')
merger.write('merged.pdf')

2. pdfminer.six

用途: 提取PDF文本

from pdfminer.high_level import extract_text

# 提取文本
text = extract_text('sample.pdf')
print(text)

3. ReportLab

用途: 生成PDF文件

from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

# 创建PDF
c = canvas.Canvas('generated.pdf', pagesize=letter)
c.drawString(100, 750, 'Hello, World!')
c.save()

4. PyMuPDF (fitz)

用途: 读取和操作PDF

import fitz

# 读取PDF文件
document = fitz.open('sample.pdf')
page = document.load_page(0)
text = page.get_text()
print(text)

# 提取图像
for img in document.get_page_images(0):
    xref = img[0]
    pix = fitz.Pixmap(document, xref)
    if pix.n < 5:  # this is GRAY or RGB
        pix.save(f'image_{xref}.png')
    else:  # CMYK: convert to RGB first
        pix1 = fitz.Pixmap(fitz.csRGB, pix)
        pix1.save(f'image_{xref}.png')
        pix1 = None
    pix = None

5. pdfplumber

用途: 提取表格和文本

import pdfplumber

# 读取PDF文件
with pdfplumber.open('sample.pdf') as pdf:
    page = pdf.pages[0]
    text = page.extract_text()
    print(text)

    # 提取表格
    tables = page.extract_tables()
    for table in tables:
        for row in table:
            print(row)

6. Camelot

用途: 提取PDF表格

import camelot

# 提取表格
tables = camelot.read_pdf('sample.pdf', pages='1')
tables[0].to_csv('table.csv')

7. tabula-py

用途: 提取PDF表格

import tabula

# 提取表格
df = tabula.read_pdf('sample.pdf', pages='1')
df.to_csv('table.csv', index=False)

# 批量提取表格
tabula.convert_into('sample.pdf', 'tables.csv', output_format='csv', pages='all')

8. Slate

用途: 提取PDF文本

import slate

# 读取PDF文件
with open('sample.pdf', 'rb') as file:
    doc = slate.PDF(file)
    for page in doc:
        print(page)

9. pdfquery

用途: 高级文本提取

import pdfquery

# 读取PDF文件
pdf = pdfquery.PDFQuery('sample.pdf')
pdf.load(0)
text = pdf.pq('LTTextLineHorizontal:contains("text_to_find")').text()
print(text)

10. PDFKit

用途: HTML转PDF

import pdfkit

# HTML转PDF
pdfkit.from_url('http://example.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello, World!', 'out.pdf')

11. pdf2image

用途: PDF转图像

from pdf2image import convert_from_path

# PDF转图像
images = convert_from_path('sample.pdf')
for i, image in enumerate(images):
    image.save(f'page_{i+1}.png', 'PNG')

  • 9
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值