关于 pdf-extraction
pdf-extraction 是使用 pdfplumber 从 PDF 中提取文本、表格和元数据的技能,适用于需要按页面、位置或表格结构处理文档的任务;扫描版 PDF 需先进行 OCR。
主要功能
- 01提取 PDF 页面文本
- 02检测并提取表格
- 03获取字符位置
- 04读取文档元数据
- 05裁剪并筛选页面区域
- 06可视化调试检测对象
适用场景
- 01提取财务报告中的表格
- 02获取指定页面的文本
- 03查找发票总额
- 04将 PDF 表格转换为 CSV 或 Excel
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ claude-office-skills/ skills --skill pdf-extraction安装后在 Claude Code 中输入/pdf-extraction确认已加载。
详细介绍
PDF 提取技能
概述
此技能使用 pdfplumber(用于 PDF 数据提取的首选库),帮助精准提取 PDF 文档中的文本、表格和元数据。与基础 PDF 阅读器不同,pdfplumber 能提供详细的字符级定位、准确的表格检测和可视化调试。
使用方法
- 提供要从中提取内容的 PDF 文件
- 指定所需内容:文本、表格、图像或元数据
- 我将生成 pdfplumber 代码并执行
提示词示例:
- “从这份财务报告中提取所有表格”
- “获取此文档第 5–10 页的文本”
- “在此 PDF 中查找并提取发票总额”
- “将此 PDF 表格转换为 CSV/Excel”
领域知识
pdfplumber 基础
import pdfplumber
# Open PDF
with pdfplumber.open('document.pdf') as pdf:
# Access pages
first_page = pdf.pages[0]
# Document metadata
print(pdf.metadata)
# Number of pages
print(len(pdf.pages))
PDF 结构
PDF Document
├── metadata (title, author, creation date)
├── pages[]
│ ├── chars (individual characters with position)
│ ├── words (grouped characters)
│ ├── lines (horizontal/vertical lines)
│ ├── rects (rectangles)
│ ├── curves (bezier curves)
│ └── images (embedded images)
└── outline (bookmarks/TOC)
文本提取
基本文本
with pdfplumber.open('document.pdf') as pdf:
# Single page
text = pdf.pages[0].extract_text()
# All pages
full_text = ''
for page in pdf.pages:
full_text += page.extract_text() or ''
高级文本选项
# With layout preservation
text = page.extract_text(
x_tolerance=3, # Horizontal tolerance for grouping
y_tolerance=3, # Vertical tolerance
layout=True, # Preserve layout
x_density=7.25, # Chars per unit width
y_density=13 # Chars per unit height
)
# Extract words with positions
words = page.extract_words(
x_tolerance=3,
y_tolerance=3,
keep_blank_chars=False,
use_text_flow=False
)
# Each word includes: text, x0, top, x1, bottom, etc.
for word in words:
print(f"{word['text']} at ({word['x0']}, {word['top']})")
字符级访问
# Get all characters
chars = page.chars
for char in chars:
print(f"'{char['text']}' at ({char['x0']}, {char['top']})")
print(f" Font: {char['fontname']}, Size: {char['size']}")
表格提取
基本表格提取
with pdfplumber.open('report.pdf') as pdf:
page = pdf.pages[0]
# Extract all tables
tables = page.extract_tables()
for i, table in enumerate(tables):
print(f"Table {i+1}:")
for row in table:
print(row)
高级表格设置
# Custom table detection
table_settings = {
"vertical_strategy": "lines", # or "text", "explicit"
"horizontal_strategy": "lines",
"explicit_vertical_lines": [], # Custom line positions
"explicit_horizontal_lines": [],
"snap_tolerance": 3,
"snap_x_tolerance": 3,
"snap_y_tolerance": 3,
"join_tolerance": 3,
"edge_min_length": 3,
"min_words_vertical": 3,
"min_words_horizontal": 1,
"intersection_tolerance": 3,
"text_tolerance": 3,
"text_x_tolerance": 3,
"text_y_tolerance": 3,
}
tables = page.extract_tables(table_settings)
查找表格
# Find tables (without extracting)
table_finder = page.find_tables()
for table in table_finder:
print(f"Table at: {table.bbox}") # (x0, top, x1, bottom)
# Extract specific table
data = table.extract()
可视化调试
# Create visual debug image
im = page.to_image(resolution=150)
# Draw detected objects
im.draw_rects(page.chars) # Character bounding boxes
im.draw_rects(page.words) # Word bounding boxes
im.draw_lines(page.lines) # Lines
im.draw_rects(page.rects) # Rectangles
# Save debug image
im.save('debug.png')
# Debug tables
im.reset()
im.debug_tablefinder()
im.save('table_debug.png')
裁剪和筛选
裁剪至区域
# Define bounding box (x0, top, x1, bottom)
bbox = (0, 0, 300, 200)
# Crop page
cropped = page.crop(bbox)
# Extract from cropped area
text = cropped.extract_text()
tables = cropped.extract_tables()
按位置筛选
# Filter characters by region
def within_bbox(obj, bbox):
x0, top, x1, bottom = bbox
return (obj['x0'] >= x0 and obj['x1'] <= x1 and
obj['top'] >= top and obj['bottom'] <= bottom)
bbox = (100, 100, 400, 300)
filtered_chars = [c for c in page.chars if within_bbox(c, bbox)]
按字体筛选
# Get text by font
def extract_by_font(page, font_name):
chars = [c for c in page.chars if font_name in c['fontname']]
return ''.join(c['text'] for c in chars)
# Extract bold text (often "Bold" in font name)
bold_text = extract_by_font(page, 'Bold')
# Extract by size
large_chars = [c for c in page.chars if c['size'] > 14]
元数据和结构
with pdfplumber.open('document.pdf') as pdf:
# Document metadata
meta = pdf.metadata
print(f"Title: {meta.get('Title')}")
print(f"Author: {meta.get('Author')}")
print(f"Created: {meta.get('CreationDate')}")
# Page info
for i, page in enumerate(pdf.pages):
print(f"Page {i+1}: {page.width} x {page.height}")
print(f" Rotation: {page.rotation}")
最佳实践
- 可视化调试:使用
to_image()了解 PDF 结构 - 调整表格设置:根据具体 PDF 调整容差
- 处理扫描版 PDF:先使用 OCR(此技能适用于原生文本)
- 逐页处理:对于大型 PDF,避免一次性全部加载
- 检查文本:某些 PDF 是图像;请确认其中存在文本
常见模式
将所有表格提取到 DataFrame
import pandas as pd
def pdf_tables_to_dataframes(pdf_path):
"""Extract all tables from PDF as pandas DataFrames."""
dfs = []
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages):
tables = page.extract_tables()
for j, table in enumerate(tables):
if table and len(table) > 1:
# First row as header
df = pd.DataFrame(table[1:], columns=table[0])
df['_page'] = i + 1
df['_table'] = j + 1
dfs.append(df)
return dfs
提取特定区域
def extract_invoice_amount(pdf_path):
"""Extract amount from typical invoice layout."""
with pdfplumber.open(pdf_path) as pdf:
page = pdf.pages[0]
# Search for "Total" and get nearby numbers
words = page.extract_words()
for i, word in enumerate(words):
if 'total' in word['text'].lower():
# Look at next few words
for next_word in words[i+1:i+5]:
text = next_word['text'].replace(',', '').replace('$', '')
try:
return float(text)
except ValueError:
continue
return None
多栏布局
def extract_columns(page, num_columns=2):
"""Extract text from multi-column layout."""
width = page.width
col_width = width / num_columns
columns = []
for i in range(num_columns):
x0 = i * col_width
x1 = (i + 1) * col_width
cropped = page.crop((x0, 0, x1, page.height))
columns.append(cropped.extract_text())
return columns
示例
示例 1:提取财务报告表格
import pdfplumber
import pandas as pd
def extract_financial_tables(pdf_path):
"""Extract tables from financial report and save to Excel."""
with pdfplumber.open(pdf_path) as pdf:
all_tables = []
for page_num, page in enumerate(pdf.pages):
# Debug: save table visualization
im = page.to_image()
im.debug_tablefinder()
im.save(f'debug_page_{page_num+1}.png')
# Extract tables
tables = page.extract_tables({
"vertical_strategy": "lines",
"horizontal_strategy": "lines",
"snap_tolerance": 5,
})
for table in tables:
if table and len(table) > 1:
# Clean data
clean_table = []
for row in table:
clean_row = [cell.strip() if cell else '' for cell in row]
clean_table.append(clean_row)
df = pd.DataFrame(clean_table[1:], columns=clean_table[0])
df['Source Page'] = page_num + 1
all_tables.append(df)
# Save to Excel with multiple sheets
with pd.ExcelWriter('extracted_tables.xlsx') as writer:
for i, df in enumerate(all_tables):
df.to_excel(writer, sheet_name=f'Table_{i+1}', index=False)
return all_tables
tables = extract_financial_tables('annual_report.pdf')
print(f"Extracted {len(tables)} tables")
示例 2:提取发票数据
import pdfplumber
import re
from datetime import datetime
def extract_invoice_data(pdf_path):
"""Extract structured data from invoice PDF."""
data = {
'invoice_number': None,
'date': None,
'total': None,
'line_items': []
}
with pdfplumber.open(pdf_path) as pdf:
page = pdf.pages[0]
text = page.extract_text()
# Extract invoice number
inv_match = re.search(r'Invoice\s*#?\s*:?\s*(\w+)', text, re.IGNORECASE)
if inv_match:
data['invoice_number'] = inv_match.group(1)
# Extract date
date_match = re.search(r'Date\s*:?\s*(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})', text)
if date_match:
data['date'] = date_match.group(1)
# Extract total
total_match = re.search(r'Total\s*:?\s*\$?([\d,]+\.?\d*)', text, re.IGNORECASE)
if total_match:
data['total'] = float(total_match.group(1).replace(',', ''))
# Extract line items from table
tables = page.extract_tables()
for table in tables:
if table and any('description' in str(row).lower() for row in table[:2]):
# Found line items table
for row in table[1:]: # Skip header
if row and len(row) >= 3:
data['line_items'].append({
'description': row[0],
'quantity': row[1] if len(row) > 1 else None,
'amount': row[-1]
})
return data
invoice = extract_invoice_data('invoice.pdf')
print(f"Invoice #{invoice['invoice_number']}")
print(f"Total: ${invoice['total']}")
示例 3:简历解析器
import pdfplumber
def parse_resume(pdf_path):
"""Extract structured sections from resume."""
with pdfplumber.open(pdf_path) as pdf:
full_text = ''
for page in pdf.pages:
full_text += (page.extract_text() or '') + '\n'
# Common resume sections
sections = {
'contact': '',
'summary': '',
'experience': '',
'education': '',
'skills': ''
}
# Split by common headers
import re
section_patterns = {
'summary': r'(summary|objective|profile)',
'experience': r'(experience|employment|work history)',
'education': r'(education|academic)',
'skills': r'(skills|competencies|technical)'
}
lines = full_text.split('\n')
current_section = 'contact'
for line in lines:
line_lower = line.lower().strip()
# Check if line is a section header
for section, pattern in section_patterns.items():
if re.match(pattern, line_lower):
current_section = section
break
sections[current_section] += line + '\n'
return sections
resume = parse_resume('resume.pdf')
print("Skills:", resume['skills'])
限制
- 无法从扫描版/图像型 PDF 中提取内容(请先使用 OCR)
- 复杂布局可能需要手动调整
- 不支持某些 PDF 加密类型
- 嵌入字体可能会影响文本提取
- 不具备直接编辑 PDF 的功能
安装
pip install pdfplumber
# For image debugging (optional)
pip install Pillow
资源
评论
支持 Markdown