-
Couldn't load subscription status.
- Fork 2
Automatically Export Book
The full_export_book.py script automates the export of a book into multiple formats (Markdown, PDF, EPUB, DOCX) using Pandoc.
- π Converts relative image paths to absolute paths before export (optional)
- π Handles both Markdown images (
) and HTML<img>/<figure>tags - π Exports book content into multiple formats using Pandoc
- π Converts absolute paths back to relative paths after export (optional)
- π Supports custom arguments for flexible execution
- π Optional EPUB cover image support via
--coverparameter - π Auto-detects language from
metadata.yaml(or override with--lang) - π Poetry integration: Run via
poetry run full-export
Pandoc does not always resolve relative paths correctly, especially when exporting to:
- PDF (via LaTeX)
- EPUB (due to internal resource handling)
- DOCX (for embedded images)
Example problematic image reference:
This may result in broken references or missing images.
Before export, the script automatically converts all image paths to absolute paths:
This ensures:
- No missing images in PDF, EPUB, and DOCX
- Platform-independent behavior (Windows, macOS, Linux)
- Correct image embedding across formats
After export, the script restores relative paths to keep the Markdown clean.
Ensure Pandoc is installed:
π https://pandoc.org/installing.html
pandoc --versionEnsure Python 3.13+ and Poetry are installed:
python3 --version
poetry --versionIf Poetry is missing:
pip install poetryRun:
poetry installpoetry run full-exportThis will:
- Convert images to absolute paths
- Compile the book into Markdown, PDF, EPUB, and DOCX
- Restore relative paths after export
Specify formats using --format (comma-separated):
Available formats:
-
markdown(GitHub Flavored Markdown) pdfepubdocx
Example: Export only PDF and EPUB
poetry run full-export --format pdf,epubUse the --cover option to specify a cover image for the EPUB:
poetry run full-export --format=epub --cover=assets/covers/cover.jpgπ Notes:
-
Only applies to EPUB export
-
If used without
--format=epub, it will be ignored -
Supported formats:
.jpg,.jpeg,.png
If images are already correctly linked, you can skip all image conversion steps:
poetry run full-export --skip-imagesπ Skips both path rewriting and <img> tag transformations.
If you are using <figure> tags (or otherwise want to preserve relative paths), use:
poetry run full-export --keep-relative-pathsβ This will:
- Skip Step 1 (Convert to absolute paths)
- Skip Step 4 (Restore relative paths)
- Leave all image/URL references as-is
π Useful when your publishing environment already handles relative paths correctly.
Some platforms like Epubli and the Tolino network still require EPUB 2 instead of the newer EPUB 3 standard.
To ensure compatibility, use the --epub2 flag:
poetry run full-export --format=epub --cover=assets/covers/cover.jpg --epub2β This will:
-
Instruct Pandoc to export the EPUB in EPUB 2.0 format
-
Avoid common EPUB validation errors like:
-
RSC-005Invalid metadata -
OPF-092Language tag issues (Deutsch (de-DE)βde-DE)
-
π Notes:
-
Only applies to EPUB output
-
Has no effect on PDF, DOCX, or Markdown
-
You can combine it with
--coverand--order
Use this option only if your distribution platform explicitly requires EPUB 2.
π Need more details about EPUB 2?
Check the full guide here:
π Export to EPUB 2 β Compatibility Guide
This page explains:
-
Why some platforms still require EPUB 2
-
How to validate your EPUB file
-
Common pitfalls and how to avoid them
-
Tips for using
--epub2effectively with Pandoc
The script auto-detects the language from config/metadata.yaml.
However, you can override it:
poetry run full-export --lang deπ§ Behavior:
-
If
--langis not provided, the script useslang:frommetadata.yaml -
If both exist and mismatch, a warning is shown
-
If neither is set, defaults to
'en'
Example in config/metadata.yaml:
title: "My Book"
author: "Author Name"
lang: "en"All logs are saved in export.log.
To monitor live:
tail -f export.logIf errors occur, check export.log for debugging.
book-project/
βββ manuscript/
β βββ chapters/
β β βββ 01-introduction.md
β β βββ 02-chapter.md
β β βββ ...
β βββ front-matter/
β β βββ toc.md
β β βββ preface.md
β β βββ foreword.md
β β βββ acknowledgments.md
β βββ back-matter/
β β βββ about-the-author.md
β β βββ appendix.md
β β βββ bibliography.md
β β βββ faq.md
β β βββ glossary.md
β β βββ index.md
β βββ figures/
β β βββ fig1.png
β β βββ fig2.svg
β β βββ ...
β βββ tables/
β β βββ table1.csv
β β βββ table2.csv
β β βββ ...
β βββ references.bib # If using citations (e.g., BibTeX, APA, MLA formats supported)
βββ assets/ # Images, media, illustrations (for book content, cover design, and figures)
β βββ covers/
β β βββ cover-design.png
β βββ figures/
β β βββ diagrams/
β β βββ infographics/
βββ config/ # Project configuration (metadata, styling, and optional Pandoc settings)
β βββ metadata.yaml # Title, author, ISBN, etc. (used for all formats: PDF, EPUB, MOBI)
β βββ styles.css # Custom styles for PDF/eBook
β βββ template.tex # LaTeX template (if needed)
βββ output/ # Compiled book formats
β βββ book.pdf
β βββ book.epub
β βββ book.mobi
β βββ book.docx
βββ scripts/ # Scripts and tools (initialize project, convert book, update metadata, and export formats)
β βββ full_export_book.py # Exports book to all publishing formats with backup
βββ create-project-documentation.md # Documentation for generate the project structure
βββ full-export-documentation.md # Documentation the export
βββ how-to-write.md # Documentation how to use the project structure and save the files
βββ LICENSE # If open-source
βββ pyproject.toml # Configuration file for poetry
βββ README.md # Project description
The script now supports three different ways of handling images. Use the one that fits your workflow:
| Mode | Steps Executed | Paths After Export | When to Use |
|---|---|---|---|
| Default (no flags) | β
Step 1 (convert to absolute) β Step 4 (restore to relative) β Tag conversion |
Restored to relative | Best for Pandoc (ensures images work in PDF/EPUB/DOCX while keeping Markdown clean) |
--skip-images |
β Step 1 β Step 4 β Tag conversion |
Whatever is in your Markdown | Fastest option, skips all image handling (use if your Markdown is already clean) |
--keep-relative-paths |
β Step 1 β Step 4 β Tag handling (if relevant) |
Preserves relative paths | Best when using <figure> or when your toolchain already supports relative paths |
Note:
--skip-imagesand--keep-relative-pathscurrently produce the same result (both skip Steps 1 & 4). Theyβre marked as mutually exclusive to avoid confusion.
flowchart TD
A([Start]) --> B{Flag?}
B -->|--skip-images| C[Skip Step 1\nSkip Step 4\nSkip tag conversion]
B -->|--keep-relative-paths| D[Skip Step 1\nRun tag handling\nSkip Step 4]
B -->|No flags| E[Run Step 1\nRun tag handling to-absolute]
C --> F[Step 2: Prepare output folder\nEnsure metadata]
D --> F
E --> F
F --> G[Step 3: Compile\nmarkdown, pdf, epub, docx]
G --> H{After compile}
H -->|--skip-images| I[Step 4: Skipped\npaths unchanged]
H -->|--keep-relative-paths| J[Step 4: Skipped\npaths remain relative]
H -->|Default| K[Step 4: Restore paths to relative\nTag handling to-relative]
I --> L[Step 5: Validation\nepub, pdf, docx, md]
J --> L
K --> L
Legend
-
Run (green): step is executed
-
Skipped (dark gray): step is not executed
-
Branching is decided in this order:
-
--skip-imagesβ skip all image-related work -
--keep-relative-pathsβ only skip path rewrites (keep relative paths) -
No flags β default behavior (absolute β compile β restore to relative)
-
- Use Default if you want maximum compatibility with Pandoc and platforms like PDF/EPUB/DOCX.
- Use
--skip-imagesif you want speed and have no broken links. - Use
--keep-relative-pathsif you rely on<figure>tags or know that relative paths will be resolved correctly downstream.
If you see:
Command 'pandoc' not foundInstall Pandoc:
sudo apt install pandoc # Ubuntu/Debian
brew install pandoc # macOS
choco install pandoc # Windows-
Ensure you pass
--cover=... -
Use
.jpgor.png -
Use an EPUB reader like Calibre or Thorium to verify
If you see:
[WARNING] This document format requires a nonempty <title> element.
π§ If youβre using
--keep-relative-paths, make sure your target platform supports relative image references. Pandoc + LaTeX for PDF, for example, may still require absolute paths.
Ensure config/metadata.yaml exists.
-
Use absolute paths by default (
--skip-imagesoff) -
Ensure referenced files exist in
assets/
Ensure config/metadata.yaml exists.
If missing, the script will automatically generate a default one.
You'll see:
β οΈβ οΈβ οΈ LANGUAGE MISMATCH DETECTED β οΈβ οΈβ οΈ
Metadata file says: 'de' but CLI argument is: 'en'
Using CLI argument value.
This is just a warning. It still works, but you may want to keep it consistent.
You can now pass a custom cover image for EPUB output using the --cover argument:
poetry run full-export --format=epub --cover=assets/covers/cover-image.jpgβ Requirements:
-
Accepted formats:
.jpg,.jpeg,.png -
Path should be relative to the project root (or absolute)
If you omit the --cover flag, the EPUB will be generated without an embedded cover image.
We've moved the shortcut documentation to its own dedicated page:
π View the Shortcut Reference β
To avoid issues with unsupported characters in EPUB/PDF uploads (especially for Kindle), use the emoji cleanup tool:
poetry run replace-emojisThis script:
-
Replaces emojis in Markdown files with safe printable symbols
-
Processes files inside
front-matter,chapters, andback-matter -
Uses
scripts/emoji_map.pyas its replacement reference
You can easily add new emoji mappings in scripts/emoji_map.py.
Example addition:
EMOJI_MAP = {
...
"π±": "β", # Smartphone icon β symbol
"π‘": "β‘", # Lightbulb icon β lightning bolt
}Just rerun poetry run replace-emojis and the new replacements will apply.
This script helps you create a clean, professional, multi-format export of your book with:
-
π¦ automatic asset handling
-
π multi-language metadata support
-
π» full CLI integration with Poetry
-
β EPUB 2 compatibility for commercial distribution
For emoji compatibility, cover images, and other enhancements, check the Wiki.
π Now ready for use in any book project! π
- π Home
- Project Initialization
- Generate Project Structure
- How to Write a Book
- Developer Workflow & Makefile
- Chapter File Generator
- Generate Images
- Convert Markdown Images
- Bulk Change File Extensions
- Restructure Chapters
- Translate Markdown with DeepL
- Translate with LM Studio
- Translation CLI Commands
- Shortcuts for Translation
- Automatic Book Export
- Shortcuts for Export
- Export HTML Chapters (Comics)
- Export to EPUB 2
- Pandoc Batch Processor
- Export HTML Books to PDF (KDP Ready)
Use this sidebar to navigate all key workflows β from setup to translation, export, and testing.