View Single Post
Old 07-08-2011, 11:00 AM   #1
jmeb
Junior Member
jmeb began at the beginning.
 
Posts: 2
Karma: 10
Join Date: May 2011
Device: Kindle DX
Batch Converting Ebooks with Calibre and Python

Mornin' all

As part of my venturing into Python I've created a python script that runs on my server to batch convert between different ebook formats using calibre's ebook-convert.

The script looks up all files matching one extension (e.g. .epub) and checks for the same filename with another extension (e.g. .mobi). It there isn't a matching file, it will run ebook-convert to create it.

Some things it includes:
* Handles filenames with spaces
* Takes a couple of different command line arguments
- "--have {epub, mobi, lit, html}": tells it what ebook format you have to convert from
- "--want {epub, mobi, lit, html}": tells it what ebook format you want to convert to
- "-s {/PATH/TO/DIR}": source directory for the ebooks you have
-" -d {/PATH/TO/DIR}": destination directory to check for existing ebooks, and drop conversions into.
* If no commands are supplied it will try to convert epub to mobi in the current directory.

For example:
* "python mbmk.py" : will create an .mobi for every .epub without a .mobi counterpart
* "python mbmk.py --have epub --want html -s ../epubs -d ../htmls" will grab a list of epubs from the ../epub directory, and create an html version in the ../htmls directory if needed.
* "python mbmk.py --have mobi --want lit -s ../mobis" creates .lit versions of mobi files in the ../mobi directory
Its written for a linux box, but should work fine on Mac OSX. Windows I doubt....

Thanks for all the work on Calibre. If anyone has suggestions on improving the script I'd be happy to have them.

The script can be found here: http://pastebin.com/XdPZUdpg
jmeb is offline   Reply With Quote