View Single Post
Old 01-10-2024, 04:20 AM   #1218
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,211
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
OK. Try this:

Code:
import subprocess
import os
from calibre.constants import iswindows, isosx

def run(gui, settings, chain):
    fmt = 'TXTZ'
    db = gui.current_db
    output_dir = 'D:\Temp'
    path_to_binary = 'C:\Program Files\7-Zip\7z.exe'
    clean_env = dict(os.environ)
    kw = {'env': clean_env, 'shell':True, 'capture_output': True}
    if iswindows:
        kw['DETACHED_PROCESS'] = 0x00000008
        del clean_env['PATH']
    elif isosx:
        if path_to_binary.lower().endswith(".app"):
            path_to_binary = 'open -a ' + path_to_binary
    else: #Linux
        clean_env['LD_LIBRARY_PATH'] = ''
    for book_id in chain.scope().get_book_ids():
        path_to_format = db.format_abspath(book_id, fmt, index_is_id=True)
        title = db.title(book_id, index_is_id=True)
        if not path_to_format:
            print(f'Book {title} does not have format: {fmt}')
            continue
        cmd = f'"{path_to_binary}" e {path_to_format} -o"{output_dir}" file-to-extract -y'
        result = subprocess.run(cmd, **kw)
        if result.returncode != 0:
            prints(f'Command error: Return code is {result.returncode} when running command for file: {path_to_format}\ncmd: {cmd}')
            sys.stdout.buffer.write(b'stderr: ' + result.stderr)
            prints()

Last edited by capink; 01-10-2024 at 04:26 AM.
capink is online now   Reply With Quote