# PDF

当用户需要对PDF文件进行任何操作时，请使用此技能。包括从 PDF 中读取或提取文本/表格、合并多个 PDF、拆分...

- Repository: https://github.com/agentscope-ai/QwenPaw (37135 stars)
- Category: Documents
- License: Apache-2.0
- Last updated: 2026-10-30
- Runs scripts: yes, review before use

## Install

Claude Code: `npx skills add agentscope-ai/QwenPaw --skill pdf -g -a claude-code -y`
Codex: `npx skills add agentscope-ai/QwenPaw --skill pdf -g -a codex -y`
Cursor: `npx skills add agentscope-ai/QwenPaw --skill pdf -g -a cursor -y`
Gemini CLI: `npx skills add agentscope-ai/QwenPaw --skill pdf -g -a gemini-cli -y`

## Description

当用户需要对PDF文件进行任何操作时，请使用此技能。包括从 PDF 中读取或提取文本/表格、合并多个 PDF、拆分 PDF、旋转页面、添加水印、创建新PDF、填写PDF表单、加密/解密 PDF、提取图片，以及对扫描版 PDF 进行 OCR 使其可搜索。如果用户提到 .pdf 文件或要求生成 PDF，请使用此技能。

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

> **重要：** 所有 `scripts/` 路径均相对于此技能目录。
> 运行方式：`cd {this_skill_dir} && python scripts/...`
> 或使用 `execute_shell_command` 的 `cwd` 参数。

# PDF 处理指南

## 前置要求

- **pypdf**：核心 PDF 读写功能
- **pdfplumber**：文本和表格提取
- **reportlab**：PDF 创建
- **pdftotext** (poppler-utils)：命令行文本提取
- **pdftoppm** (poppler-utils)：PDF 转图片
- **qpdf**：PDF 操作（合并、拆分、旋转、解密）

## 概述

本指南涵盖了使用 Python 库和命令行工具进行 PDF 处理的基本操作。有关高级功能、JavaScript 库和详细示例，请参阅 REFERENCE.md。如果需要填写 PDF 表单，请阅读 FORMS.md 并按照其中的说明操作。

## 快速入门

```python
from pypdf import PdfReader, PdfWriter

# 读取 PDF
reader = PdfReader("document.pdf")
print(f"Pages: {len(reader.pages)}")

# 提取文本
text = ""
for page in reader.pages:
    text += page.extract_text()
```

## Python 库

### pypdf - 基本操作

#### 合并 PDF
```python
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
```python
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)
```

#### 提取元数据
```python
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}")
```

#### 旋转页面
```python
reader = PdfReader("input.pdf")
writer = PdfWriter()

page = reader.pages[0]
page.rotate(90)  # 顺时针旋转 90 度
writer.add_page(page)

with open("rotated.pdf", "wb") as output:
    writer.write(output)
```

### pdfplumber - 文本和表格提取

#### 提取带布局的文本
```python
import pdfplumber

with pdfplumber.open("document.pdf") as pdf:
    for page in pdf.pages:
        text = page.extract_text()
        print(text)
```

The rest: https://github.com/agentscope-ai/QwenPaw/blob/HEAD/src/qwenpaw/agents/skills/pdf-zh/SKILL.md
