run2_pdf-extraction
Robust PDF text extraction with fallback strategies and error handling for academic papers and documents
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 92
- Forks
- 7
01 前言 Front Matter
- name
- run2_pdf-extraction
- description
- Robust PDF text extraction with fallback strategies and error handling for academic papers and documents
03 SKILL.md 正文
PDF Text Extraction Skill (Improved)
Overview
Extract text from PDF files with improved robustness, handling corrupt PDFs gracefully. Multiple extraction strategies ensure maximum content recovery.
Installation
pip install pdfplumber pypdf pymupdf -q
Usage Examples
Primary Strategy: pdfplumber (Most Reliable)
import pdfplumber
import PyPDF2
def extract_pdf_content_robust(pdf_path, max_pages=5):
"""
Extract text from PDF with fallback strategies.
Tries pdfplumber first, then falls back to PyPDF2.
"""
text = ""
# Strategy 1: pdfplumber (best for modern PDFs)
try:
with pdfplumber.open(pdf_path) as pdf:
for i, page in enumerate(pdf.pages[:max_pages]):
extracted = page.extract_text()
if extracted:
text += extracted + " "
if len(text.strip()) > 100: # If we got substantial text
return text
except Exception as e:
pass # Try fallback
# Strategy 2: PyPDF2 (fallback for problematic PDFs)
try:
with open(pdf_path, 'rb') as f:
reader = PyPDF2.PdfReader(f)
for page in reader.pages[:max_pages]:
text += page.extract_text() + " "
if len(text.strip()) > 100:
return text
except Exception as e:
pass
return text or ""
Optimized Extraction for Academic Papers
def extract_paper_metadata(pdf_path, max_pages=3):
"""
Extract key content from academic papers:
- Title (usually on first page)
- Abstract (usually first 3 pages)
- Keywords (sometimes in abstract area)
"""
try:
with pdfplumber.open(pdf_path) as pdf:
# Extract first 3 pages which contain title and abstract
text = ""
for page in pdf.pages[:max_pages]:
text += page.extract_text() or ""
return text
except:
return ""
Key Improvements
- Dual extraction strategy: pdfplumber + PyPDF2 fallback
- Robustness: Handles corrupt/damaged PDFs gracefully
- Academic focus: Optimized for extracting paper abstracts and titles
- Efficiency: Returns early if sufficient text extracted
- Error isolation: Failures in one method don't prevent trying alternatives
Best Practices
- For academic papers, first 3-5 pages contain most essential information
- Title and abstract are usually in first 2 pages
- Some PDFs are scanned images - these need OCR (pymupdf with OCR)
- Check text length to verify successful extraction
- Handle encoding issues gracefully
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.