项目文件夹

文件
wehub-resource-sync bb5c75ce05
Component Security Validation / Security Audit (push) Has been cancelled
Deploy to Cloudflare Pages / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:58 +08:00

1 行
7.2 KiB
JSON

{"content": "---\nname: PDF Processing Pro\ndescription: Production-ready PDF processing with forms, tables, OCR, validation, and batch operations. Use when working with complex PDF workflows in production environments, processing large volumes of PDFs, or requiring robust error handling and validation.\n---\n\n# PDF Processing Pro\n\nProduction-ready PDF processing toolkit with pre-built scripts, comprehensive error handling, and support for complex workflows.\n\n## Quick start\n\n### Extract text from PDF\n\n```python\nimport pdfplumber\n\nwith pdfplumber.open(\"document.pdf\") as pdf:\n text = pdf.pages[0].extract_text()\n print(text)\n```\n\n### Analyze PDF form (using included script)\n\n```bash\npython scripts/analyze_form.py input.pdf --output fields.json\n# Returns: JSON with all form fields, types, and positions\n```\n\n### Fill PDF form with validation\n\n```bash\npython scripts/fill_form.py input.pdf data.json output.pdf\n# Validates all fields before filling, includes error reporting\n```\n\n### Extract tables from PDF\n\n```bash\npython scripts/extract_tables.py report.pdf --output tables.csv\n# Extracts all tables with automatic column detection\n```\n\n## Features\n\n### ✅ Production-ready scripts\n\nAll scripts include:\n- **Error handling**: Graceful failures with detailed error messages\n- **Validation**: Input validation and type checking\n- **Logging**: Configurable logging with timestamps\n- **Type hints**: Full type annotations for IDE support\n- **CLI interface**: `--help` flag for all scripts\n- **Exit codes**: Proper exit codes for automation\n\n### ✅ Comprehensive workflows\n\n- **PDF Forms**: Complete form processing pipeline\n- **Table Extraction**: Advanced table detection and extraction\n- **OCR Processing**: Scanned PDF text extraction\n- **Batch Operations**: Process multiple PDFs efficiently\n- **Validation**: Pre and post-processing validation\n\n## Advanced topics\n\n### PDF Form Processing\n\nFor complete form workflows including:\n- Field analysis and detection\n- Dynamic form filling\n- Validation rules\n- Multi-page forms\n- Checkbox and radio button handling\n\nSee [FORMS.md](FORMS.md)\n\n### Table Extraction\n\nFor complex table extraction:\n- Multi-page tables\n- Merged cells\n- Nested tables\n- Custom table detection\n- Export to CSV/Excel\n\nSee [TABLES.md](TABLES.md)\n\n### OCR Processing\n\nFor scanned PDFs and image-based documents:\n- Tesseract integration\n- Language support\n- Image preprocessing\n- Confidence scoring\n- Batch OCR\n\nSee [OCR.md](OCR.md)\n\n## Included scripts\n\n### Form processing\n\n**analyze_form.py** - Extract form field information\n```bash\npython scripts/analyze_form.py input.pdf [--output fields.json] [--verbose]\n```\n\n**fill_form.py** - Fill PDF forms with data\n```bash\npython scripts/fill_form.py input.pdf data.json output.pdf [--validate]\n```\n\n**validate_form.py** - Validate form data before filling\n```bash\npython scripts/validate_form.py data.json schema.json\n```\n\n### Table extraction\n\n**extract_tables.py** - Extract tables to CSV/Excel\n```bash\npython scripts/extract_tables.py input.pdf [--output tables.csv] [--format csv|excel]\n```\n\n### Text extraction\n\n**extract_text.py** - Extract text with formatting preservation\n```bash\npython scripts/extract_text.py input.pdf [--output text.txt] [--preserve-formatting]\n```\n\n### Utilities\n\n**merge_pdfs.py** - Merge multiple PDFs\n```bash\npython scripts/merge_pdfs.py file1.pdf file2.pdf file3.pdf --output merged.pdf\n```\n\n**split_pdf.py** - Split PDF into individual pages\n```bash\npython scripts/split_pdf.py input.pdf --output-dir pages/\n```\n\n**validate_pdf.py** - Validate PDF integrity\n```bash\npython scripts/validate_pdf.py input.pdf\n```\n\n## Common workflows\n\n### Workflow 1: Process form submissions\n\n```bash\n# 1. Analyze form structure\npython scripts/analyze_form.py template.pdf --output schema.json\n\n# 2. Validate submission data\npython scripts/validate_form.py submission.json schema.json\n\n# 3. Fill form\npython scripts/fill_form.py template.pdf submission.json completed.pdf\n\n# 4. Validate output\npython scripts/validate_pdf.py completed.pdf\n```\n\n### Workflow 2: Extract data from reports\n\n```bash\n# 1. Extract tables\npython scripts/extract_tables.py monthly_report.pdf --output data.csv\n\n# 2. Extract text for analysis\npython scripts/extract_text.py monthly_report.pdf --output report.txt\n```\n\n### Workflow 3: Batch processing\n\n```python\nimport glob\nfrom pathlib import Path\nimport subprocess\n\n# Process all PDFs in directory\nfor pdf_file in glob.glob(\"invoices/*.pdf\"):\n output_file = Path(\"processed\") / Path(pdf_file).name\n\n result = subprocess.run([\n \"python\", \"scripts/extract_text.py\",\n pdf_file,\n \"--output\", str(output_file)\n ], capture_output=True)\n\n if result.returncode == 0:\n print(f\"✓ Processed: {pdf_file}\")\n else:\n print(f\"✗ Failed: {pdf_file} - {result.stderr}\")\n```\n\n## Error handling\n\nAll scripts follow consistent error patterns:\n\n```python\n# Exit codes\n# 0 - Success\n# 1 - File not found\n# 2 - Invalid input\n# 3 - Processing error\n# 4 - Validation error\n\n# Example usage in automation\nresult = subprocess.run([\"python\", \"scripts/fill_form.py\", ...])\n\nif result.returncode == 0:\n print(\"Success\")\nelif result.returncode == 4:\n print(\"Validation failed - check input data\")\nelse:\n print(f\"Error occurred: {result.returncode}\")\n```\n\n## Dependencies\n\nAll scripts require:\n\n```bash\npip install pdfplumber pypdf pillow pytesseract pandas\n```\n\nOptional for OCR:\n```bash\n# Install tesseract-ocr system package\n# macOS: brew install tesseract\n# Ubuntu: apt-get install tesseract-ocr\n# Windows: Download from GitHub releases\n```\n\n## Performance tips\n\n- **Use batch processing** for multiple PDFs\n- **Enable multiprocessing** with `--parallel` flag (where supported)\n- **Cache extracted data** to avoid re-processing\n- **Validate inputs early** to fail fast\n- **Use streaming** for large PDFs (>50MB)\n\n## Best practices\n\n1. **Always validate inputs** before processing\n2. **Use try-except** in custom scripts\n3. **Log all operations** for debugging\n4. **Test with sample PDFs** before production\n5. **Set timeouts** for long-running operations\n6. **Check exit codes** in automation\n7. **Backup originals** before modification\n\n## Troubleshooting\n\n### Common issues\n\n**\"Module not found\" errors**:\n```bash\npip install -r requirements.txt\n```\n\n**Tesseract not found**:\n```bash\n# Install tesseract system package (see Dependencies)\n```\n\n**Memory errors with large PDFs**:\n```python\n# Process page by page instead of loading entire PDF\nwith pdfplumber.open(\"large.pdf\") as pdf:\n for page in pdf.pages:\n text = page.extract_text()\n # Process page immediately\n```\n\n**Permission errors**:\n```bash\nchmod +x scripts/*.py\n```\n\n## Getting help\n\nAll scripts support `--help`:\n\n```bash\npython scripts/analyze_form.py --help\npython scripts/extract_tables.py --help\n```\n\nFor detailed documentation on specific topics, see:\n- [FORMS.md](FORMS.md) - Complete form processing guide\n- [TABLES.md](TABLES.md) - Advanced table extraction\n- [OCR.md](OCR.md) - Scanned PDF processing\n"}