Implement dumping for odt in addition to docx

This commit is contained in:
Kovid Goyal 2014-03-29 15:38:15 +05:30
parent 30bbd17481
commit 2a333aa46a
2 changed files with 7 additions and 6 deletions

View File

@ -279,10 +279,10 @@ def main(args=sys.argv):
main(['calibre-diff'] + args[1:]) main(['calibre-diff'] + args[1:])
elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}: elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'py', 'recipe'}:
run_script(args[1], args[2:]) run_script(args[1], args[2:])
elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'mobi', 'azw', 'azw3', 'docx'}: elif len(args) >= 2 and args[1].rpartition('.')[-1] in {'mobi', 'azw', 'azw3', 'docx', 'odt'}:
for path in args[1:]: for path in args[1:]:
ext = path.rpartition('.')[-1] ext = path.rpartition('.')[-1]
if ext == 'docx': if ext in {'docx', 'odt'}:
from calibre.ebooks.docx.dump import dump from calibre.ebooks.docx.dump import dump
dump(path) dump(path)
elif ext in {'mobi', 'azw', 'azw3'}: elif ext in {'mobi', 'azw', 'azw3'}:

View File

@ -25,10 +25,11 @@ def dump(path):
if f.endswith('.xml') or f.endswith('.rels'): if f.endswith('.xml') or f.endswith('.rels'):
with open(f, 'r+b') as stream: with open(f, 'r+b') as stream:
raw = stream.read() raw = stream.read()
root = etree.fromstring(raw) if raw:
stream.seek(0) root = etree.fromstring(raw)
stream.truncate() stream.seek(0)
stream.write(etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True)) stream.truncate()
stream.write(etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True))
print (path, 'dumped to', dest) print (path, 'dumped to', dest)