Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loop into epub file and make changes based on pattern . #279

Open
catafest opened this issue Apr 9, 2023 · 0 comments
Open

loop into epub file and make changes based on pattern . #279

catafest opened this issue Apr 9, 2023 · 0 comments

Comments

@catafest
Copy link

catafest commented Apr 9, 2023

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, {})```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant