Skip to main content

EPUB Dual Language

A Python CLI that turns an EPUB into a dual-language EPUB using OpenRouter.

The default output is paragraph aligned:

Original paragraph
Translated paragraph

This tends to work well for novels and essays because both languages stay close together without interrupting every sentence.

Features

  • Translates EPUB books with OpenRouter chat models.
  • Preserves the original EPUB container and stylesheet links.
  • Produces a separate dual-language edition with updated title and EPUB identifiers.
  • Supports paragraph or sentence interleaving.
  • Supports chapter-based or token-budget chunking.
  • Can translate only the beginning of a book for quick tests.
  • Can add rare bracketed translator notes for important cultural or archaic references.
  • Recovers completed items from truncated JSON responses instead of retranslating them.
  • Isolates model-filtered passages by splitting failed batches.
  • Retries temporary network, rate-limit, and server failures with backoff.
  • Can use a fallback model for a passage rejected by the primary model.

Example of a generated dual-language EPUB (left) vs. original (right) opened in Calibre:

example

Installation

Install from PyPI:

pip install epub-dual-language

Python 3.10 or newer is required.

For development, clone the repository and install it into a Python environment:

pip install -e ".[dev]"

OpenRouter API Key

The CLI reads OPENROUTER_API_KEY from the environment. You can pass it directly when you run the command.

macOS/Linux:

OPENROUTER_API_KEY=your_openrouter_key epub-dual-language "path/to/book.epub" "English"

PowerShell:

$env:OPENROUTER_API_KEY="your_openrouter_key"; epub-dual-language "path/to/book.epub" "English"

Windows cmd:

set OPENROUTER_API_KEY=your_openrouter_key && epub-dual-language "path/to/book.epub" "English"

You can also create a .env file in the directory where you run the command:

OPENROUTER_API_KEY=your_openrouter_key

When you call epub-dual-language, it loads .env from the current working directory. An environment variable already set in your shell takes priority over the .env value. The repository's own .env is ignored by git.

Basic Usage

epub-dual-language "path/to/book.epub" "English"

Write to a specific output path:

epub-dual-language "path/to/book.epub" "English" --output "output/book.dual.epub"

Translate only a small beginning sample:

epub-dual-language "path/to/book.epub" "English" --limit-units 30 --output "output/sample.dual.epub"

Run without calling OpenRouter, for testing:

epub-dual-language "path/to/book.epub" "English" --dry-run --limit-units 20

Options

epub-dual-language INPUT_EPUB TARGET_LANGUAGE \
  --source-language German \
  --model google/gemini-3-flash-preview \
  --fallback-model provider/fallback-model \
  --api-attempts 3 \
  --api-timeout 120 \
  --chunking tokens \
  --max-tokens 4000 \
  --layout paragraph \
  --translation-style italic-muted \
  --translator-notes \
  --limit-units 30 \
  --output output/sample.dual.epub

Important options:

  • --model: primary OpenRouter model.
  • --fallback-model: optional model used only when one isolated passage fails with the primary model.
  • --api-attempts: attempts for temporary connection, timeout, HTTP 408/429, or HTTP 5xx failures. Default: 3.
  • --api-timeout: timeout in seconds for each API request. Default: 120.
  • --max-tokens: estimated input budget for each initial translation batch. Default: 6000.
  • --chunking: tokens or chapters.
  • --layout: paragraph or sentence.
  • --translation-style: italic, muted, italic-muted, or plain.
  • --translator-notes: permits rare, short translator notes.
  • --limit-units: translates only the first N extracted units.
  • --dry-run: creates a sample EPUB without calling OpenRouter.

Metadata

The generated EPUB is marked as a separate dual-language edition.

For example, an input title like:

Original Title

becomes:

Original Title (Dual English)

The OPF unique identifier is also replaced with a new deterministic dual-edition UUID. This reduces the chance that Calibre or an e-reader treats the translated edition as the same book as the source.

Chunking

--chunking tokens is the default. It walks the EPUB in reading order and packs paragraphs or sentences into model requests until the estimated --max-tokens budget is reached. Small samples can therefore fit into a single API call even if they cross EPUB file boundaries.

