Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

Parsing and Rebuilding EPUB Files in Python: Lessons Learned from Building an AI Book Translator

Description and use

A system for translating EPUB books while preserving formatting was built using ebooklib and lxml for parsing and safe text replacement, with batch processing via LLM.

Problem

Translating EPUB books while preserving formatting, fonts, images, and structure.

Outcome

The pipeline successfully translates EPUBs while maintaining formatting and structure, enabling a functional AI book translation service.

Workflow

  • 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

Evidence

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.

Source

We settled on ebooklib for the container-level work and lxml for parsing and modifying the XHTML content.

Source

To replace text while preserving the block-level structure, we used lxml to set the text content of each block element.

Source

  • Record date: 2026-10-17T03:41:25.261Z