Chuyển đến nội dung
Skillpedia
PDF, Word và slide7.8k · #12932

pdf-text-extraction-9424c5

Skill này làm gì

Skill này giải thích cách trích xuất văn bản đọc được từ tệp PDF bằng pdftotext khi trình đọc thông thường thất bại. Dành cho người làm việc với tài liệu PDF mà AI không đọc trực tiếp được.

Khi nào nên dùng

  • ✓Khi đọc một PDF trả về nội dung bị lỗi hoặc nhị phân.
  • ✓Khi PDF chứa ảnh quét hoặc cấu trúc nhị phân phức tạp.
  • ✓Khi tôi cần văn bản PDF được lưu thành tệp hoặc dùng ở các bước sau.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction-9424c5/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-text-extraction-9424c5/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Hãy đọc văn bản từ document.pdf của tôi. Trình đọc thông thường cho đầu ra lỗi, nên hãy trích xuất bằng pdftotext.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

AI xác nhận tệp tồn tại, chạy pdftotext để lấy văn bản, và dùng đầu ra trực tiếp, hoặc ghi vào một tệp văn bản tạm nếu cần.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

PDF Text Extraction via pdftotext

Problem

When using read_file on PDF documents, the function may return binary image data or garbled content instead of readable text. This occurs because PDFs can contain scanned images or complex binary structures that read_file cannot properly parse as text.

Solution

Use the pdftotext command-line utility via run_shell to extract clean text content from PDF files.

Steps

1. Verify PDF file exists

import os

pdf_path = "path/to/document.pdf"
if not os.path.exists(pdf_path):
    raise FileNotFoundError(f"PDF not found: {pdf_path}")

2. Extract text using pdftotext

from tools import run_shell

# Extract text to stdout
result = run_shell(command=f"pdftotext '{pdf_path}' -", timeout=60)
pdf_text = result.stdout

# Alternative: extract to a temporary file
temp_txt = "/tmp/extracted.txt"
run_shell(command=f"pdftotext '{pdf_path}' '{temp_txt}'", timeout=60)
with open(temp_txt, 'r') as f:
    pdf_text = f.read()

3. Handle parameter naming carefully

When calling read_file, be aware of the parameter name:

  • Use filetype="pdf" (not file_type)
  • Some tool implementations may use different parameter names
# Correct parameter usage
content = read_file(file_path="doc.pdf", filetype="pdf")

# If this returns binary/garbled data, fall back to pdftotext

Common pdftotext Options

Option Description
- Output to stdout
-layout Maintain original layout
-f <n> Start from page n
-l <n> End at page n
-q Quiet mode

Example with options:

result = run_shell(command=f"pdftotext -layout -q '{pdf_path}' -", timeout=60)

Error Handling

from tools import run_shell

def extract_pdf_text(pdf_path):
    """Extract text from PDF using pdftotext with error handling."""
    import os
    
    if not os.path.exists(pdf_path):
        raise FileNotFoundError(f"PDF not found: {pdf_path}")
    
    result = run_shell(command=f"pdftotext '{pdf_path}' -", timeout=60)
    
    if result.returncode != 0:
        raise RuntimeError(f"pdftotext failed: {result.stderr}")
    
    return result.stdout.strip()

When to Use This Pattern

  • read_file returns binary data, garbled text, or image content for a PDF
  • You need searchable/processable text from PDF documents
  • The PDF contains text (not just scanned images - for those, consider OCR tools)

Prerequisites

  • pdftotext must be installed (part of poppler-utils on Debian/Ubuntu, poppler on macOS via Homebrew)
  • Verify availability: run_shell(command="which pdftotext")
Câu hỏi thường gặp

Vì sao kiểm tra tệp tồn tại trước?

Hướng dẫn dừng lại với một lỗi rõ ràng nếu đường dẫn PDF sai.

Mẹo tham số của read_file là gì?

Hướng dẫn nói nên dùng filetype thay vì file_type khi gọi công cụ đọc.

Đầu ra có thể đi đâu?

Nó có thể được in ra màn hình hoặc ghi vào một tệp văn bản.

Chi tiết

Mô tả gốc của tác giả

Extract text from PDF files using pdftotext when read_file returns binary data

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12932
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự