Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Bender needs help!
-
Hey humans! I have two ebooks, one in french and one in english. English book is the translation of the french book, called Le Petit Prince. I want to place the translated english sentence after every french sentence in the whole book so I can simultaneously read both languages while listening to audiobook. Can anyone please tell me how to do that with pythonista please!
Here is an example what it should look like:
https://drive.google.com/file/d/1gV7zrQB-hQ2WFKV7J-mvfwAfiPpoZE4H/view?usp=drivesdk
-
@Benderrodriguez If your ebooks are .epub, it could be possible.
An .epub file is a .zip, try to rename .epub in .zip and unzip it. You will see different sub-folders and one contains all chapters in xml format. You could analyze these xml files in both languages, hoping there are "synchronized " without any inserted blank chapter or paragraph or line in one of both languages.
Some years ago, I have downloaded an epub Python module, written by Florian Strzelecki, but I don't remember from where. This module could help to read an epub and analyze its content.
Edit: it was on https://bitbucket.org/exirel/epub but page does not exist anymore. I only can find a clone of version 0.5.1 here but "my" version is 0.5.2 that I could upload in GitHub
Anyway, I think that the script you want could be possible but not easy. Good luck
You could read Getting Text from epub Files in Python using EbookLib
-
@Benderrodriguez I write this morning a quick and dirty code to display text of the same epub in two different languages. Of course, the script does not exactly what you hope but you could start from it.
As I don't see immediately how to certify the synchronisation of both texts, this little script displays them in two columns.from bs4 import BeautifulSoup import console import epub import ui def get_epub(fil): global book_file book_file = epub.open_epub(fil) book = epub.Book(book_file) chapters = book.chapters t = '='*40 + '\n' for chapter in chapters: #print(chapter.identifier) soup = BeautifulSoup(chapter, 'html.parser') text_array = [para.get_text() for para in soup.find_all('p')] #print(text_array) for text in text_array: lines = text.split('.') for line in lines: if line != '' and line != ' ': line += '.' line = line.lstrip() #print(line) t += line + '\n' t += '='*40 + '\n' # Try to extract cover # find possible cover in meta cover_id = None met = book_file.opf.metadata for meta in met.metas: if meta[0] == 'cover': # name cover_id = meta[1] # content break # leave loop cover = None if cover_id != None: # Get Manifest element with identifier given by cover_id man_item = book_file.get_item(cover_id) if man_item != None: # href = name of cover file # cover_fil = man_item.href # Get file from zip container try: cover = book_file.read_item(man_item) except Exception as e: cover = None return cover, t class MyView(ui.View): def __init__(self,w,h): file_path = "Le Petit Prince.epub" cover1,t1 = get_epub(file_path) iv1 = ui.ImageView() iv1.frame = (0,0,100,100) self.add_subview(iv1) if cover1: iv1.image = ui.Image.from_data(cover1) tv1 = ui.TextView() tv1.border_color = 'blue' tv1.border_width = 1 tv1.frame = (0,100,w/2,h-200) tv1.text = t1 self.add_subview(tv1) file_path = "The Little Prince.epub" cover2,t2 = get_epub(file_path) iv2 = ui.ImageView() iv2.frame = (w/2,0,100,100) self.add_subview(iv2) if cover2: iv2.image = ui.Image.from_data(cover2) tv2 = ui.TextView() tv2.border_color = 'blue' tv2.border_width = 1 tv2.frame = (w/2,100,w/2,h-200) tv2.text = t2 self.add_subview(tv2) self.name = 'Epub in two languages for Benderrodriguez' def main(): #----- Main process ----- console.clear() w, h = ui.get_screen_size() disp_mode = 'fullscreen' # Hide script my_back = MyView(w,h) my_back.background_color = 'white' my_back.present(disp_mode,hide_title_bar=False) # Protect against import if __name__ == '__main__': main()
-
@cvp Hey human! Very nice code, thank you very much:) The columns idea is great! I will try that method! Now Bender can learn some français! Merci!!!
By the way in case you didnt know this is my great great great grandfather
-
This post is deleted! -
@cvp Thanks!
-
@Benderrodriguez If you want to try this code, you need the epub module.
There It is epub.py
-
@cvp Much obliged!:)
-
@cvp
Hey human! I wanted to thank you again for your code with the Le Petit Prince problem! :) Also, since you are a talented coder and a nice human, I would like to ask you to participate in a coding course Code in Place in Stanford University this spring. Please take a look: https://codeinplace.stanford.edu/
Have a great day human! -
@Benderrodriguez Hello. You're very kind but I'm very far from a good coder, above all in Python.
And I'm too old to teach and more, in English. And Stanford is far from Belgium.
Anyway, I'm sure you could find in this forum skilled guy who would be the right person for this job. -
@cvp Hello human! No worries at all! Good news is that its all online and if you want to polish your skills in python you can apply as a student. The course is free and lasts a few feeks! Its a nice community too :) All the best! Bender and Karel
-
@Benderrodriguez Thanks. I've checked the site and it seems very attractive, even for very old students 😉
-
@cvp yes of course!:) age makes no difference, im over a 1000 years myself !