Closed manujchandra closed 5 months ago
Hi!
Unfortunately, the get_content()
inside the write_epub()
function is causing the information inside the <head>
tag to be lost.
I also incurred this issue, but found a workaround in another issue here: https://github.com/aerkalov/ebooklib/issues/221#issuecomment-1489878202
I hope this helps!
Best, Sascha
Hi,
I found the same thing. The issue is that ebooklib writes its own<head>
section to each HTML file, and this replaces any header you may have written yourself.
It's possible to specify a CSS file by adding an appropriate EpubItem
to each chapter as you go. If you want to put the sytlesheet in a folder called styles, as is usual, you have to jump through a few hoops to get the name resolved correctly.
This is what I do. I create two EpubItem
s, one is used to get the style sheet included in the ebook, the other is to create the reference to it from each chapter.
# Create EpubItem object to include the CSS in the ebook
doc_style = epub.EpubItem(
uid = "doc_style",
file_name = "styles/my_styles.css",
media_type = "text/css",
content = open("../my_styles.css").read() )
# Add the style sheet to the book
journal_book.add_item(doc_style)
# ... and a second CSS object with a relative path to be referenced by
# chapters whose source is not in the EBOOK root (e.g. in text/).
# (we don't need any content, it's not going to be added to the document,
# it's just a holder for a relative path to the real CSS file)
#
# NOTE: This is very hacky and may one day stop working.
doc_style_ref = epub.EpubItem(
uid = "doc_style",
file_name = "../styles/my_styles.css",
media_type = "text/css",
content = '' )
Note the difference between the file_name
fields of the two.
Then, as each chapter is being processed I have:
chapter.add_item( doc_style_ref )
To add the object containing the relative path name to the chapter.
ebooklib doesn't have any special logic to handle relative paths.
I hope this is clear ... and useful!
I was able to implement this solution, which seems to be working for my use case.
Hi,
Upon experimentation, I have found that before
epub.write_epub(output_file, new_book)
the CSS link is present.But once this chapter is written to disk, using
epub.write_epub(output_file, new_book)
the CSS link is deleted.The full script I am using can be found here
So, my conclusion is that
write_epub
is deleting the css link.Is there any way to ensure the CSS link is written to the disk? Thanks. Or is there something I am doing wrong? My intention is to take an xhtml from the epub, split it by size, ensuring that the styles are applied to each chunk, and save the epub part.
I am using this epub as a sample.
Thanks in advance.