欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中PDF解析利器pdfplumber的使用詳細教程

 更新時間:2025年04月04日 09:16:30   作者:aiweker  
pdfplumber是一個Python庫,專門用于從PDF文件中提取文本、表格和其他信息,本文將為大家詳細介紹一下pdfplumber的安裝與詳細使用,有需要的小伙伴可以參考下

1. 簡介與安裝

1.1 pdfplumber概述

pdfplumber是一個Python庫,專門用于從PDF文件中提取文本、表格和其他信息。相比其他PDF處理庫,pdfplumber提供了更直觀的API和更精確的文本定位能力。

主要特點:

  • 精確提取文本(包括位置、字體等信息)
  • 高效提取表格數(shù)據(jù)
  • 支持頁面級和文檔級的操作
  • 可視化調(diào)試功能

1.2 安裝方法

pip install pdfplumber

1.3 基礎使用示例

import pdfplumber

with pdfplumber.open("example.pdf") as pdf:
    first_page = pdf.pages[0]
    print(first_page.extract_text())

代碼解釋:

  • pdfplumber.open()打開PDF文件
  • pdf.pages獲取所有頁面的列表
  • extract_text()提取頁面文本內(nèi)容

2. 文本提取功能

2.1 基本文本提取

with pdfplumber.open("report.pdf") as pdf:
    for page in pdf.pages:
        print(page.extract_text())

應用場景:合同文本分析、報告內(nèi)容提取等

2.2 帶格式的文本提取

with pdfplumber.open("formatted.pdf") as pdf:
    page = pdf.pages[0]
    words = page.extract_words()
    for word in words:
        print(f"文本: {word['text']}, 位置: {word['x0'], word['top']}, 字體: {word['fontname']}")

輸出示例:

文本: 標題, 位置: (72.0, 84.0), 字體: Helvetica-Bold
文本: 內(nèi)容, 位置: (72.0, 96.0), 字體: Helvetica

2.3 按區(qū)域提取文本

with pdfplumber.open("document.pdf") as pdf:
    page = pdf.pages[0]
    # 定義區(qū)域(x0, top, x1, bottom)
    area = (50, 100, 400, 300)  
    cropped = page.crop(area)
    print(cropped.extract_text())

應用場景:提取發(fā)票中的特定信息、掃描件中的關鍵數(shù)據(jù)等

3. 表格提取功能

3.1 簡單表格提取

with pdfplumber.open("data.pdf") as pdf:
    page = pdf.pages[0]
    table = page.extract_table()
    for row in table:
        print(row)

輸出示例:

['姓名', '年齡', '職業(yè)']
['張三', '28', '工程師']
['李四', '32', '設計師']

3.2 復雜表格處理

with pdfplumber.open("complex_table.pdf") as pdf:
    page = pdf.pages[0]
    # 自定義表格設置
    table_settings = {
        "vertical_strategy": "text", 
        "horizontal_strategy": "text",
        "intersection_y_tolerance": 10
    }
    table = page.extract_table(table_settings)

參數(shù)說明:

  • vertical_strategy:垂直分割策略
  • horizontal_strategy:水平分割策略
  • intersection_y_tolerance:行合并容差

3.3 多頁表格處理

with pdfplumber.open("multi_page_table.pdf") as pdf:
    full_table = []
    for page in pdf.pages:
        table = page.extract_table()
        if table:
            # 跳過表頭(假設第一頁已經(jīng)有表頭)
            if page.page_number > 1:
                table = table[1:]
            full_table.extend(table)
    
    for row in full_table:
        print(row)

應用場景:財務報表分析、數(shù)據(jù)報表匯總等

4. 高級功能

4.1 可視化調(diào)試

with pdfplumber.open("debug.pdf") as pdf:
    page = pdf.pages[0]
    im = page.to_image()
    im.debug_tablefinder().show()

功能說明:

  • to_image()將頁面轉(zhuǎn)為圖像
  • debug_tablefinder()高亮顯示檢測到的表格
  • show()顯示圖像(需要安裝Pillow)

4.2 提取圖形元素

with pdfplumber.open("drawing.pdf") as pdf:
    page = pdf.pages[0]
    lines = page.lines
    curves = page.curves
    rects = page.rects
    
    print(f"找到 {len(lines)} 條直線")
    print(f"找到 {len(curves)} 條曲線")
    print(f"找到 {len(rects)} 個矩形")

應用場景:工程圖紙分析、設計文檔處理等

4.3 自定義提取策略

def custom_extract_method(page):
    # 獲取所有字符對象
    chars = page.chars
    # 按y坐標分組(行)
    lines = {}
    for char in chars:
        line_key = round(char["top"])
        if line_key not in lines:
            lines[line_key] = []
        lines[line_key].append(char)
    
    # 按x坐標排序并拼接文本
    result = []
    for y in sorted(lines.keys()):
        line_chars = sorted(lines[y], key=lambda c: c["x0"])
        line_text = "".join([c["text"] for c in line_chars])
        result.append(line_text)
    
    return "\n".join(result)

???????with pdfplumber.open("custom.pdf") as pdf:
    page = pdf.pages[0]
    print(custom_extract_method(page))

應用場景:處理特殊格式的PDF文檔

5. 性能優(yōu)化技巧

5.1 按需加載頁面

with pdfplumber.open("large.pdf") as pdf:
    # 只處理前5頁
    for page in pdf.pages[:5]:
        process(page.extract_text())

5.2 并行處理

from concurrent.futures import ThreadPoolExecutor

def process_page(page):
    return page.extract_text()

with pdfplumber.open("big_file.pdf") as pdf:
    with ThreadPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(process_page, pdf.pages))

5.3 緩存處理結(jié)果

import pickle

def extract_and_cache(pdf_path, cache_path):
    try:
        with open(cache_path, "rb") as f:
            return pickle.load(f)
    except FileNotFoundError:
        with pdfplumber.open(pdf_path) as pdf:
            data = [page.extract_text() for page in pdf.pages]
            with open(cache_path, "wb") as f:
                pickle.dump(data, f)
            return data

text_data = extract_and_cache("report.pdf", "report_cache.pkl")

6. 實際應用案例

6.1 發(fā)票信息提取系統(tǒng)

def extract_invoice_info(pdf_path):
    invoice_data = {
        "invoice_no": None,
        "date": None,
        "total": None
    }
    
    with pdfplumber.open(pdf_path) as pdf:
        for page in pdf.pages:
            text = page.extract_text()
            lines = text.split("\n")
            
            for line in lines:
                if "發(fā)票號碼" in line:
                    invoice_data["invoice_no"] = line.split(":")[1].strip()
                elif "日期" in line:
                    invoice_data["date"] = line.split(":")[1].strip()
                elif "合計" in line:
                    invoice_data["total"] = line.split()[-1]
    
    return invoice_data

6.2 學術論文分析

def analyze_paper(pdf_path):
    sections = {
        "abstract": "",
        "introduction": "",
        "conclusion": ""
    }
    
    with pdfplumber.open(pdf_path) as pdf:
        current_section = None
        for page in pdf.pages:
            text = page.extract_text()
            for line in text.split("\n"):
                line = line.strip()
                if line.lower() == "abstract":
                    current_section = "abstract"
                elif line.lower().startswith("1. introduction"):
                    current_section = "introduction"
                elif line.lower().startswith("conclusion"):
                    current_section = "conclusion"
                elif current_section:
                    sections[current_section] += line + "\n"
    
    return sections

6.3 財務報表轉(zhuǎn)換

import csv

def convert_pdf_to_csv(pdf_path, csv_path):
    with pdfplumber.open(pdf_path) as pdf:
        with open(csv_path, "w", newline="") as f:
            writer = csv.writer(f)
            for page in pdf.pages:
                table = page.extract_table()
                if table:
                    writer.writerows(table)

7. 常見問題與解決方案

7.1 中文亂碼問題

with pdfplumber.open("chinese.pdf") as pdf:
    page = pdf.pages[0]
    # 確保系統(tǒng)安裝了中文字體
    text = page.extract_text()
    print(text.encode("utf-8").decode("utf-8"))

解決方案:

  • 確保系統(tǒng)安裝了正確的字體
  • 檢查Python環(huán)境編碼設置
  • 使用支持中文的PDF解析器參數(shù)

7.2 表格識別不準確

table_settings = {
    "vertical_strategy": "lines",
    "horizontal_strategy": "lines",
    "explicit_vertical_lines": page.lines,
    "explicit_horizontal_lines": page.lines,
    "intersection_x_tolerance": 15,
    "intersection_y_tolerance": 15
}
table = page.extract_table(table_settings)

調(diào)整策略:

  • 嘗試不同的分割策略
  • 調(diào)整容差參數(shù)
  • 使用可視化調(diào)試工具

7.3 大文件處理內(nèi)存不足

# 逐頁處理并立即釋放內(nèi)存
with pdfplumber.open("huge.pdf") as pdf:
    for i, page in enumerate(pdf.pages):
        process(page.extract_text())
        # 手動釋放頁面資源
        pdf.release_resources()
        if i % 10 == 0:
            print(f"已處理 {i+1} 頁")

8. 總結(jié)與最佳實踐

8.1 pdfplumber核心優(yōu)勢

  • 精確的文本定位:保留文本在頁面中的位置信息
  • 強大的表格提?。禾幚韽碗s表格結(jié)構能力突出
  • 豐富的元數(shù)據(jù):提供字體、大小等格式信息
  • 可視化調(diào)試:直觀驗證解析結(jié)果
  • 靈活的API:支持自定義提取邏輯

