Last active
October 8, 2023 10:14
-
-
Save vladikk/3b6e1df99020583788eb3a2921375518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3 readfstr.py input.epub output.epub | |
import argparse | |
from ebooklib import epub # pip install ebooklib | |
from bs4 import BeautifulSoup, NavigableString # pip install bs4 | |
def make_half_word_bold(soup, text): | |
new_elements = [] | |
for word in text.split(): | |
n = len(word) | |
half = n // 2 | |
new_element = soup.new_tag("span") | |
b_tag = soup.new_tag("b") | |
b_tag.string = word[:half] | |
new_element.append(b_tag) | |
new_element.append(word[half:]) | |
new_elements.append(new_element) | |
return new_elements | |
def style_epub(input_file, output_file): | |
# Load ePub book | |
book = epub.read_epub(input_file) | |
# Iterate through items in the ePub | |
for item in book.get_items_of_type(9): # 9 is the constant for XHTML | |
# Parse with BeautifulSoup | |
soup = BeautifulSoup(item.content, 'html.parser') | |
# Modify text within tags | |
for tag in soup.find_all(string=True): | |
parent = tag.parent | |
if parent is not None: | |
modified_elements = make_half_word_bold(soup, tag) | |
tag.extract() | |
for elem in modified_elements: | |
parent.append(elem) | |
parent.append(' ') | |
# Update the item content | |
item.content = str(soup).encode('utf-8') | |
# Write out modified ePub | |
epub.write_epub(output_file, book) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Style an ePub file.') | |
parser.add_argument('input_file', type=str, help='Path to the input ePub file') | |
parser.add_argument('output_file', type=str, help='Path for the styled output ePub file') | |
args = parser.parse_args() | |
style_epub(args.input_file, args.output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment