View Single Post
Old 08-18-2015, 05:05 AM   #7
Georgy
Junior Member
Georgy began at the beginning.
 
Posts: 4
Karma: 10
Join Date: Aug 2015
Device: ipad
This is script I use for fixing multiple files or all epubs in folder. For sb's sake.

Usage:
calibre-debug batchfix.py -- <file or folder 1> <file or folder 2> ...

batchfix.py:
Code:
from calibre.ebooks.oeb.polish.check.main import run_checks, fix_errors
from calibre.ebooks.oeb.polish.container import get_container

from os import listdir
from os.path import isfile, join, isdir

def fix_file(filepath) :
    try :
        container = get_container(filepath, tweak_mode=True)
        errors = run_checks(container)
        if errors:
            if fix_errors(container, errors):
                print ('Fixed: %s' % filepath)
                container.commit()
            else :
                print ('Unable fix: %s' % filepath)
        else :
            print ('Correct: %s' % filepath)
    except :
        print ('Unhandled error: %s' % filepath)

def fix_folder(folder) :
    epubs = [ f for f in listdir(folder) if isfile(join(folder,f)) and f.lower().endswith('.epub') ]
    for f in epubs :
        fix_file(join(folder,f))

import sys
for arg in sys.argv[1:] :
    if isfile(arg) : fix_file(arg)
    elif isdir(arg) : fix_folder(arg)
    else : print ('Unknown argument: %s' % arg)
Georgy is offline   Reply With Quote