1

This code converts epub file to txt file:

ebook-convert "book.epub" "book.txt"

How can I use it to convert all .epub files in the directory?

I am using Ubuntu.

Code

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)
  • 2
    Read man find. – ctrl-alt-delor Mar 9 at 15:10
  • Thnx, i tried find but something wrong. – silver Mar 9 at 15:15
  • 3
    Show us what you tried, and what went wrong. – ctrl-alt-delor Mar 9 at 15:17
1

For files in a single directory (without recursing into subdirectories) you shouldn't need anything more than a simple shell loop e.g.

for f in ./*.epub; do
  ebook-convert "$f" "${f%.epub}.txt"
done

The shell parameter expansion ${f%.epub} removes the trailing .epub so that you may add the .txt file extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.