Linux Applications

Convert and Optimize Ebooks in Linux

This article will cover a guide on using “ebook-convert” and “ebook-polish” commands available as a part of the Calibre ebook management software suite. These commands can be used to convert ebooks in a variety of ways and you can adapt ebooks for your ereader using various options available under them.

Installing Calibre

You can install Calibre in your Linux distribution from the package manager. To install it in Ubuntu, use the command below:

$ sudo apt install calibre

Once installed, you should now have “ebook-convert” and “ebook-polish” commands available on your system.

You can also get executable binaries that work on all Linux distributions for Calibre, ebook-convert, and ebook-polish commands from here.

Ebook-convert Command

Ebook-convert command allows you to convert ebooks from one format to another. You can also change the look and feel of ebooks while converting to another format. The look and feel option allows you to change font properties, set custom CSS styles, change justification of content,  remove existing CSS styles, embed fonts, remove blank lines, modify indentation, change margins, modify line height, and replace punctuation characters. The ebook-convert command also allows you to change book structure and layout using an option called “Heuristic Processing”. You can use this option to remove hyphens, modify headings, center content, change bad indents, remove blank paragraphs, and so on.

Ebook-convert command can also be used to search and replace contents of an ebook. One of its options allows you to change the cover image as well. You can also use it to modify book metadata like author name, title, year of publishing, and so on. It includes an option to modify the table of contents as well. Below are some examples of the ebook-convert command:

To convert an ebook from “epub” to Kindle compatible “azw3” format, use a command in the following format:

$ ebook-convert file.epub file.azw3

If you are executing pre-compiled binary for ebook-convert command from a local folder, run the following command instead:

$ ./ebook-convert file.epub file.azw3

The first argument is the input file that you want to convert while the second argument is the name of the converted output file. You can change the extension of the output file to any other format. Just replace “.azw3” with any other extension supported by the Calibre and the ebook-convert command. For instance, to convert an “.epub” file to “.mobi“, you will need to run a command in the following format:

$ ebook-convert file.epub file.mobi

To change justification of contents of the ebook, use a command in the following format:

$ ebook-convert file.epub file.azw3 --change-justification justify

The “–change-justification” argument accepts “left“, “right“, “original” and “justify” as possible values. You can know more about all look and feel options from here. In case you want to use multiple options at once, use a command in the following format:

$ ebook-convert file.epub file.azw3 --change-justification justify --remove-paragraph-spacing

If you want to use the heuristic processing feature, you will have to enable it first and then you can use additional options as needed.

$ ebook-convert file.epub file.azw3 –enable-heuristics --disable-dehyphenate

You can learn more about heuristic processing options from here. The complete manual for the ebook-convert command containing all options can be found here.

Ebook-polish Command

The ebook-poilsh command works with “epub” and “azw3” file formats only. It can be used to modify attributes and styles of an existing ebook file. Unlike ebook-convert command, it doesn’t convert an ebook file into another format, but makes changes to an existing ebook file supplied as an argument.

You can use ebook-polish command to add and remove soft hyphens, change cover image, embed custom fonts, compress images and so on. Below is an example of ebook-polish command with multiple options where a “file.epub” is polished into a “polished_file.epub” file.

$ ebook-polish --add-soft-hyphens --upgrade-book file.epub polished_file.epub

You can read more about all options available for ebook-polish command from here.

Script to Batch Convert and Polish Multiple Ebook Files at Once

I have written a small script that can batch convert multiple “epub” files into “azw3” files at once. It also makes use of the ebook-polish command and adds soft-hyphens to the converted “azw3” file. To use this script, paste the code below into a “.sh” file, mark it executable and put the file into the Calibre installation folder.

#! /bin/bash


function convert () {

        filename="$1"

        extension="${filename##*.}"

        root="${filename%.*}"

        outputExtension=".azw3"

        convertedName="${root}_converted${outputExtension}"

        polishedName="${root}_converted_and_polished${outputExtension}"


        echo ""

        echo "++++++ Converting book: $filename ++++++"

        ./ebook-convert "$filename" "$convertedName" \

        --change-justification justify \

        --margin-left 0 \

        --margin-right 0 \

        --margin-top 0 \

        --margin-bottom 0 \

        --remove-paragraph-spacing \

        --remove-paragraph-spacing-indent-size 1.0 \

        --filter-css font-family

        sleep 0.1


        echo ""

        echo "++++++ Polishing book: $convertedName ++++++"

        ./ebook-polish --add-soft-hyphens --upgrade-book "$convertedName" "$polishedName"

        sleep 0.1


        echo ""

        echo "++++++ Removing obsolete file: $convertedName ++++++"

        rm "$convertedName"

        sleep 0.1


        echo ""

        echo "++++++ Done, final book is: $polishedName ++++++"

        echo ""

}


for name in "$@"

do

        echo "++++++ Staring conversion of: "$name" ++++++"

            convert "$name"

            sleep 0.1

done

For instance, if you saved the above code into a “convert_epub_to_azw3.sh” file, you will have to run commands in following patterns:

$ ./convert_epub_to_azw3.sh file.epub

$ ./convert_epub_to_azw3.sh *.epub

After successful conversion, you should get files named as “filename_converted_and_polished.azw3“. You can change the “outputExtension” variable to change the extension of the output file. Though ebook-polish command works with “epub” and “azw3” file formats only. You can also change switches in front of “./ebook-convert” and “./ebook-polish” commands in the script according to your needs.

Conclusion

Ebook-convert and ebook-polish commands provide a useful method to convert ebooks from command line without having to navigate numerous options available in the Calibre GUI. You can also use these commands to automate book conversion using shell scripts in Linux.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.