如何在使用ebooklib时将HTML文件插入章节内容中?

huangapple go评论75阅读模式
英文:

How do insert a HTML file in the content of a chapter when using ebooklib?

问题

I'm making an EPUB using the EbookLib library and I'm following along their documentation. I am trying to set the content of a chapter to be the content of a HTML file. The only way I got it to work was giving plain HTML when setting the content.

c1 = epub.EpubHtml(title='Chapter one', file_name='ch1.xhtml', lang='en')

c1.set_content(u'<html><body><h1>Introduction</h1><p>Introduction paragraph.</p></body></html>')'

Is it possible to give a HTML file to be the content of the chapter?

I've tried things like c1.set_content(file_name='ch1.xhtml') but that didn't work, it only accepts plain HTML.

英文:

I'm making an EPUB using the EbookLib library and I'm following along their documentation. I am trying to set the content of a chapter to be the content of a HTML file. The only way I got it to work was giving plain HTML when setting the content.

c1 = epub.EpubHtml(title='Chapter one', file_name='ch1.xhtml', lang='en')

c1.set_content(u'<html><body><h1>Introduction</h1><p>Introduction paragraph.</p></body></html>')'

Is it possible to give a HTML file to be the content of the chapter?

I've tried things like c1.set_content(file_name='ch1.xhtml') but that didn't work, it only accepts plain HTML.

答案1

得分: 1

我明白了!我打开并读取文件存储在一个变量中,然后将该变量传递给set_content函数。发布这个以便将来有人可以使用。

file = open('ch1.xhtml', 'r')
lines = file.read()
c2.set_content(lines)
file.close()
英文:

I figured it out! I'm opening and reading the file in a variable and then passing that variable to the set_content function. Posting this so it could be of use to someone in the future.

file = open('ch1.xhtml', 'r')
lines = file.read()
c2.set_content(lines)
file.close()

huangapple
  • 本文由 发表于 2023年2月6日 05:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定