|
|
#1 |
|
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 224
Karma: 304158
Join Date: Jan 2016
Location: France
Device: none
|
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. |
|
|
|
|
|
#2 |
|
A Hairy Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,417
Karma: 20212733
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. |
|
|
|
| Advert | |
|
|
|
|
#3 |
|
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 224
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 06:15 PM. |
|
|
|
![]() |
|
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 03:10 AM |
| Search and Replace Python help | Tanjamuse | Calibre | 2 | 08-06-2016 06:25 PM |
| Search and replace with marked text | mrmikel | Editor | 1 | 05-01-2014 03:07 PM |
| search replace puts \1(space) in text | isabelle1231 | Sigil | 2 | 05-21-2011 05:46 PM |
| Search & replace TEXT | ToeRag | Calibre | 3 | 04-10-2010 02:44 PM |