关于 Pdf
Pdf 是一项 PDF 处理指南技能,面向需要创建、读取或修改 PDF 的工作。它按任务路由到 Python 库和命令行工具,涵盖从文本与表格提取到 OCR 和表单填写。
主要功能
- 01创建、合并与拆分 PDF
- 02提取文本、表格和元数据
- 03扫描文档 OCR
- 04填写表单及添加水印
- 05旋转页面、加密和提取图像
适用场景
- 01从报告 PDF 提取表格数据
- 02合并合同或拆分页面范围
- 03将扫描文档转换为文本
- 04根据数据批量填写 PDF 表单
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ danielmiessler/ lifeos --skill pdf安装后在 Claude Code 中输入/pdf确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSE.txtforms.mdreference.mdScripts/check_bounding_boxes.pycheck_bounding_boxes_test.pycheck_fillable_fields.pyfill_pdf_form_with_annotations.pycreate_validation_image.pyconvert_pdf_to_images.pyextract_form_field_info.pyfill_fillable_fields.py详细介绍
PDF 处理指南
🎯 加载完整 PAI 上下文
使用此技能开始任何任务前,请加载完整的 PAI 上下文:
read ~/.claude/PAI/SKILL.md
这将提供以下内容:
- 完整的联系人列表(Angela、Bunny、Saša、Greg、团队成员)
- 技术栈偏好(TypeScript>Python、bun>npm、uv>pip)
- 安全规则和代码库安全协议
- 响应格式要求(结构化表情符号格式)
- Agent 路由的语音 ID(ElevenLabs)
- 个人偏好和操作说明
何时启用此技能
PDF 任务直接触发条件
- 用户想要创建新的 PDF 文档
- 用户想要合并多个 PDF,或将多个 PDF 组合、串联
- 用户想要将 PDF 拆分或分隔成单独的页面/章节
- 用户提到“从 PDF 提取文本”“PDF 文本提取”
- 用户提到“从 PDF 提取表格”“PDF 表格”
- 用户想要“填写 PDF 表单”“PDF 表单填写”
- 用户提到“OCR”“扫描版 PDF”或“扫描件转文本”
- 用户想要添加水印、密码保护或加密
- 用户想要从 PDF 中提取图像
- 用户想要旋转页面或操作 PDF 结构
上下文触发条件
- 用户提供了用于处理的 .pdf 文件路径
- 用户提到表单填写自动化或批量 PDF 处理
- 用户需要以编程方式大规模处理 PDF
🔀 PDF 工作流路由
此技能支持多种 PDF 处理工作流:
创建工作流
触发词:“创建 PDF”“生成 PDF”“制作 PDF”“从数据生成 PDF”
**工具:**reportlab(Python) **文档:**第 136-181 行(SKILL.md)
使用场景:
- 从头创建新的 PDF
- 以编程方式生成报告
- 包含文本和图形的多页文档
- 根据模板或数据生成 PDF
合并/拆分工作流
触发词:“合并 PDF”“组合 PDF”“拆分 PDF”“分隔页面”
**工具:**pypdf(Python)、qpdf(CLI) **文档:**第 46-68 行(SKILL.md)、第 199-211 行(qpdf)
使用场景:
- 将多个 PDF 合并成一个文档
- 将 PDF 拆分成单独页面或页面范围
- 重新排列 PDF 页面顺序
- 提取特定页面范围
文本提取工作流
触发词:“提取文本”“PDF 转文本”“读取 PDF 内容”
**工具:**pdfplumber(Python)、pdftotext(CLI) **文档:**第 95-103 行(pdfplumber)、第 186-196 行(pdftotext)
使用场景:
- 在保留版式的同时提取文本
- 将 PDF 转换为纯文本
- 从多个 PDF 批量提取文本
- 提取元数据
表格提取工作流
触发词:“提取表格”“PDF 表格”“从 PDF 提取表格数据”
**工具:**pdfplumber + pandas(Python) **文档:**第 106-133 行(SKILL.md)
使用场景:
- 将结构化表格数据提取到 Excel/CSV
- 从 PDF 报告中提取财务数据
- 将 PDF 表格转换为数据框
- 提取并合并多个表格
表单填写工作流
触发词:“填写 PDF 表单”“PDF 表单填写”“完成 PDF 表单”
**工具:**pdf-lib(JavaScript)或 pypdf(Python) **文档:**forms.md(完整指南)
使用场景:
- 以编程方式填写表单
- 批量处理表单
- 基于模板生成 PDF
- 从数据源填充表单字段
OCR 工作流
触发词:“OCR”“扫描版 PDF”“从扫描件提取文本”“图像转文本”
**工具:**pytesseract + pdf2image(Python) **文档:**第 227-244 行(SKILL.md)
使用场景:
- 从扫描文档中提取文本
- 处理基于图像的 PDF
- 将扫描表单转换为可编辑文本
- 将旧版文档数字化
操作工作流
触发词:“水印”“密码保护”“加密 PDF”“旋转页面”“提取图像”
**工具:**pypdf(Python)、pdfimages(CLI) **文档:**第 246-288 行(SKILL.md)
使用场景:
- 为 PDF 添加水印
- 密码保护和加密
- 页面旋转和变换
- 从 PDF 中提取图像
概述
本指南介绍使用 Python 库和命令行工具进行 PDF 处理的基本操作。有关高级功能、JavaScript 库和详细示例,请参阅 reference.md。如果需要填写 PDF 表单,请阅读 forms.md 并按照其中的说明操作。
快速入门
from pypdf import PdfReader, PdfWriter
# Read a PDF
reader = PdfReader("document.pdf")
print(f"Pages: {len(reader.pages)}")
# Extract text
text = ""
for page in reader.pages:
text += page.extract_text()
Python 库
pypdf - 基本操作
合并 PDF
from pypdf import PdfWriter, PdfReader
writer = PdfWriter()
for pdf_file in ["doc1.pdf", "doc2.pdf", "doc3.pdf"]:
reader = PdfReader(pdf_file)
for page in reader.pages:
writer.add_page(page)
with open("merged.pdf", "wb") as output:
writer.write(output)
拆分 PDF
reader = PdfReader("input.pdf")
for i, page in enumerate(reader.pages):
writer = PdfWriter()
writer.add_page(page)
with open(f"page_{i+1}.pdf", "wb") as output:
writer.write(output)
提取元数据
reader = PdfReader("document.pdf")
meta = reader.metadata
print(f"Title: {meta.title}")
print(f"Author: {meta.author}")
print(f"Subject: {meta.subject}")
print(f"Creator: {meta.creator}")
旋转页面
reader = PdfReader("input.pdf")
writer = PdfWriter()
page = reader.pages[0]
page.rotate(90) # Rotate 90 degrees clockwise
writer.add_page(page)
with open("rotated.pdf", "wb") as output:
writer.write(output)
pdfplumber - 文本和表格提取
按布局提取文本
import pdfplumber
with pdfplumber.open("document.pdf") as pdf:
for page in pdf.pages:
text = page.extract_text()
print(text)
提取表格
with pdfplumber.open("document.pdf") as pdf:
for i, page in enumerate(pdf.pages):
tables = page.extract_tables()
for j, table in enumerate(tables):
print(f"Table {j+1} on page {i+1}:")
for row in table:
print(row)
高级表格提取
import pandas as pd
with pdfplumber.open("document.pdf") as pdf:
all_tables = []
for page in pdf.pages:
tables = page.extract_tables()
for table in tables:
if table: # Check if table is not empty
df = pd.DataFrame(table[1:], columns=table[0])
all_tables.append(df)
# Combine all tables
if all_tables:
combined_df = pd.concat(all_tables, ignore_index=True)
combined_df.to_excel("extracted_tables.xlsx", index=False)
reportlab - 创建 PDF
创建基本 PDF
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
c = canvas.Canvas("hello.pdf", pagesize=letter)
width, height = letter
# Add text
c.drawString(100, height - 100, "Hello World!")
c.drawString(100, height - 120, "This is a PDF created with reportlab")
# Add a line
c.line(100, height - 140, 400, height - 140)
# Save
c.save()
创建多页 PDF
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate("report.pdf", pagesize=letter)
styles = getSampleStyleSheet()
story = []
# Add content
title = Paragraph("Report Title", styles['Title'])
story.append(title)
story.append(Spacer(1, 12))
body = Paragraph("This is the body of the report. " * 20, styles['Normal'])
story.append(body)
story.append(PageBreak())
# Page 2
story.append(Paragraph("Page 2", styles['Heading1']))
story.append(Paragraph("Content for page 2", styles['Normal']))
# Build PDF
doc.build(story)
命令行工具
pdftotext (poppler-utils)
# Extract text
pdftotext input.pdf output.txt
# Extract text preserving layout
pdftotext -layout input.pdf output.txt
# Extract specific pages
pdftotext -f 1 -l 5 input.pdf output.txt # Pages 1-5
qpdf
# Merge PDFs
qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf
# Split pages
qpdf input.pdf --pages . 1-5 -- pages1-5.pdf
qpdf input.pdf --pages . 6-10 -- pages6-10.pdf
# Rotate pages
qpdf input.pdf output.pdf --rotate=+90:1 # Rotate page 1 by 90 degrees
# Remove password
qpdf --password=mypassword --decrypt encrypted.pdf decrypted.pdf
pdftk(如果可用)
# Merge
pdftk file1.pdf file2.pdf cat output merged.pdf
# Split
pdftk input.pdf burst
# Rotate
pdftk input.pdf rotate 1east output rotated.pdf
常见任务
从扫描版 PDF 中提取文本
# Requires: pip install pytesseract pdf2image
import pytesseract
from pdf2image import convert_from_path
# Convert PDF to images
images = convert_from_path('scanned.pdf')
# OCR each page
text = ""
for i, image in enumerate(images):
text += f"Page {i+1}:\n"
text += pytesseract.image_to_string(image)
text += "\n\n"
print(text)
添加水印
from pypdf import PdfReader, PdfWriter
# Create watermark (or load existing)
watermark = PdfReader("watermark.pdf").pages[0]
# Apply to all pages
reader = PdfReader("document.pdf")
writer = PdfWriter()
for page in reader.pages:
page.merge_page(watermark)
writer.add_page(page)
with open("watermarked.pdf", "wb") as output:
writer.write(output)
提取图像
# Using pdfimages (poppler-utils)
pdfimages -j input.pdf output_prefix
# This extracts all images as output_prefix-000.jpg, output_prefix-001.jpg, etc.
密码保护
from pypdf import PdfReader, PdfWriter
reader = PdfReader("input.pdf")
writer = PdfWriter()
for page in reader.pages:
writer.add_page(page)
# Add password
writer.encrypt("userpassword", "ownerpassword")
with open("encrypted.pdf", "wb") as output:
writer.write(output)
快速参考
| 任务 | 最佳工具 | 命令/代码 |
|---|---|---|
| 合并 PDF | pypdf | writer.add_page(page) |
| 拆分 PDF | pypdf | 每个文件一页 |
| 提取文本 | pdfplumber | page.extract_text() |
| 提取表格 | pdfplumber | page.extract_tables() |
| 创建 PDF | reportlab | Canvas 或 Platypus |
| 通过命令行合并 | qpdf | qpdf --empty --pages ... |
| 对扫描版 PDF 进行 OCR | pytesseract | 先转换为图像 |
| 填写 PDF 表单 | pdf-lib 或 pypdf(参见 forms.md) | 参见 forms.md |
示例
示例 1:从 PDF 报告中提取表格
User: "Pull the tables out of this quarterly report PDF"
→ Opens PDF with pdfplumber
→ Extracts tables, converts to pandas DataFrame
→ Exports to Excel file with clean formatting
示例 2:合并多个 PDF
User: "Combine these three contracts into one PDF"
→ Uses pypdf to read all input files
→ Adds pages sequentially to new writer
→ Saves merged document to output path
示例 3:填写 PDF 表单
User: "Fill in this tax form with my info"
→ Reads forms.md for form-filling workflow
→ Uses pdf-lib to populate form fields
→ Saves completed PDF with flattened form data
后续步骤
- 有关 pypdfium2 的高级用法,请参阅 reference.md
- 有关 JavaScript 库(pdf-lib),请参阅 reference.md
- 如果需要填写 PDF 表单,请按照 forms.md 中的说明操作
- 有关故障排除指南,请参阅 reference.md