Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to process azw3, epub, and mobi into text files using calibre ebook-convert
directories = ['azw3', 'epub', 'mobi']
conversion_space = "convert"
sink = open('/dev/null', 'w')
def convert_book(input, output):
subprocess.call("ebook-convert" + " " + input + " " + output, shell=True, stdout=sink, stderr=sink)
for directory in directories:
conversion_directory = os.path.join(directory, conversion_space)
if not os.path.exists(conversion_directory):
os.makedirs(conversion_directory)
print("Creating {} directory".format(directory))
# Convert all files ending in the directory name
files_to_convert = [file for file in os.listdir(directory) if directory in file]
for file in files_to_convert:
# Remove extension for conversion to TXT
base = file.split("." + directory)[0]
output = base + ".txt"
full_input = shlex.quote(os.path.join(directory, file))
full_output = shlex.quote(os.path.join(directory, os.path.join(conversion_space, output)))
print("Creating {}".format(output))
convert_book(full_input, full_output)
sink.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment