ebooklib icon indicating copy to clipboard operation
ebooklib copied to clipboard

Exception caught when switch the order of code

Open JeffersonQin opened this issue 2 years ago • 1 comments

The following code snippet will work:

about_content = epub.EpubHtml(title='Title', file_name='Text/about.xhtml', lang='zh-CN', content=about_text)
i = 0
epub_nav = ['nav', about_content]
epub_toc = [about_content]
epub_contents = [about_content]
for content in _contents:
	i += 1
	item = (epub.EpubHtml(title=content['title'], file_name=f'Text/Section{i}.xhtml', lang='zh-CN', content=content['content']))
	epub_toc.append(item)
	epub_nav.append(item)
	epub_contents.append(item)

book.add_item(about_content)
for epub_content in epub_contents:
	book.add_item(epub_content)

# configure book
book.toc = tuple(epub_toc)

# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

book.spine = epub_nav

epub.write_epub(os.path.join(path, f'{self.title}.epub'), book, {})

But when I change the order of the code, for example, I perform book.add_item immediately after epub.Html() in the loop, I will receive error.

about_content = epub.EpubHtml(title='Title', file_name='Text/about.xhtml', lang='zh-CN', content=about_text)
i = 0
epub_nav = ['nav', about_content]
epub_toc = [about_content]
epub_contents = [about_content]
for content in _contents:
	i += 1
	item = (epub.EpubHtml(title=content['title'], file_name=f'Text/Section{i}.xhtml', lang='zh-CN', content=content['content']))
	book.add_item(item)
	epub_toc.append(item)
	epub_nav.append(item)
	epub_contents.append(item)

# book.add_item(about_content)
# for epub_content in epub_contents:
# 	book.add_item(epub_content)

# configure book
book.toc = tuple(epub_toc)

# add default NCX and Nav file
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())

book.spine = epub_nav

epub.write_epub(os.path.join(path, f'{self.title}.epub'), book, {})

Traceback:

Error: TypeError("Argument must be bytes or unicode, got 'NoneType'")
Traceback (most recent call last):
  File "D:\CodeSpace\lightnovel_epub\lightnovel.py", line 217, in write_epub
    epub.write_epub(os.path.join(path, f'{self.title}.epub'), book, {})
  File "C:\Users\JeffersonQin\AppData\Local\Programs\Python\Python38\lib\site-packages\ebooklib\epub.py", line 1717, in write_epub
    epub.write()
  File "C:\Users\JeffersonQin\AppData\Local\Programs\Python\Python38\lib\site-packages\ebooklib\epub.py", line 1363, in write
    self._write_opf()
  File "C:\Users\JeffersonQin\AppData\Local\Programs\Python\Python38\lib\site-packages\ebooklib\epub.py", line 1099, in _write_opf
    self._write_opf_spine(root, _ncx_id)
  File "C:\Users\JeffersonQin\AppData\Local\Programs\Python\Python38\lib\site-packages\ebooklib\epub.py", line 1045, in _write_opf_spine
    etree.SubElement(spine, 'itemref', opts)
  File "src\lxml\etree.pyx", line 3136, in lxml.etree.SubElement
  File "src\lxml\apihelpers.pxi", line 199, in lxml.etree._makeSubElement
  File "src\lxml\apihelpers.pxi", line 194, in lxml.etree._makeSubElement
  File "src\lxml\apihelpers.pxi", line 323, in lxml.etree._initNodeAttributes
  File "src\lxml\apihelpers.pxi", line 334, in lxml.etree._addAttributeToNode
  File "src\lxml\apihelpers.pxi", line 1538, in lxml.etree._utf8
TypeError: Argument must be bytes or unicode, got 'NoneType'

Also, the wield thing is that I will not receive such error when I am manipulating images. As I perform add_item(epub_image) as soon as after the image instance was created.

JeffersonQin avatar Dec 15 '21 06:12 JeffersonQin

I would say this is happening because in the 2nd example you commented this line:

book.add_item(about_content)

and you are using about_content in epub_nav and epub_toc.

aerkalov avatar Jun 18 '22 21:06 aerkalov