--chunking chapters sends one EPUB spine document at a time. Many EPUBs store chapters as separate XHTML files, so this often approximates chapter-by-chapter translation. It is simple, but very long chapters can create large requests.

Token counts are estimates based on text-like tokens, not provider billing tokens.

Smaller values reduce the chance of truncated model output but create more API calls. A value around 3000–4000 is a practical starting point for paragraph translation.

API Failure Handling

The translator distinguishes between temporary service failures, malformed model output, content filtering, and fatal API configuration errors.

Failure Behavior
Connection error or timeout Retry the same batch with exponential backoff.
HTTP 408, 429, 500, 502, 503, or 504 Retry the same batch with exponential backoff.
Truncated or malformed translation JSON Keep every complete translation object that can be recovered, then request only unresolved items.
content_filter, model error, or unusable model output Split the unresolved batch in half immediately and continue recursively.
One isolated passage still fails Try --fallback-model when configured; otherwise insert an error placeholder and continue the EPUB.
Authentication, permission, invalid model, or other non-retryable HTTP error Stop the run because splitting would not repair the request.
Temporary API failure after all configured attempts Stop the run rather than fill the book with misleading placeholders.

The placeholder written for an irreducible passage is:

[TRANSLATION ERROR: This passage could not be translated.]

This behavior is designed for cases where a specific literary passage triggers a model or provider filter. Successful passages are retained in their original order, and one rejected passage does not prevent the rest of the EPUB from being generated.

--api-attempts applies only to failures likely to be temporary. Model-output failures are not retried unchanged: they are recovered or split immediately.

Example using a fallback model:

epub-dual-language "book.epub" "Spanish" \
  --source-language French \
  --model google/gemini-3-flash-preview \
  --fallback-model provider/fallback-model \
  --chunking tokens \
  --max-tokens 4000

If a malformed response contains valid items 0 through 6 and truncates during item 7, the translator keeps items 0 through 6 and requests only item 7 and the remaining unresolved items. It does not pay to translate the successful prefix again.

Layouts

--layout paragraph is the default and recommended mode for reading:

Original paragraph
Translated paragraph

--layout sentence alternates original and translated sentences inside each paragraph. It can be useful for language study, but it is less pleasant for uninterrupted reading.

Translation Styling

Translated text is italic by default so it is easy to distinguish from the original without changing the book too aggressively.

epub-dual-language "path/to/book.epub" "English" --translation-style italic

Available styles:

  • italic
  • muted
  • italic-muted
  • plain

Translator Notes

Use --translator-notes to let the model add rare, short notes in square brackets for genuinely important archaic, historical, or culturally specific references:

epub-dual-language "path/to/book.epub" "English" --translator-notes

The prompt explicitly asks the model not to explain ordinary lines or add notes everywhere.

Development

Run tests:

pytest

Run the CLI module directly:

python -m epub_dual_language.cli "path/to/book.epub" "English" --dry-run --limit-units 10

Metadata

Release files for epub-dual-language 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for epub-dual-language 0.1.6
File Size Uploaded Provenance Actions
epub_dual_language-0.1.6.tar.gz 21.3 kB With provenance Details for epub_dual_language-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for epub-dual-language 0.1.6
File Interpreter ABI Platform Actions
epub_dual_language-0.1.6-py3-none-any.whl Python 3 none any Details for epub_dual_language-0.1.6-py3-none-any.whl

Total release size: 38.2 kB

Release files / epub_dual_language-0.1.6.tar.gz

Download URL epub_dual_language-0.1.6.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
76e499944cf7684292b4aa0aaf2f6dd82625905d7d32eb2af87b04a0133b4374
BLAKE2b-256 checksum
How to use checksums
5d7107dc9d9a85351366988da3806d5c735cc1f88965cfbded8aadf7d1b3a2a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Release files / epub_dual_language-0.1.6-py3-none-any.whl

Download URL epub_dual_language-0.1.6-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b9a3891f099a34a0785eeb8d0c624eb0856c8e91dc46188b6cf04477b5ca8635
BLAKE2b-256 checksum
How to use checksums
49819d7cd1de327738a01e4f8ad69929c103b7ff346b258e4f3239ae5b3ef1b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

Security for epub-dual-language 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for epub_dual_language-0.1.6.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page