Skip to content

Instantly share code, notes, and snippets.

@mheiber
Last active January 2, 2016 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mheiber/8284902 to your computer and use it in GitHub Desktop.
Save mheiber/8284902 to your computer and use it in GitHub Desktop.
EPUB namespace converted for Google Play. This program changes the namespace format for XHTML files so EPUBs will validate with the Google Play store. Python's ElementTree module for XML parsing changes the namespace format to something Google Play doesn't like.
'''
xmlNamespaceConvert.py
Language: Python 2.7
This program changes the namespace format for XHTML files so EPUBs will
validate with the Google Play store. One use for this is if you edit EPUBs
using Python's elementTree module, which messes with namespaces.
This program changes the format from: '<html:[tag]>' to simply <tag> and makes the appropriate change
to the namespace declaratio.n
Example: '<html:div>' becomes '<div>'.
Instructions:
1. Validate the EPUB with epubcheck and make any necessary adjustments.
2. Unzip the EPUB
3. In the code below, eplace "PROVIDE DIRECTORY HERE" with the path to where you unzipped the EPUB.
4. Run this program: python xmlNamespaceConvert.py
5. Zip the mimetype file and rename to the name of your epub.
"mimetype.zip" rename to "1293095885.zip" where the number is the ISBN
6. Add the META-INF and OPS folders to the zip file.
7. Change the extension of the zip file to .epub .
8. Re-validate before uploading.
'''
directory="PROVIDE DIRECTORY HERE"
import os
import re
os.chdir(os.path.join(directory,'OPS'))
for xFile in os.listdir(os.getcwd()):
if xFile.endswith(".xhtml"):
out=""
with open(xFile,'r') as f:
text=f.read()
out=re.sub('<html:','<', text)
out=re.sub('/html:', '/', out)
out=re.sub('xmlns:html','xmlns',out)
with open(xFile,'w') as f:
f.write(out)
print 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment