๐ŸŽ Get the FREE AI Skills Starter Guide โ€” Subscribe โ†’
BytesAgainBytesAgain
๐Ÿฆ€ ClawHub

Invoice-Recognition

by @aitanjp

Extract invoice information from images and PDF files using Baidu OCR API, export to Excel. Supports single file, multiple files, or entire directory process...

Versionv1.0.0
Downloads659
Installs1
Starsโญ 1
TERMINAL
clawhub install invoice-extractor

๐Ÿ“– About This Skill


name: invoice-extractor description: Extract invoice information from images and PDF files using Baidu OCR API, export to Excel. Supports single file, multiple files, or entire directory processing. Use when the user mentions invoices, invoice recognition, extracting invoice data, processing receipts, converting invoices to Excel, or batch processing invoice files.

Invoice Extractor

Extract invoice information from images (PNG, JPG) and PDF files, then export to Excel format.

Capabilities

  • Multi-format support: PNG, JPG, JPEG, BMP, TIFF, PDF
  • High accuracy: Uses Baidu OCR API specialized for invoice recognition
  • Complete fields: Extracts all invoice fields including buyer/seller info, amounts, items
  • Excel export: Formatted Excel output with summary and detail sheets
  • Flexible input: Single file, multiple files, or entire directory processing
  • Batch processing: Process hundreds of invoices in one command
  • Preview mode: List files before processing
  • Prerequisites

    1. Baidu Cloud OCR API credentials (free tier: 50,000 requests/day) 2. Python environment with required packages

    Quick Start

    1. Setup Baidu OCR

    Get API credentials from https://cloud.baidu.com/product/ocr: 1. Register/login to Baidu Cloud 2. Create an application 3. Get API Key and Secret Key

    2. Configure

    Create config.txt in the project root:

    BAIDU_API_KEY=your_api_key_here
    BAIDU_SECRET_KEY=your_secret_key_here
    

    Or run the setup wizard:

    python main_baidu.py --setup
    

    3. Run

    Process a single file:

    python main_baidu.py -f invoice.pdf
    

    Process multiple files:

    python main_baidu.py -f invoice1.pdf -f invoice2.png
    

    Process entire directory:

    python main_baidu.py -i ./fp
    

    Mixed mode (directory + extra files):

    python main_baidu.py -i ./fp -f extra_invoice.pdf
    

    Output will be saved to output/ directory as Excel file.

    Workflow

    Task Progress:
    
  • [ ] Check prerequisites (Baidu API credentials)
  • [ ] Choose input method (single file / multiple files / directory)
  • [ ] Scan and collect invoice files
  • [ ] Preview files (optional with --list)
  • [ ] Process each file with Baidu OCR
  • [ ] Parse invoice fields
  • [ ] Export to Excel
  • [ ] Verify output
  • Input Methods

    Single File

    Process one specific invoice file:
    python main_baidu.py -f invoice.pdf
    python main_baidu.py -f "path/to/invoice.png"
    

    Multiple Files

    Process several specific files:
    python main_baidu.py -f file1.pdf -f file2.png -f file3.jpg
    

    Entire Directory

    Process all invoice files in a directory (recursive):
    python main_baidu.py -i ./my_invoices
    python main_baidu.py -i "/path/to/invoice/folder"
    

    Mixed Mode

    Combine directory and individual files:
    python main_baidu.py -i ./fp -f ./extra/invoice.pdf
    

    Preview Mode

    List files without processing:
    python main_baidu.py -i ./fp --list
    

    Extracted Fields

    Basic Information

  • Invoice code (ๅ‘็ฅจไปฃ็ )
  • Invoice number (ๅ‘็ฅจๅท็ )
  • Invoice date (ๅผ€็ฅจๆ—ฅๆœŸ)
  • Invoice type (ๅ‘็ฅจ็ฑปๅž‹)
  • Buyer Information

  • Name (่ดญไนฐๆ–นๅ็งฐ)
  • Tax number (็บณ็จŽไบบ่ฏ†ๅˆซๅท)
  • Address and phone (ๅœฐๅ€็”ต่ฏ)
  • Bank account (ๅผ€ๆˆท่กŒๅŠ่ดฆๅท)
  • Seller Information

  • Name (้”€ๅ”ฎๆ–นๅ็งฐ)
  • Tax number (็บณ็จŽไบบ่ฏ†ๅˆซๅท)
  • Address and phone (ๅœฐๅ€็”ต่ฏ)
  • Bank account (ๅผ€ๆˆท่กŒๅŠ่ดฆๅท)
  • Amounts

  • Total amount (ๅˆ่ฎก้‡‘้ข)
  • Total tax (ๅˆ่ฎก็จŽ้ข)
  • Amount with tax (ไปท็จŽๅˆ่ฎก)
  • Items

  • Product name (่ดง็‰ฉๅ็งฐ)
  • Specification (่ง„ๆ ผๅž‹ๅท)
  • Unit (ๅ•ไฝ)
  • Quantity (ๆ•ฐ้‡)
  • Unit price (ๅ•ไปท)
  • Amount (้‡‘้ข)
  • Tax rate (็จŽ็އ)
  • Tax amount (็จŽ้ข)
  • Command Line Options

    python main_baidu.py [options]

    Input Options: -f FILE, --file FILE Specify invoice file (can be used multiple times) -i DIR, --input DIR Input directory (default: fp)

    Output Options: -o DIR, --output DIR Output directory (default: output) -n NAME, --name NAME Output filename prefix (default: ๅ‘็ฅจไฟกๆฏ)

    Authentication Options: --api-key KEY Baidu API Key --secret-key KEY Baidu Secret Key

    Other Options: --setup Run configuration wizard --list List files to be processed without processing -h, --help Show help

    Usage Examples

    Example 1: Single File

    python main_baidu.py -f "invoice.pdf"
    

    Example 2: Multiple Files

    python main_baidu.py -f "1.pdf" -f "2.png" -f "3.jpg"
    

    Example 3: Entire Directory

    python main_baidu.py -i "./2024_invoices"
    

    Example 4: Preview Before Processing

    python main_baidu.py -i ./fp --list
    

    Then process:

    python main_baidu.py -i ./fp

    Example 5: Mixed Input

    python main_baidu.py -i ./fp -f ./urgent/invoice.pdf -o ./output -n "March_2024"
    

    Example 6: Custom Output

    python main_baidu.py -i ./fp -o ./reports -n "Q1_Invoice_Summary"
    

    Project Structure

    .
    โ”œโ”€โ”€ fp/                      # Place invoice files here
    โ”œโ”€โ”€ output/                  # Excel output directory
    โ”œโ”€โ”€ src/
    โ”‚   โ”œโ”€โ”€ main_baidu.py       # Main entry point
    โ”‚   โ”œโ”€โ”€ baidu_ocr_extractor.py  # Baidu OCR wrapper
    โ”‚   โ”œโ”€โ”€ invoice_model.py    # Data models
    โ”‚   โ”œโ”€โ”€ excel_exporter.py   # Excel export
    โ”‚   โ””โ”€โ”€ config.py           # Configuration
    โ”œโ”€โ”€ scripts/                 # Utility scripts
    โ”‚   โ”œโ”€โ”€ batch_process.py    # Batch processing helper
    โ”‚   โ””โ”€โ”€ verify_export.py    # Verify Excel export
    โ”œโ”€โ”€ config.txt              # API credentials
    โ”œโ”€โ”€ requirements.txt        # Dependencies
    โ”œโ”€โ”€ SKILL.md                # This file
    โ”œโ”€โ”€ setup.md                # Detailed setup guide
    โ””โ”€โ”€ examples.md             # Usage examples
    

    Utility Scripts

    Batch Processing Helper

    python scripts/batch_process.py /path/to/invoices
    

    Verify Export

    python scripts/verify_export.py output/invoice_info.xlsx
    

    Error Handling

    Common issues and solutions:

    "Baidu OCR authentication failed"

  • Check API Key and Secret Key in config.txt
  • Verify credentials are correct in Baidu Cloud console
  • "No invoice files found"

  • Ensure files are in the specified directory
  • Check file formats (supported: png, jpg, jpeg, bmp, tiff, pdf)
  • Use --list to see what files are detected
  • "Image format error"

  • PDF files are automatically converted to images
  • Ensure PDF is not corrupted or password-protected
  • "File not found"

  • Check file path is correct
  • Use quotes for paths with spaces: "path/to/file name.pdf"
  • Advanced Usage

    Environment Variables

    Set credentials via environment:
    export BAIDU_API_KEY="your_key"
    export BAIDU_SECRET_KEY="your_secret"
    

    Batch Processing Script

    Create a script for monthly processing:
    #!/bin/bash
    MONTH=$(date +%Y%m)
    python main_baidu.py \
      -i "/invoices/$MONTH" \
      -o "/reports/$MONTH" \
      -n "Invoice_Report_$MONTH"
    

    Additional Resources

  • For detailed setup instructions, see setup.md
  • For more examples, see examples.md
  • For API documentation, visit https://cloud.baidu.com/doc/OCR/index.html
  • ๐Ÿ’ก Examples

    1. Setup Baidu OCR

    Get API credentials from https://cloud.baidu.com/product/ocr: 1. Register/login to Baidu Cloud 2. Create an application 3. Get API Key and Secret Key

    2. Configure

    Create config.txt in the project root:

    BAIDU_API_KEY=your_api_key_here
    BAIDU_SECRET_KEY=your_secret_key_here
    

    Or run the setup wizard:

    python main_baidu.py --setup
    

    3. Run

    Process a single file:

    python main_baidu.py -f invoice.pdf
    

    Process multiple files:

    python main_baidu.py -f invoice1.pdf -f invoice2.png
    

    Process entire directory:

    python main_baidu.py -i ./fp
    

    Mixed mode (directory + extra files):

    python main_baidu.py -i ./fp -f extra_invoice.pdf
    

    Output will be saved to output/ directory as Excel file.

    โš™๏ธ Configuration

    1. Baidu Cloud OCR API credentials (free tier: 50,000 requests/day) 2. Python environment with required packages