support Kindle Previewer 4 on Windows (#1440)

* initial commit

* refactor kindlegen check

* search for windows kindlegen

* increase search range
This commit is contained in:
Alex Xu
2026-09-15 18:27:43 -07:00
committed by GitHub
3 changed files with 59 additions and 40 deletions
+1
View File
@@ -69,6 +69,7 @@ def modify_path():
os.path.expandvars('%LOCALAPPDATA%\\Amazon\\KC2'),
os.path.expandvars('%LOCALAPPDATA%\\Amazon\\Kindle Previewer 3\\lib\\fc\\bin\\'),
os.path.expandvars('%UserProfile%\\Kindle Previewer 3\\lib\\fc\\bin\\'),
'C:\\Tools',
'C:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin',
'D:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin',
'E:\\Apps\\Kindle Previewer 3\\lib\\fc\\bin',
+7 -32
View File
@@ -32,7 +32,6 @@ import sys
from urllib.parse import unquote
from time import sleep
from shutil import move, rmtree
from subprocess import STDOUT, PIPE, CalledProcessError
import requests
from xml.sax.saxutils import escape
@@ -45,9 +44,9 @@ from PIL.Image import Dither
from .KCC_spread_label import LabelSpreadsDialog
from .shared import HTMLStripper, sanitizeTrace, walkLevel, subprocess_run
from .shared import HTMLStripper, sanitizeTrace, walkLevel
from .comicarchive import SEVENZIP, TAR, available_archive_tools
from .comic2ebook import OS_SORT_KEY, flattenTree, getWorkFolder, removeNonImages, sanitizeTree
from .comic2ebook import OS_SORT_KEY, flattenTree, getWorkFolder, removeNonImages, sanitizeTree, detectKindleGen
from . import __version__
from . import comic2ebook
from . import metadata
@@ -1255,8 +1254,8 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
self.needClean = True
return
if 'MOBI' in GUI.formats[str(GUI.formatBox.currentText())]['format'] and not self.kindleGen:
self.detectKindleGen()
if not self.kindleGen:
if not detectKindleGen(GUI):
self.progress.stop()
GUI.jobList.clear()
self.display_kindlegen_missing()
self.needClean = True
@@ -1384,32 +1383,6 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
self.saveSettings(None)
sys.exit(0)
def detectKindleGen(self, startup=False):
if not sys.platform.startswith('win'):
try:
os.chmod('/usr/local/bin/kindlegen', 0o755)
except Exception:
pass
try:
versionCheck = subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8', errors='ignore', check=True)
self.kindleGen = True
for line in versionCheck.stdout.splitlines():
if 'Amazon kindlegen' in line:
versionCheck = line.split('V')[1].split(' ')[0]
if Version(versionCheck) < Version('2.9'):
self.addMessage('Your <a href="https://www.amazon.com/b?node=23496309011">KindleGen</a>'
' is outdated! MOBI conversion might fail.', 'warning')
break
except (FileNotFoundError, CalledProcessError):
self.kindleGen = False
if startup:
self.display_kindlegen_missing()
except OSError as e:
self.kindleGen = False
if startup:
error = f"kindlegen: {e.strerror}\n\n Re-install or re-open Rosetta/Kindle Previewer/other Intel app?"
self.showDialog(error, 'error')
def __init__(self, kccapp, kccwindow):
global APP, MW, GUI
APP = kccapp
@@ -1442,8 +1415,11 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
except Exception:
self.options = default_options
self.worker = WorkerThread()
self.worker.setObjectName('worker')
self.versionCheck = VersionThread(self.startNumber2)
self.versionCheck.setObjectName('version')
self.progress = ProgressThread()
self.progress.setObjectName('progress')
self.tray = SystemTrayIcon()
self.conversionAlive = False
self.needClean = True
@@ -1674,7 +1650,6 @@ class KCCGUI(KCC_ui.Ui_mainWindow):
if not any([self.tar, self.sevenzip]):
self.addMessage('<a href="https://github.com/ciromattia/kcc#7-zip">Install 7z (link)</a>'
' to enable CBZ/CBR/ZIP/etc processing.', 'warning')
self.detectKindleGen(True)
APP.messageFromOtherInstance.connect(self.handleMessage)
GUI.defaultOutputFolderButton.clicked.connect(self.selectDefaultOutputFolder)
+51 -8
View File
@@ -40,6 +40,7 @@ from shutil import move, copytree, rmtree
from multiprocessing import Pool, cpu_count
from uuid import uuid4
from natsort import os_sort_keygen, os_sorted
from packaging.version import Version
from slugify import slugify as slugify_ext
from PIL import Image, ImageFile, ImageOps
from pathlib import Path
@@ -1749,16 +1750,58 @@ def checkTools(source):
print('ERROR: 7z is missing!')
sys.exit(1)
if options.format == 'MOBI':
try:
subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, check=True)
except (FileNotFoundError, CalledProcessError):
print('ERROR: KindleGen is missing!')
sys.exit(1)
except OSError as e:
print(f"kindlegen: {e.strerror}")
print('Re-install or re-open Rosetta/Kindle Previewer/other Intel app?')
if not detectKindleGen():
sys.exit(1)
def detectKindleGen(GUI=None):
if sys.platform.startswith('win'):
if not _detectKindleGen():
start = perf_counter()
try:
for i in range(0, 200):
for j in range(0, 20):
for k in range (0, 20):
path = f'C:\\Program Files\\WindowsApps\\KindlePreviewerApp_4.{i}.{j}.{k}_x64__ek06e0aw29nma'
if os.path.exists(path):
kindlegen_path = os.path.join(path, 'KindlePreviewerApp\\resources\\KFXGen\\bin\\kindlegen.exe')
tool_path = 'C:\\Tools'
if not os.path.isdir(tool_path):
os.mkdir(tool_path)
shutil.copy2(kindlegen_path, tool_path)
except Exception:
pass
end = perf_counter()
print(f"Search for Windows KindleGen: {end - start} sec")
return _detectKindleGen(GUI)
else:
try:
os.chmod('/usr/local/bin/kindlegen', 0o755)
except Exception:
pass
return _detectKindleGen(GUI)
def _detectKindleGen(GUI=None):
try:
versionCheck = subprocess_run(['kindlegen', '-locale', 'en'], stdout=PIPE, stderr=STDOUT, encoding='UTF-8', errors='ignore', check=True)
for line in versionCheck.stdout.splitlines():
if 'Amazon kindlegen' in line:
versionCheck = line.split('V')[1].split(' ')[0]
if Version(versionCheck) < Version('2.9'):
if GUI:
GUI.addMessage('Your <a href="https://www.amazon.com/b?node=23496309011">KindleGen</a>'
' is outdated! MOBI conversion might fail.', 'warning')
break
return True
except (FileNotFoundError, CalledProcessError):
print('ERROR: KindleGen is missing!')
return False
except (OSError, Exception) as e:
print(f"kindlegen: {e.strerror}")
print('Re-install or re-open Rosetta/Kindle Previewer/other Intel app?')
if GUI:
error = f"kindlegen: {e.strerror}\n\n Re-install or re-open Rosetta/Kindle Previewer/other Intel app?"
GUI.showDialog(error, 'error')
return False
def checkPre(source='KCC-'):
# Make sure that all temporary files are gone