View Single Post
Old 11-23-2024, 02:45 PM   #3
Shohreh
Addict
Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.Shohreh ought to be getting tired of karma fortunes by now.
 
Posts: 207
Karma: 304158
Join Date: Jan 2016
Location: France
Device: none
Thanks.

This seems to do the trick:
Code:
#pip install ebooklib
import ebooklib
from ebooklib import epub
from bs4 import BeautifulSoup

INPUTFILE = "input.epub"

#epub.py UserWarning: In the future version we will turn default option ignore_ncx to True.
book = epub.read_epub(INPUTFILE,{"ignore_ncx": True})

#loop through HTML files
items = list(book.get_items_of_type(ebooklib.ITEM_DOCUMENT))
for item in items:
	print('==================================')
	print('NAME : ', item.get_name())
	print('----------------------------------')
	soup = BeautifulSoup(item.get_body_content(), 'html.parser')
	title = soup.find('h1')
	if title and title.string == "Generic title":
		title.string = "New title"
		item.set_content(soup.prettify())
	print('==================================')

epub.write_epub('edited.epub', book)

Last edited by Shohreh; 11-25-2024 at 05:15 PM.
Shohreh is offline   Reply With Quote