A Python tool for converting EPUB ebooks to Markdown format with preserved structure, images, and metadata.
- Structure Preservation: Maintains chapter organization and hierarchy
- Image Support: Extracts and saves images with proper linking
- Metadata Extraction: Saves book metadata (title, author, description, etc.)
- Flexible Output: Choose between separate chapter files or single consolidated file
- Batch Processing: Convert multiple EPUB files at once
- Clean Conversion: Sanitizes filenames and handles various EPUB formats
pip install epub2mdgit clone https://github.com/lavallee/epub2md.git
cd epub_to_md
pip install -e .pip install git+https://github.com/lavallee/epub2md.gitAfter installation, use the epub2md command:
# Convert a single EPUB file
epub2md book.epub
# Convert multiple EPUB files
epub2md book1.epub book2.epub
# Convert all EPUB files in current directory
epub2md *.epub
# Save as single markdown file instead of separate chapters
epub2md book.epub --single-file
# Specify custom output directory
epub2md book.epub --output-dir my_books
# Enable verbose logging
epub2md book.epub --verboseYou can also run it as a Python module:
python -m epub2md book.epubfrom epub2md import EPUBToMarkdownConverter
converter = EPUBToMarkdownConverter(
output_base_dir="output",
single_file=False,
preserve_structure=True
)
success, output_path = converter.convert_epub("book.epub")
if success:
print(f"Conversion successful! Output: {output_path}")
else:
print(f"Conversion failed: {output_path}")output/
book_title_author/
README.md # Table of contents with links
metadata.json # Book metadata
images/ # Extracted images
image1.jpg
image2.png
chapters/ # Individual chapter files
01_chapter_name.md
02_chapter_name.md
output/
book_title_author/
README.md # Table of contents
metadata.json # Book metadata
full_book.md # Complete book in single file
images/ # Extracted images
image1.jpg
image2.png
usage: epub2md [-h] [-o OUTPUT_DIR] [-s] [--no-structure] [-v]
epub_files [epub_files ...]
Convert EPUB files to Markdown format
positional arguments:
epub_files EPUB file(s) to convert
options:
-h, --help show this help message and exit
-o, --output-dir OUTPUT_DIR
Base output directory (default: output)
-s, --single-file Save as single markdown file instead of separate
chapters
--no-structure Do not preserve chapter structure
-v, --verbose Enable verbose logging
- Python 3.8+
- EbookLib
- html2text
- beautifulsoup4
- Pillow
- lxml
MIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- Report issues: GitHub Issues
- Source code: GitHub Repository