Batch convert .mobi ebooks to .epub using Calibre

I got a Kobo Glo eBook reader some time ago. I find it quite awesome: With numerous public libraries in Paris and this device (together with sources of free books like FeedBooks, Baen Free Library or less authoritative sources), it is hard to think I will ever buy another physical book again.

I was recently looking for a way to convert .mobi files to .epub: The Kobo supports .mobi ebooks natively but the support is not as good as with .epub. It turns out Calibre allows just that: It is an open source e-book application for managing a library of ebooks and it does a lot of stuff. It is possible to use the GUI to perform the conversion but Calibre also provides some command-line tools to perform (among other things) conversions in batch. Here is an example of a Python script that uses the ebook-convert tool to convert all .mobi files in a directory to .epub:

# -*- coding: utf-8 *-*

import sys, os, subprocess

_, dirPath = sys.argv

convertedDirPath = os.path.join(dirPath, "__converted")
if not os.path.exists(convertedDirPath):
    os.makedirs(convertedDirPath)

for fileName in os.listdir(dirPath):
    if fileName.endswith(".mobi"):
        rootFileName, _ = os.path.splitext(fileName)
        epubFileName = rootFileName + ".epub"
        mobiFilePath = os.path.join(dirPath, fileName)
        convertedMobiFilePath = os.path.join(convertedDirPath, fileName)
        epubFilePath = os.path.join(dirPath, epubFileName)
        conversionResult = subprocess.call(["ebook-convert", mobiFilePath, epubFilePath])
        if conversionResult == 0:
            os.rename(mobiFilePath, convertedMobiFilePath)

It is then possible to copy the converted files directly to the eReader after plugging it into the computer.

One more nice thing with the Kobo is that it stores its settings in a SQLite database on the device. Therefore there are many tools available to manipulate the content. For example, I saw this Python library that can do just that (although I haven’t tested yet with my version of Kobo).

4 thoughts on “Batch convert .mobi ebooks to .epub using Calibre

  1. Thank you for the script, very useful. Too bad it’s not folder-recursive (folders in folders).

    And a few more info for people trying to use it :
    1) install Python
    2) copy script in a file like “conv.py”
    3) run the file in a command-line/terminal window with “conv.py ” where is the folder containing the .mobi files to be converted.

  2. Just a small change for convenience so that you can simply double-click the script in the folder containing the .mobi files :
    replace line 05 with :
    dirPath = os.getcwd()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s