diff --git a/src/calibre/ebooks/docx/to_html.py b/src/calibre/ebooks/docx/to_html.py
index 0bb67ad425..08cf8200e5 100644
--- a/src/calibre/ebooks/docx/to_html.py
+++ b/src/calibre/ebooks/docx/to_html.py
@@ -343,8 +343,11 @@ class Convert(object):
opf.create_spine(['index.html'])
if self.cover_image is not None:
opf.guide.set_cover(self.cover_image)
- with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(os.path.join(self.dest_dir, 'toc.ncx'), 'wb') as ncx:
+ toc_file = os.path.join(self.dest_dir, 'toc.ncx')
+ with open(os.path.join(self.dest_dir, 'metadata.opf'), 'wb') as of, open(toc_file, 'wb') as ncx:
opf.render(of, ncx, 'toc.ncx')
+ if os.path.getsize(toc_file) == 0:
+ os.remove(toc_file)
return os.path.join(self.dest_dir, 'metadata.opf')
def read_block_anchors(self, doc):
diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py
index 3fc21d1bb7..b97e5c962a 100644
--- a/src/calibre/ebooks/oeb/polish/container.py
+++ b/src/calibre/ebooks/oeb/polish/container.py
@@ -661,7 +661,7 @@ class Container(object): # {{{
package.append(item)
return item
- def generate_item(self, name, id_prefix=None, media_type=None):
+ def generate_item(self, name, id_prefix=None, media_type=None, unique_href=True):
'''Add an item to the manifest with href derived from the given
name. Ensures uniqueness of href and id automatically. Returns
generated item.'''
@@ -681,10 +681,11 @@ class Container(object): # {{{
def exists(h):
return self.exists(self.href_to_name(h, self.opf_name))
- c = 0
- while href in all_names or exists(href):
- c += 1
- href = '%s_%d.%s'%(base, c, ext)
+ if unique_href:
+ c = 0
+ while href in all_names or exists(href):
+ c += 1
+ href = '%s_%d.%s'%(base, c, ext)
manifest = self.opf_xpath('//opf:manifest')[0]
item = manifest.makeelement(OPF('item'),
id=item_id, href=href)
diff --git a/src/calibre/ebooks/oeb/polish/import_book.py b/src/calibre/ebooks/oeb/polish/import_book.py
index 0275f70e8b..9ecf1d7e9d 100644
--- a/src/calibre/ebooks/oeb/polish/import_book.py
+++ b/src/calibre/ebooks/oeb/polish/import_book.py
@@ -23,10 +23,10 @@ def auto_fill_manifest(container):
for name, mt in container.mime_map.iteritems():
if name not in manifest_name_map and not container.ok_to_be_unmanifested(name):
- mitem = container.generate_item(name)
+ mitem = container.generate_item(name, unique_href=False)
gname = container.href_to_name(mitem.get('href'), container.opf_name)
if gname != name:
- raise ValueError('This should never happen (gname=%r, name=%r)' % (gname, name))
+ raise ValueError('This should never happen (gname=%r, name=%r, href=%r)' % (gname, name, mitem.get('href')))
manifest_name_map[name] = mitem.get('id')
manifest_id_map[mitem.get('id')] = name