change-epub-formatting.py

#!/usr/bin/python
#DTPD#
import subprocess
import os.path
import sys
import glob

def fixfile(in_file_name):
  ebookconvert = r'C:\PROGRA~2\Calibre2\ebook-convert.exe'
  #ebookconvert = '/usr/bin/ebook-convert'
  base = os.path.basename(in_file_name)
  dirn = os.path.dirname(in_file_name)
  newdir = 'myformat'
  try:
    os.mkdir(os.path.join(dirn, newdir))
  except OSError:
    pass
  out_file_name = os.path.join(dirn, newdir, base)

  cmd = [ebookconvert, in_file_name, out_file_name,
    '--preserve-cover-aspect-ratio',
    '--change-justification', 'left',
    '--margin-bottom', '4',
    '--margin-left', '4',
    '--margin-right', '4',
    '--margin-top', '4',
    '--remove-paragraph-spacing']
  print ' '.join(cmd), '\n'
  sproc = subprocess.Popen(cmd)
  sproc.wait()

if __name__ == '__main__':
  for arg in sys.argv[1:]:
    for filen in glob.glob(arg):
      fixfile(filen)
  exit(0)