跳到正文
FunCoding

pdf

Process PDF files - extract text, read content, create PDFs, merge or split documents. Use when user asks to read, create, or manipulate PDF files.

Star
829
最近提交
2026年1月17日

backend/skills/pdf/SKILL.md

在 GitHub 上查看

PDF Processing Skill

You now have expertise in PDF file processing. Follow these instructions based on the task.

Reading PDFs

# Install if needed
apt-get install -y poppler-utils

# Extract text from PDF
pdftotext input.pdf output.txt

# Extract specific pages
pdftotext -f 1 -l 5 input.pdf output.txt

# Extract with layout preservation
pdftotext -layout input.pdf output.txt

Using Python PyMuPDF

import fitz  # PyMuPDF

# Open and read PDF
doc = fitz.open("input.pdf")
for page_num, page in enumerate(doc):
    text = page.get_text()
    print(f"--- Page {page_num + 1} ---")
    print(text)

# Extract text from specific pages
page = doc[0]  # First page (0-indexed)
text = page.get_text()

Using pdfplumber (for tables)

import pdfplumber

with pdfplumber.open("input.pdf") as pdf:
    for page in pdf.pages:
        # Extract text
        text = page.extract_text()
        print(text)

        # Extract tables
        tables = page.extract_tables()
        for table in tables:
            for row in table:
                print(row)

Creating PDFs

Using Python reportlab

from reportlab.lib.pagesizes import letter, A4
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

# Create a simple PDF
c = canvas.Canvas("output.pdf", pagesize=letter)
c.setFont("Helvetica", 12)
c.drawString(1*inch, 10*inch, "Hello World")
c.save()

Using Pandoc (from Markdown)

# Install pandoc if needed
apt-get install -y pandoc texlive-xetex

# Convert Markdown to PDF
pandoc input.md -o output.pdf

# With custom template
pandoc input.md -o output.pdf --template=template.tex

Using weasyprint (from HTML)

from weasyprint import HTML

# From HTML string
html_content = "<h1>Hello World</h1><p>This is a PDF.</p>"
HTML(string=html_content).write_pdf("output.pdf")

# From HTML file
HTML("input.html").write_pdf("output.pdf")

Merging PDFs

import fitz

result = fitz.open()
for pdf_file in ["file1.pdf", "file2.pdf", "file3.pdf"]:
    doc = fitz.open(pdf_file)
    result.insert_pdf(doc)
result.save("merged.pdf")

Splitting PDFs

import fitz

doc = fitz.open("input.pdf")

# Extract specific pages
output = fitz.open()
output.insert_pdf(doc, from_page=0, to_page=4)  # Pages 1-5
output.save("pages_1_to_5.pdf")

# Split into individual pages
for i, page in enumerate(doc):
    single_page = fitz.open()
    single_page.insert_pdf(doc, from_page=i, to_page=i)
    single_page.save(f"page_{i+1}.pdf")

Adding Watermarks

import fitz

doc = fitz.open("input.pdf")
for page in doc:
    # Add text watermark
    page.insert_text(
        (100, 100),
        "CONFIDENTIAL",
        fontsize=50,
        color=(0.8, 0.8, 0.8),
        rotate=45
    )
doc.save("watermarked.pdf")

Key Libraries

LibraryUse CaseInstall
PyMuPDF (fitz)Read, write, merge, splitpip install pymupdf
pdftotextCLI text extractionapt install poppler-utils
pdfplumberTable extractionpip install pdfplumber
reportlabCreate PDFs programmaticallypip install reportlab
weasyprintHTML to PDFpip install weasyprint
pdf2imagePDF to imagespip install pdf2image
PyPDF2Merge, split, rotatepip install pypdf2

Best Practices

  1. Always check if required libraries are installed before use
  2. For large PDFs, process pages in batches to manage memory
  3. When extracting text, verify encoding (UTF-8 is recommended)
  4. For table extraction, pdfplumber is more reliable than PyMuPDF
  5. Close PDF documents after processing to free resources

相似的 Skill

精选

pptx

生成、读取和修改演示文稿,也能合并拆分幻灯片。

ANanthropics/skills203k近 7 天新增 2,822 个 star

精选

docx

创建和编辑 Word 文档,支持目录、页码、页眉等排版。

ANanthropics/skills203k近 7 天新增 2,822 个 star

精选

pdf

提取文本和表格、合并拆分、填写表单,也能对扫描件做 OCR。

ANanthropics/skills203k近 7 天新增 2,822 个 star

精选

doc-coauthoring

按步骤协作撰写技术方案、提案和决策文档,逐轮修改。

ANanthropics/skills203k近 7 天新增 2,822 个 star

精选

xlsx

读写 Excel 和 CSV,处理公式、格式、图表与数据清洗。

ANanthropics/skills203k近 7 天新增 2,822 个 star

精选

canvas-design

按设计理念生成海报和视觉作品,输出 PNG 或 PDF。

ANanthropics/skills203k近 7 天新增 2,822 个 star