'Make a valid epub with ZipFile
So my ePub generator GenEpub.py
has no errors on the Python interpreter or by running zip -t
. Thanks to everyone that assisted me with that issue: Python - ZipFile' object has no attribute 'seek'
My current problem that I cannot create a valid ePub file with the code I made. I have unzipped an ePub that I know was created properly and works, then re-zipped with my script and it still reports this error on epubcheck
.
Hal's-iMac: epubcheck-4.0.2 Hal$ java -jar epubcheck.jar SampleBook.epub
ERROR(PKG-006): SampleBook.epub/SampleBook.epub(-1,-1): Mimetype file entry is missing or is not the first file in the archive.
FATAL(RSC-002): SampleBook.epub/SampleBook.epub(-1,-1): Required META-INF/container.xml resource could not be found.
ERROR(RSC-001): SampleBook.epub/SampleBook.epub(-1,-1): File 'META-INF/container.xml' could not be found.
Check finished with errors
epubcheck completed
Here's the code so far, I try to sync it with my GitHub repository: https://github.com/inferno986return/Damore-essay-ebook
#!/usr/bin/env python
#GenEpub.py - Generates an .epub file from the data provided.
#Ideally with no errors or warnings from epubcheck (needs to be implemented, maybe with the Python wrapper).
import os
import json
import zipfile
with open('metadata.json') as json_file:
data = json.load(json_file)
#The ePub standard requires deflated compression and a compression order.
zf = zipfile.ZipFile(data["fileName"] + '.epub', mode='w', compression=zipfile.ZIP_STORED)
zf.write(data["fileName"] + '/mimetype')
for dirname, subdirs, files in os.walk(data["fileName"] + '/META-INF'):
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname, filename))
for dirname, subdirs, files in os.walk(data["fileName"] + '/EBOOK'):
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname, filename))
zf.close()
#zipfile has a built-in validator for debugging
with open(data["fileName"] + '.epub','r') as f:
if zipfile.is_zipfile(f) is True:
print("ZIP file is valid.")
#Extra debugging information
#print(getinfo.compress_type(zf))
#print(getinfo.compress_size(zf))
#print(getinfo.file_size(zf))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|