Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
script book   2   23725
Python Script 1: Convert ebooks from epub to mobi format

We are starting a series of python scripts which we may use in our daily life to automate the mundane task and save some time.

This is the first article in this series. Recently I bought Amazon's Ebook Reader, kindle paperwhite 3.

I purchased a few books from the kindle store and downloaded most of the books in Epub format. Now kindle doesn't support epub format. You need to convert them to either mobi or azw3 format.

Converting books one by one using some online tool is extremely time-consuming and frustrating. Hence I searched for some tool which might perform the bulk conversion. I found caliber. You may install and use it to convert books from one format to another in bulk.

But if you are more of a terminal guy then you need to read further. First install the calibre. Use the tool if you want to. Or use the below script to convert the books.

from os import listdir, rename
from os.path import isfile, join
import subprocess


# return name of file to be kept after conversion.
# we are just changing the extension. azw3 here.
def get_final_filename(f):
    f = f.split(".")
    filename = ".".join(f[0:-1])
    processed_file_name = filename+".azw3"
    return processed_file_name


# return file extension. pdf or epub or mobi
def get_file_extension(f):
    return f.split(".")[-1]


# list of extensions that needs to be ignored.
ignored_extensions = ["pdf"]

# here all the downloaded files are kept
mypath = "/home/user/Downloads/ebooks/"

# path where converted files are stored
mypath_converted = "/home/user/Downloads/ebooks/kindle/"

# path where processed files will be moved to, clearing the downloaded folder
mypath_processed = "/home/user/Downloads/ebooks/processed/"

raw_files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
converted_files =  [f for f in listdir(mypath_converted) if isfile(join(mypath_converted, f))]

for f in raw_files:
    final_file_name = get_final_filename(f)
    extension = get_file_extension(f)
    if final_file_name not in converted_files and extension not in ignored_extensions:
        print("Converting : "+f)
        try:
            subprocess.call(["ebook-convert",mypath+f,mypath_converted+final_file_name]) 
            s = rename(mypath+f, mypath_processed+f)
            print(s)
        except Exception as e:
            print(e)
    else:
        print("Already exists : "+final_file_name)
 

I have a folder 'ebooks' which contains all the downloaded ebooks.

After files are converted to the required format, they are stored in 'ebooks/kindle' directory and the original file is moved to 'ebooks/processed' directory.


Once calibre is installed, a command-line utility 'ebook-convert' is made available which takes two command-line arguments, name of the file to be converted and name of the output file. We will be calling this command-line utility from our program, passing the file names one by one.


As of now, we are ignoring pdf file as they take a lot of time to convert and require some setting updates in Calibre.

I will leave that up to you.


Kindle unlimited offer : Read over 1 million books.


script book   2   23725
2 comments on 'Python Script 1: Convert Ebooks From Epub To Mobi Format'
Login to comment

Gabriel April 20, 2020, 10:41 a.m.
Helle i have thisCan u help me ?Converting : [myfile.epub[Errno 2] No such file or directory: 'ebook-convert': 'ebook-convert'Thx
Dr Pi. June 9, 2020, 7:31 a.m.
Thankyou for this article, I've used it as the starting point for my code : https://github.com/RGGH/kindle

Related Articles:
Python program to convert Linux file permissions from octal number to rwx string
Python program to convert Linux file permissions from octal number to rwx string, Linux file conversion python program, Python script to convert Linux file permissions...
Python Script 17: Setting bing image of the day as desktop wallpaper
Python Script to Set bing image of the day as desktop wallpaper, Automating the desktop wallpaper change to bing image of day using python code, changing desktop wallpaper using python, downloading an image using python code, updating the desktop wallpaper daily using python...
Python Script 16: Generating word cloud image of a text using python
word cloud python, generating a word cloud of a text using python code, python code to generate a word cloud image of a text. Using word frequencies to generate a word cloud image using a python script...
Python Script 13: Generating ascii code from Image
Generating ascii art from image, converting colored image to ascii code, python script to convert image to ascii code, python code to generate the ascii image from jpg image....
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap