This should solve both issues, hopefully:
Code:
import subprocess
import os
import sys
from calibre.constants import iswindows, isosx
#import pathlib
#exec = pathlib.PureWindowsPath(path).as_posix()
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:
del clean_env['PATH']
path_to_binary = os.path.normpath(path_to_binary)
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
if iswindows:
path_to_format = os.path.normpath(path_to_format)
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:
print(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)
print()