Skip to content
cccskills
FreePublished on GitHub

pdf

Work with PDF files to extract text, tables, and images, combine or split documents, rotate pages, and create new PDFs. It also covers forms, watermarks, password protection, and OCR, which recognizes text in scanned pages.

OriginalShow the original description

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

See how to install

Good for

  • Extracting text and tables from PDFs
  • Combining documents or splitting pages
  • Recognizing text in scanned PDFs
  • Creating PDFs and filling forms

Files included (12)

  • SKILL.md8.3 KB
  • forms.md11.3 KB
  • LICENSE.txt1.4 KB
  • reference.md15.7 KB
  • scripts/check_bounding_boxes.py2.7 KB
  • scripts/check_fillable_fields.py268 B
  • scripts/convert_pdf_to_images.py1008 B
  • scripts/create_validation_image.py1.2 KB
  • scripts/extract_form_field_info.py4.2 KB
  • scripts/extract_form_structure.py3.9 KB
  • scripts/fill_fillable_fields.py3.7 KB
  • scripts/fill_pdf_form_with_annotations.py3.2 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

重要: 所有 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 并按照其中的说明操作。

快速入门

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

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)  # 顺时针旋转 90 度
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:  # 检查表格是否为空
                df = pd.DataFrame(table[1:], columns=table[0])
                all_tables.append(df)

# 合并所有表格
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

# 添加文本
c.drawString(100, height - 100, "Hello World!")
c.drawString(100, height - 120, "This is a PDF created with reportlab")

# 添加线条
c.line(100, height - 140, 400, height - 140)

# 保存
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 = []

# 添加内容
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())

# 第 2 页
story.append(Paragraph("Page 2", styles['Heading1']))
story.append(Paragraph("Content for page 2", styles['Normal']))

# 构建 PDF
doc.build(story)

下标和上标

重要:切勿在 ReportLab PDF 中使用 Unicode 下标/上标字符(₀₁₂₃₄₅₆₇₈₉、⁰¹²³⁴⁵⁶⁷⁸⁹)。内置字体不包含这些字形,会导致它们渲染为黑色方块。

请改用 ReportLab 在 Paragraph 对象中的 XML 标记标签:

from reportlab.platypus import Paragraph
from reportlab.lib.styles import getSampleStyleSheet

styles = getSampleStyleSheet()

# 下标:使用 <sub> 标签
chemical = Paragraph("H<sub>2</sub>O", styles['Normal'])

# 上标:使用 <super> 标签
squared = Paragraph("x<super>2</super> + y<super>2</super>", styles['Normal'])

对于使用 canvas 绘制的文本(非 Paragraph 对象),请手动调整字体大小和位置,而不是使用 Unicode 下标/上标。

命令行工具

pdftotext (poppler-utils)

# 提取文本
pdftotext input.pdf output.txt

# 保留布局提取文本
pdftotext -layout input.pdf output.txt

# 提取指定页面
pdftotext -f 1 -l 5 input.pdf output.txt  # 第 1-5 页

qpdf

# 合并 PDF
qpdf --empty --pages file1.pdf file2.pdf -- merged.pdf

# 拆分页面
qpdf input.pdf --pages . 1-5 -- pages1-5.pdf
qpdf input.pdf --pages . 6-10 -- pages6-10.pdf

# 旋转页面
qpdf input.pdf output.pdf --rotate=+90:1  # 将第 1 页旋转 90 度

# 移除密码
qpdf --password=mypassword --decrypt encrypted.pdf decrypted.pdf

pdftk(如果可用)

# 合并
pdftk file1.pdf file2.pdf cat output merged.pdf

# 拆分
pdftk input.pdf burst

# 旋转
pdftk input.pdf rotate 1east output rotated.pdf

常见任务

从扫描版 PDF 提取文本

# 需要安装:pip install pytesseract pdf2image
import pytesseract
from pdf2image import convert_from_path

# 将 PDF 转换为图片
images = convert_from_path('scanned.pdf')

# 对每页进行 OCR
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

# 创建水印(或加载已有水印)
watermark = PdfReader("watermark.pdf").pages[0]

# 应用到所有页面
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)

提取图片

# 使用 pdfimages (poppler-utils)
pdfimages -j input.pdf output_prefix

# 这会将所有图片提取为 output_prefix-000.jpg、output_prefix-001.jpg 等

密码保护

from pypdf import PdfReader, PdfWriter

reader = PdfReader("input.pdf")
writer = PdfWriter()

for page in reader.pages:
    writer.add_page(page)

# 添加密码
writer.encrypt("userpassword", "ownerpassword")

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

快速参考

任务最佳工具命令/代码
合并 PDFpypdfwriter.add_page(page)
拆分 PDFpypdf每页一个文件
提取文本pdfplumberpage.extract_text()
提取表格pdfplumberpage.extract_tables()
创建 PDFreportlabCanvas 或 Platypus
命令行合并qpdfqpdf --empty --pages ...
OCR 扫描版 PDFpytesseract先转换为图片
填写 PDF 表单pdf-lib 或 pypdf(见 FORMS.md)见 FORMS.md

后续步骤

  • 有关 pypdfium2 的高级用法,请参阅 REFERENCE.md
  • 有关 JavaScript 库(pdf-lib),请参阅 REFERENCE.md
  • 如果需要填写 PDF 表单,请按照 FORMS.md 中的说明操作
  • 有关故障排除指南,请参阅 REFERENCE.md

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

alicloud_cli

FreeSummary

Find installation steps, configuration guidance, and command examples for Alibaba Cloud CLI using local Chinese documentation. The skill checks its quick reference or relevant documentation pages before suggesting commands, parameters, and options.

  • Look up installation and configuration steps
  • Check commands for querying ECS instances
  • Find output filtering and pagination options
agentscope-ai/QwenPaw36KUpdated Oct 22, 2026

browser

FreeSummary

Use QwenPaw's built-in Browser SDK, a browser control interface, to read pages, search, fill forms, click controls, and capture screenshots through asynchronous Python. The workflow checks the page before and after actions and hands authentication steps to a person.

  • Search a website and inspect the resulting page
  • Fill forms and select options in a browser
  • Read page text and capture screenshots
agentscope-ai/QwenPaw36KUpdated Oct 22, 2026

browser

FreeSummary

Automate browser tasks through QwenPaw's built-in Browser SDK using asynchronous Python. Read page content, fill forms, click controls, and capture screenshots, checking the resulting page after each action to verify what happened.

  • Search a website and inspect the results
  • Fill forms and operate page controls
  • Capture page screenshots
agentscope-ai/QwenPaw36KUpdated Oct 22, 2026

browser_cdp

FreeSummary

Connect an agent to an existing Chrome browser, find local CDP ports used for remote browser control, or start a browser on a specified port for sharing with other agents and tools.

  • Connecting to Chrome with existing logins and open tabs
  • Finding available local browser debugging ports
  • Starting a browser on a fixed debugging port
agentscope-ai/QwenPaw36KUpdated Oct 22, 2026

browser_cdp

FreeSummary

Connect to an existing Chrome browser or launch one with an explicit debugging port for sharing across agents and tools. It also covers scanning local CDP ports, the connection endpoints used to control a browser.

  • Connect to Chrome with existing logins and open tabs
  • Find available local browser debugging ports
  • Launch a browser with a fixed debugging port
agentscope-ai/QwenPaw36KUpdated Oct 22, 2026

browser_visible

FreeSummary

Configure how the browser starts, including window visibility, proxy settings, extra launch options, and a custom browser executable. It also explains opening a local debugging port so other local tools can connect.

  • Show the browser window during automation
  • Start a browser with a proxy or incognito mode
  • Choose a specific installed browser
agentscope-ai/QwenPaw36KUpdated Oct 22, 2026

See all skills by agentscope-ai

Report a problem with this skill