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

pdf-extraction-fallback-7db3aa

Skill này làm gì

Skill này thử nhiều cách lấy văn bản từ PDF khi lần thử đầu tiên thất bại. Dành cho người làm việc với biểu mẫu, giấy tờ pháp lý hoặc tài liệu quét.

Khi nào nên dùng

  • ✓Khi lần đọc PDF đầu tiên trả về lỗi hoặc không có gì.
  • ✓Khi PDF dường như là bản quét hoặc hình ảnh chứ không phải văn bản.
  • ✓Khi việc trích xuất trước đó cho văn bản thiếu hoặc bị lỗi từ một biểu mẫu hoặc bảng.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/pdf-extraction-fallback-7db3aa/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-extraction-fallback-7db3aa/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

“AI không đọc được văn bản trong PDF biểu mẫu thuế của tôi. Hãy thử các cách khác nhau cho đến khi lấy được văn bản.”

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

Cách hoạt động

AI trước tiên thử một công cụ dòng lệnh như pdftotext, kiểm tra xem đầu ra có nội dung thật không, và chuyển sang các thư viện Python nếu không có.

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 Extraction Fallback Strategy

Purpose

When processing complex documents (tax forms, legal documents, scanned materials), PDF extraction often fails on the first attempt. This skill provides a systematic fallback approach that tries multiple extraction methods in sequence until one succeeds.

When to Use

  • Initial PDF reading tools return errors or empty content
  • Document appears to be scanned/image-based rather than text-based
  • Previous extraction attempts produced incomplete or garbled output
  • Working with forms, tables, or structured documents that need reliable extraction

Fallback Sequence

Tier 1: Shell-Based Extraction (pdftotext)

Start with command-line tools that often handle edge cases better:

# Extract text maintaining layout
pdftotext -layout input.pdf output.txt

# Extract raw text (faster, less formatting)
pdftotext input.pdf output.txt

# Extract specific page range
pdftotext -f 1 -l 3 input.pdf output.txt

Check if output contains meaningful content before proceeding.

Tier 2: Python-Based Parsing

If shell tools fail, use Python libraries with different extraction approaches:

# Using PyPDF2 for basic text extraction
import PyPDF2
with open('document.pdf', 'rb') as f:
    reader = PyPDF2.PdfReader(f)
    text = ''.join(page.extract_text() for page in reader.pages)

# Using pdfplumber for tables and structured content
import pdfplumber
with pdfplumber.open('document.pdf') as pdf:
    for page in pdf.pages:
        text = page.extract_text()
        tables = page.extract_tables()

# Using pypdf for newer PDF features
from pypdf import PdfReader
reader = PdfReader('document.pdf')
text = ''.join(page.extract_text() for page in reader.pages)

Tier 3: OCR Tools (for Scanned Documents)

If the PDF contains images or scanned content, use OCR:

# Using tesseract via command line
tesseract input.pdf output --psm 6

# Using Python with pytesseract
import pytesseract
from pdf2image import convert_from_path

images = convert_from_path('document.pdf')
text = ''.join(pytesseract.image_to_string(img) for img in images)

Implementation Pattern

def extract_pdf_resilient(pdf_path):
    """Try multiple extraction methods until one succeeds."""
    
    # Tier 1: Shell extraction
    result = run_shell(f'pdftotext -layout "{pdf_path}" -')
    if result.stdout and len(result.stdout.strip()) > 100:
        return result.stdout, 'pdftotext'
    
    # Tier 2: Python libraries
    try:
        import pdfplumber
        with pdfplumber.open(pdf_path) as pdf:
            text = ''.join(page.extract_text() or '' for page in pdf.pages)
        if text.strip():
            return text, 'pdfplumber'
    except Exception:
        pass
    
    # Tier 3: OCR fallback
    try:
        from pdf2image import convert_from_path
        import pytesseract
        images = convert_from_path(pdf_path)
        text = ''.join(pytesseract.image_to_string(img) for img in images)
        if text.strip():
            return text, 'tesseract-ocr'
    except Exception:
        pass
    
    raise ExtractionError("All extraction methods failed")

Decision Criteria

Indicator Action
Empty output Proceed to next tier
Garbled/special characters Try next tier
Partial content Accept if meets minimum threshold
Tool not available Skip to next tier
Format-specific errors Try alternative library

Best Practices

  1. Validate each attempt - Check output length and quality before accepting
  2. Log which method succeeded - Track which tier worked for future reference
  3. Set minimum content thresholds - Don't accept trivial results (e.g., <50 chars)
  4. Combine methods if needed - Some documents need multiple approaches for different sections
  5. Preserve original file - Never modify the source PDF during extraction attempts

Error Handling

  • Catch exceptions at each tier, don't fail immediately
  • Log detailed error messages for debugging
  • Continue to next tier even if current tier partially succeeds but produces poor quality
  • After all tiers fail, provide clear summary of what was tried

Output Quality Check

Before declaring extraction complete:

def validate_extraction(text):
    if not text or len(text.strip()) < 50:
        return False
    if text.count('') > len(text) * 0.1:  # Too many replacement chars
        return False
    if len(set(text)) < 10:  # Too little cha
Câu hỏi thường gặp

Công cụ nào được thử trước?

Hướng dẫn bắt đầu với pdftotext, vì công cụ này thường xử lý tệp khó tốt hơn.

Kết quả xấu được nhận ra thế nào?

Hướng dẫn yêu cầu kiểm tra đầu ra có nội dung có nghĩa hay không trước khi đi tiếp.

Thư viện Python có được dùng sau không?

Có. Các thư viện như PyPDF2 hoặc pdfplumber được dùng nếu công cụ shell thất bại.

Chi tiết

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

Resilient multi-tier PDF extraction with sequential fallback strategies when initial reading fails

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12921
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ự