How to make an audio book reader with Python

Chapter 1
Chapter 2

Welcome to the tale of Red Riding Hood! This classic story has been reimagined and given a fun twist, and I’m excited to share it with you.

Before we begin, I want to let you know that this tale was created using a program written in Python. That’s right, a computer was used to generate this story, just for you to enjoy.

So sit back, relax, and get ready for an exciting journey through the forest with Red Riding Hood. Who knows what dangers and adventures await her on the path to her grandmother’s house? Let’s find out together!

If you’re interested in seeing the code that was used to generate this tale, you can find it on repl.it. Repl.it is a online platform that allows users to write, run, and share code in a variety of programming languages.

To view the code, simply visit repl.it and search for the program by name. From there, you can see how the code was written and how it was used to create this fun and exciting story.

I hope you enjoyed this tale and the unique way in which it was created. Whether you’re a seasoned programmer or new to the world of coding, I hope this story has inspired you to explore the possibilities of what can be achieved with a little bit of creativity and some clever code.

from io import BytesIO
import pygame
import tkinter as tk
from gtts import gTTS

pygame.init()
pygame.mixer.init()
pygame.display.set_caption("Write and play to speak (activate sound on bottom right)")
def speak(text, language='en'):
    mp3_fo = BytesIO()
    tts = gTTS(text, lang=language)
    tts.write_to_fp(mp3_fo)
    pygame.mixer.music.load(mp3_fo, 'mp3')
    pygame.mixer.music.play()

def speak_clear(event):
  for line in tx.get('0.0', tk.END).split("."):
    speak(line)

def change_text(text):
  tx.delete('0.0', tk.END)
  tx.insert(tk.END, text)
root = tk.Tk()

tx = tk.Text(root)
tx.pack()
tx.bind("<Return>", speak_clear)
text1 = """
Once upon a time, in a small village nestled in the heart of a dense forest, there lived a young girl named Red. Red was known for her bright red hooded cloak, which she wore everywhere she went.

One day, Red's mother asked her to take a basket of goodies to her grandmother, who lived on the other side of the forest. Red eagerly accepted the task and set off early the next morning, determined to make it to her grandmother's house before noon.

As Red walked through the forest, she couldn't help but notice how quiet and still everything seemed. No birds sang, no animals rustled in the underbrush, and no breeze stirred the leaves on the trees.

Despite the eerie silence, Red remained undaunted and continued on her way. She had always been a brave and adventurous girl, and she was determined to reach her grandmother's house no matter what.
But little did Red know, danger lurked around every corner. A sly and cunning wolf, who had been watching Red from afar, had set his sights on her and was determined to make her his next meal.

As Red walked deeper into the forest, the wolf began to follow her, waiting for the perfect moment to strike. But little did he know, Red was no ordinary girl. She was clever and resourceful, and she would not be taken down so easily.

Stay tuned for chapter 2 to see what happens next!
"""
tx.insert(tk.END, text1)

text2 = """
As Red continued her journey through the forest, she began to sense that she was being followed. She turned around to see if anyone was there, but all she saw was a dense wall of trees and bushes.

Red shrugged it off and continued walking, but the feeling of being watched only grew stronger. She quickened her pace, hoping to reach her grandmother's house as soon as possible.

But the wolf was not far behind. He had been stalking Red for miles, waiting for the perfect opportunity to strike. And as Red rounded a bend in the path, he saw his chance.

With a growl, the wolf sprang from the bushes and lunged at Red. But the quick-thinking girl was ready for him. She grabbed her basket of goodies and swung it at the wolf, knocking him to the ground.

The wolf yelped in surprise and scrambled to his feet, ready to attack again. But Red was not about to let him get the upper hand. She grabbed a rolling pin from her basket and swung it at the wolf, sending him running back into the forest.

Red breathed a sigh of relief and continued on her way, more determined than ever to reach her grandmother's house. And as she walked, she couldn't help but feel a sense of pride and accomplishment. She had faced danger and emerged victorious, and she knew that she was more than capable of taking care of herself.

When Red finally reached her grandmother's house, she was greeted with open arms and a warm meal. And as she sat down to eat, she couldn't help but smile at the thought of the adventure she had just experienced.

Red knew that she would always be ready to face any challenge that came her way, and she was grateful for the strength and courage that she had discovered within herself. The end
"""


frame1 = tk.Frame(root).pack()
tk.Button(frame1, text="Chapter 1", command=lambda:change_text(text1)).pack(side="left")

tk.Button(frame1, text="Chapter 2", command=lambda:change_text(text2)).pack(side="left")



# buttons = []
# for num, letter in enumerate("abcdefghilmnopqrstuvz"):
#   buttons.append(tk.Button(frame1, text=letter, command=lambda:tx.insert(tk.END, buttons[num]['text'])).pack(side="left"))


bt = tk.Button(root, text="PLAY (write something and activate sound on the right)",
    command=lambda: speak(tx.get('0.0', tk.END)))
bt.pack()
root.mainloop()

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.