Hello! Sorry in advance if im posting in the wrong section! I am creating a python (3.11) script that converts epub to azw3 and im encountering an error that says "EPUB appears to be invalid ZIP file, trying a more forgiving ZIP parser". This occurs when running a command with subprocess.run. I suspected a corrupted file, however when i enter the command manually into the cli, the file sucessfully converts. To clarify, I copy paste the command obtained by python after "Running command: " into the cli and it works. I am unsure of whether this is a problem with my code or python itself when handling epub files or some kind of incompatibility with calibre... I was wondering if anybody has any suggestions to fix or workaround this error. Here is the code:
Spoiler:
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
import wmi
from pathlib import Path
import subprocess
def is_kindle_connected():
c = wmi.WMI()
for device in c.Win32_PnPEntity():
if "Kindle" in device.Caption:
return True
return False
if is_kindle_connected():
print("Kindle device connected.")
else:
print("Kindle device not connected.")
def get_kindle_documents_folder():
kindle_documents_folder = None
try:
output = subprocess.check_output("wmic logicaldisk get caption,volumename", shell=True)
lines = output.decode("utf-8").splitlines()
for line in lines:
if "Kindle" in line:
kindle_drive_letter = line.split()[0]
kindle_documents_folder = os.path.join(kindle_drive_letter, "Documents")
break
except subprocess.CalledProcessError:
pass
return kindle_documents_folder
kindle_documents_folder = get_kindle_documents_folder()
if kindle_documents_folder:
print("Kindle Documents Folder:", kindle_documents_folder)
else:
print("Kindle drive not found.")
def get_downloads_path():
return os.path.join(os.path.expanduser("~"), "Downloads")
class Handler(FileSystemEventHandler):
def __init__(self):
super().__init__()
def on_created(self, event):
if event.is_directory:
return
file_name = os.path.basename(event.src_path)
if file_name.endswith(".epub"):
input_ebook_path = os.path.join(get_downloads_path(), file_name)
print(input_ebook_path)
output_ebook_path = os.path.join(kindle_documents_folder, file_name[:-5] + ".azw3")
print(output_ebook_path)
command = ["ebook-convert", str(input_ebook_path), str(output_ebook_path), "--output-profile", "kindle"]
print("Running command:", " ".join(command))
try:
subprocess.run(["ebook-convert",str(input_ebook_path),str(output_ebook_pa th),"--output-profile","kindle"], check=True)
print("Conversion successful!")
except subprocess.CalledProcessError:
print("Conversion failed.")
if __name__ == "__main__":
downloads_path = get_downloads_path()
if downloads_path:
print("Downloads Path:", downloads_path)
else:
print("Unsupported operating system.")
sys.exit(1)
event_handler=Handler()
observer = Observer()
observer.schedule(event_handler, downloads_path, recursive=True)
observer.start()
print("monitoring...")
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
and heres the error that i received:
C:\Users\Domovyk\Downloads\Stardust_-_Neil_Gaiman(1).epub
D:Documents\Stardust_-_Neil_Gaiman(1).azw3
Running command: ebook-convert C:\Users\Domovyk\Downloads\Stardust_-_Neil_Gaiman(1).epub D:Documents\Stardust_-_Neil_Gaiman(1).azw3 --output-profile kindle
Conversion options changed from defaults:
output_profile: 'kindle'
1% Converting input to HTML...
InputFormatPlugin: EPUB Input running
on C:\Users\Domovyk\Downloads\Stardust_-_Neil_Gaiman(1).epub
EPUB appears to be invalid ZIP file, trying a more forgiving ZIP parser
Traceback (most recent call last):
File "calibre\ebooks\conversion\plugins\epub_input. py", line 259, in convert
File "calibre\utils\zipfile.py", line 774, in __init__
File "calibre\utils\zipfile.py", line 809, in _GetContents
File "calibre\utils\zipfile.py", line 824, in _RealGetContents
calibre.utils.zipfile.BadZipfile: File is not a zip file
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "runpy.py", line 196, in _run_module_as_main
File "runpy.py", line 86, in _run_code
File "site.py", line 83, in <module>
File "site.py", line 78, in main
File "site.py", line 50, in run_entry_point
File "calibre\ebooks\conversion\cli.py", line 419, in main
File "calibre\ebooks\conversion\plumber.py", line 1108, in run
File "calibre\customize\conversion.py", line 242, in __call__
File "calibre\ebooks\conversion\plugins\epub_input. py", line 266, in convert
File "calibre\utils\localunzip.py", line 252, in extractall
File "calibre\utils\localunzip.py", line 239, in _extractall
ValueError: Not a ZIP file
Conversion failed.
Thank you for reading :)