#GPT-3.5 | Conversation #1113826786598457374
1 messages · Page 1 of 1 (latest)
This code must delete certain text and images from epub files but don't do it. Can you fix it? import glob
from ebooklib import epub
PATH = '/path/to/epub/files'
Path to text file with text to remove
TEXT_FILE = 'text_to_remove.txt'
with open(TEXT_FILE) as f:
text_to_remove = f.read().split('\n\n')
text_to_remove = [t for t in text_to_remove if t]
for filename in glob.iglob(f'{PATH}/*.epub'):
book = epub.read_epub(filename)
for item in book.get_items():
if item.get_type() == ebooklib.ITEM_IMAGE:
book.remove_item(item)
else:
content = item.get_body_content()
for text in text_to_remove:
content = content.replace(text, "")
item.content = content
# Remove cover
book.set_cover(None, None, None)
# Write modified epub
epub.write_epub(filename, book, {})