Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Formats > ePub

Notices

Reply
 
Thread Tools Search this Thread
Old 11-22-2024, 05:20 AM   #1
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
Question Python library to search and replace text?

Hello,

I'd like to 1) grab the article's title from the metadata, and 2) replace the default title with it.

Does someone know of a good library in Python3 I could use, and possibly some code I could borrow (steal)?

Thank you.
Attached Thumbnails
Click image for larger version

Name:	0D4FCC5F-0A7D-4F7F-8A9A-9E35566AB91B.png
Views:	98
Size:	11.2 KB
ID:	212132  
Shohreh is offline   Reply With Quote
Old 11-22-2024, 07:22 AM   #2
Turtle91
A Hairy Wizard
Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.Turtle91 ought to be getting tired of karma fortunes by now.
 
Turtle91's Avatar
 
Posts: 3,348
Karma: 20171571
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
You can use the re module for python regex. Here are the documents.

You’ll also need to open a zip file (epub is just a zip with .epub extension) - you can try the zipfile module, but there may be others - and then you can read the metadata file (.opf) and then use the os module to rename the epub file.
Turtle91 is offline   Reply With Quote
Advert
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
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to replace text with Search and Replace with regex on Calibre darrnih ePub 2 04-02-2024 02:10 AM
Search and Replace Python help Tanjamuse Calibre 2 08-06-2016 05:25 PM
Search and replace with marked text mrmikel Editor 1 05-01-2014 02:07 PM
search replace puts \1(space) in text isabelle1231 Sigil 2 05-21-2011 04:46 PM
Search & replace TEXT ToeRag Calibre 3 04-10-2010 01:44 PM


All times are GMT -4. The time now is 07:37 PM.


MobileRead.com is a privately owned, operated and funded community.