Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Conversion

Notices

Reply
 
Thread Tools Search this Thread
Old 08-25-2023, 08:23 PM   #1
miki.xxx
Junior Member
miki.xxx began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Aug 2023
Device: kindle paperwhite 11
Question python and converting filetypes

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 :)
Attached Thumbnails
Click image for larger version

Name:	Screenshot 2023-08-26 020054.png
Views:	53
Size:	201.8 KB
ID:	203353  

Last edited by theducks; 08-25-2023 at 10:36 PM. Reason: Please Spoiler Log files
miki.xxx is offline   Reply With Quote
Old 08-25-2023, 11:04 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,878
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
ebook-convert doesnt care about how it is launched. Most likely you are running you script before the download is completed leading to a partial/corrupted file being converted.
kovidgoyal is offline   Reply With Quote
Old 08-26-2023, 06:57 AM   #3
miki.xxx
Junior Member
miki.xxx began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Aug 2023
Device: kindle paperwhite 11
Thanks so much its working now
miki.xxx is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting ePub Python coding books to PDF outputs Python code far too small lysakowski Conversion 9 04-30-2023 05:44 AM
Batch Converting Ebooks with Calibre and Python jmeb Conversion 17 10-20-2018 11:16 AM
Changing the filetypes associated with KPVBooklet? Gigahawk Kindle Developer's Corner 1 02-12-2016 08:11 AM
More filetypes? Perkin Editor 1 11-25-2014 09:48 PM
Export - different filetypes marcl Calibre 2 01-14-2014 05:30 AM


All times are GMT -4. The time now is 01:00 AM.


MobileRead.com is a privately owned, operated and funded community.