8.2 適用場景推薦

優(yōu)先選擇pdfplumber:

  • 需要精確文本位置信息的應用
  • 復雜PDF表格數(shù)據(jù)提取
  • 需要分析PDF格式和排版的場景

考慮其他方案:

  • 僅需簡單文本提取(可考慮PyPDF2)
  • 需要編輯PDF(考慮PyMuPDF)
  • 超大PDF文件處理(考慮分頁處理)

8.3 最佳實踐建議

預處理PDF文件:

# 使用Ghostscript優(yōu)化PDF
import subprocess
subprocess.run(["gs", "-sDEVICE=pdfwrite", "-dNOPAUSE", "-dBATCH", 
               "-dSAFER", "-sOutputFile=optimized.pdf", "original.pdf"])

組合使用多種工具:

# 結(jié)合PyMuPDF獲取更精確的文本位置
import fitz
doc = fitz.open("combined.pdf")

建立錯誤處理機制:

def safe_extract(pdf_path):
    try:
        with pdfplumber.open(pdf_path) as pdf:
            return pdf.pages[0].extract_text()
    except Exception as e:
        print(f"處理{pdf_path}時出錯: {str(e)}")
        return None

性能監(jiān)控:

import time
start = time.time()
# pdf處理操作
print(f"處理耗時: {time.time()-start:.2f}秒")

pdfplumber是Python生態(tài)中最強大的PDF解析庫之一,特別適合需要精確提取文本和表格數(shù)據(jù)的應用場景。通過合理使用其豐富的功能和靈活的API,可以解決大多數(shù)PDF處理需求。對于特殊需求,結(jié)合其他PDF處理工具和自定義邏輯,能夠構建出高效可靠的PDF處理流程。

到此這篇關于Python中PDF解析利器pdfplumber的使用詳細教程的文章就介紹到這了,更多相關Python pdfplumber內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python 快速實現(xiàn)CLI 應用程序的腳手架

    Python 快速實現(xiàn)CLI 應用程序的腳手架

    本篇文章主要介紹了Python 快速實現(xiàn)CLI 應用程序的腳手架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • python中argparse模塊及action='store_true'詳解

    python中argparse模塊及action='store_true'詳解

    argparse?是一個用來解析命令行參數(shù)的?Python?庫,它是?Python?標準庫的一部分,這篇文章主要介紹了python中argparse模塊及action=‘store_true‘詳解,需要的朋友可以參考下
    2023-02-02
  • 對python條件表達式的四種實現(xiàn)方法小結(jié)

    對python條件表達式的四種實現(xiàn)方法小結(jié)

    今天小編就為大家分享一篇對python條件表達式的四種實現(xiàn)方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python使用Rich?type和TinyDB構建聯(lián)系人通訊錄

    Python使用Rich?type和TinyDB構建聯(lián)系人通訊錄

    這篇文章主要為大家介紹了Python使用Rich?type和TinyDB構建聯(lián)系人通訊錄應用程序,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • python集合的創(chuàng)建、添加及刪除操作示例

    python集合的創(chuàng)建、添加及刪除操作示例

    這篇文章主要介紹了python集合的創(chuàng)建、添加及刪除操作,結(jié)合實例形式分析了Python集合的概念、功能及針對集合的創(chuàng)建、添加與刪除等相關操作實現(xiàn)技巧,需要的朋友可以參考下
    2019-10-10
  • 詳解Python給照片換底色(藍底換紅底)

    詳解Python給照片換底色(藍底換紅底)

    這篇文章主要介紹了詳解Python給照片換底色(藍底換紅底),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • 完美解決Python 2.7不能正常使用pip install的問題

    完美解決Python 2.7不能正常使用pip install的問題

    今天小編就為大家分享一篇完美解決Python 2.7不能正常使用pip install的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python中的特殊語法:filter、map、reduce、lambda介紹

    Python中的特殊語法:filter、map、reduce、lambda介紹

    這篇文章主要介紹了Python中的特殊語法:filter、map、reduce、lambda介紹,本文分別對這個特殊語法給出了代碼實例,需要的朋友可以參考下
    2015-04-04
  • 簡單介紹Python的輕便web框架Bottle

    簡單介紹Python的輕便web框架Bottle

    這篇文章主要介紹了Python的輕便web框架Bottle,因其注重輕便的設計,與Flask一樣,Bottle框架的人氣同樣也非常高,需要的朋友可以參考下
    2015-04-04
  • Python進程Multiprocessing模塊原理解析

    Python進程Multiprocessing模塊原理解析

    這篇文章主要介紹了Python進程Multiprocessing模塊原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02

最新評論