ebooklib
ebooklib copied to clipboard
loop into epub file and make changes based on pattern .
I tried to add the content of an SVG file named GraphQL into a basic epub file based on a pattern. The epub file comes with this basic text pattern: [[svg=GraphQL.svg]] which will be changed with the SVG file content. Can you help me with this issue? Thank you. This is the source code I used:
# Open the EPUB file
book = epub.read_epub(str(self.epub_file_name))
# Get all HTML files in the book
html_files = [item for item in book.get_items() if item.get_type() == epub.EpubHtml]
# Loop over HTML files and search for SVG references
for html in html_files:
content = html.content.decode('utf-8')
svg_refs = re.findall(r'\[\[svg=([\w.-]+)\]\]', content)
# Loop over SVG references and add them to the book
for svg_ref in svg_refs:
svg_file = os.path.join(self.svg_folder_name, svg_ref)
if os.path.exists(svg_file):
with open(svg_file, 'rb') as f:
svg_content = f.read()
# Add the SVG content to the HTML content
svg_tag = f'<img src="data:image/svg+xml;base64,{base64.b64encode(svg_content).decode()}" />'
content = content.replace(f"[[svg={svg_ref}]]", svg_tag)
else:
logging.error(f'SVG file not found: {svg_file}')
# Save the updated HTML content
html.content = content.encode('utf-8')
# Update the book with new items
book.spine = ['cover'] + html_files + book.spine[len(html_files)+1:]
book.toc = [epub.Link(x.href, x.title, x.title) for x in html_files]
epub.write_epub(self.epub_file_name, book, {})```