Parsing and Rebuilding EPUB Files in Python: Lessons Learned from Building an AI Book Translator
Описание и применение
Построена система перевода EPUB‑книг с сохранением форматирования, используя ebooklib и lxml для парсинга и безопасной замены текста, а также пакетную обработку с LLM.
Задача
Translating EPUB books while preserving formatting, fonts, images, and structure.
Результат
The pipeline successfully translates EPUBs while maintaining formatting and structure, enabling a functional AI book translation service.
Рабочий процесс
- Load EPUB with ebooklib.read_epub
- Iterate over spine items and parse XHTML with lxml
- Handle namespaces for XPath queries
- Extract block-level text while preserving order
- Batch paragraphs for LLM translation
- Replace text in original elements without removing attributes
- Rebuild EPUB with updated content and metadata
Доказательства
We started building LectuLibre, an AI-powered book translation service, with a simple premise: users upload an EPUB or PDF, we translate it using large language models (Claude, DeepSeek), and they download a translated copy.
We settled on ebooklib for the container-level work and lxml for parsing and modifying the XHTML content.
To replace text while preserving the block-level structure, we used lxml to set the text content of each block element.
- Дата записи: 2026-10-17T03:41:25.261Z