️35MM.COFFEE
Profile
jimmy

2 天前

想对一个 pdf 进行中英翻译,并排展示,试了下沉浸式翻译,效果还不错,就是免费用户会有水印,于是祭出 Claude Code,经过几轮沟通后,也实现了一个 Lite 版本,确实方便。 如果你也想做一个类似的工具,可以尝试以下 Prompt:
Create a Python-based PDF bilingual translation tool with the following specifications:

  Create a command-line application that converts PDFs into bilingual format with original and translated text displayed side-by-side. The project should include:

  Core Features:
  - Extract text from PDF files with intelligent paragraph detection
  - Translate using multiple AI providers (OpenAI, Google AI/Gemini)
  - Generate bilingual PDFs with side-by-side layout (original left, translation right)
  - Translation caching system for resumable processing
  - Table of Contents extraction and preservation
  - Support for CJK languages (Chinese, Japanese, Korean) with proper fonts
  - Configurable settings via JSON config file
  - Comprehensive command-line interface

  Technical Requirements:
  - Dependencies: pdfplumber, reportlab, openai, google-generativeai, PyPDF2, pathlib2, typing-extensions
  - Font Management: Dual font system with separate English and CJK font support, automatic font fallback
  - PDF Layout: Landscape A4 format with responsive column widths
  - Caching: MD5-based translation cache using pickle files
  - Concurrency: Multi-threaded translation processing (default 3 workers)
  - Error Handling: Robust error handling for API failures, network issues, and invalid PDFs

  File Structure:
  pdf-trans/
  ├── pdftranslator.py    # Main application with PDFTranslator class
  ├── config.json         # Configuration file with font paths, settings
  ├── requirements.txt    # Python dependencies
  └── README.md          # Comprehensive documentation

  Key Command-Line Arguments:
  - Required: inputpdf, --target-lang
  - Optional: --output, --source-lang, --provider (openai/google), --model, --api-key, --api-url, --cache-dir, --concurrent, font settings, --verbose

  Configuration Features:
  - JSON config with default providers, models, languages
  - Font configuration for different OS (macOS, Linux, Windows)
  - PDF formatting settings (font sizes, spacing, layout)
  - Translation settings (temperature, max tokens, retry attempts)

  Special Capabilities:
  - Automatic paragraph detection with sophisticated text segmentation
  - Mixed font rendering for documents with both English and CJK characters
  - HTML character escaping and substitution for problematic characters
  - Bookmark preservation from original PDF to bilingual output
  - Environment variable support for API keys
  - Resume interrupted translations using cache

  The tool should be production-ready with comprehensive error handling, logging, and user-friendly CLI experience.
Tweet image
一个创意工作者的线上交流社区