Python ebook maker – 4.1 (finale)

We have made a very simple app to create an ebook with very few lines of code.

The final part of the code to make an ebook is this (until new ideas come):

import tkinter as tk
import glob
from time import sleep
import os
"""
aggiunto ctrl+s <Control+s> to bind of text
aggiunta label all'editor
"""

class Ebook:
	def __init__(self, root):
		"""Define window for the app"""
		self.root = root
		self.root.geometry("850x400")
		self.root["bg"] = "coral"
		self.menu()
		self.editor()

	# Widgets on the left ===============|
	def menu(self):
		"""Listbox on the left with file names"""
		self.frame2 = tk.Frame(self.root)
		self.frame2["bg"] = "coral"
		self.frame2.pack(side='left', fill=tk.Y)

		self.button = tk.Button(self.frame2, text="Save", command = self.save)
		self.button.pack()
		self.button_ebook = tk.Button(self.frame2, text="Save ebook", command = self.save_ebook)

		self.frame1 = tk.Frame(self.root)
		self.frame1["bg"] = "coral"
		self.frame1.pack(side='left', fill=tk.Y)
		self.button_ebook.pack()
		self.lstb = tk.Listbox(self.frame1, width=30)
		self.lstb['bg'] = "black"
		self.lstb['fg'] = 'gold'
		self.lstb.pack(fill=tk.Y, expand=1)
		self.lstb.bind("<<ListboxSelect>>", lambda x: self.show_text_in_editor())
		self.files = glob.glob("lezioni\\*.txt")
		for file in self.files:
			self.lstb.insert(tk.END, file)

	def save(self):
		with open(self.filename, "w") as file:
			file.write(self.text.get("1.0", tk.END))
		self.label_file_name["text"] += "...saved"

	def save_ebook(self):
		html = ""
		with open("ebook.html", "w", encoding="utf-8") as htmlfile:
			for file in self.files:
				with open(file, "r") as singlefile:
					for line in singlefile:
						if line[0] == "*":
							line = line.replace("*","")
							html += f"<h2>{line}</h2>"
						elif line[0] == "^":
							line = line.replace("^","")
							html += f"<h3>{line}</h3>"
						else:
							html += f"<p>{line}</p>"
			htmlfile.write(html)
		os.startfile("ebook.html")


	def show_text_in_editor(self):
		"""Shows text of selected file in the editor"""
		if not self.lstb.curselection() is ():
			index = self.lstb.curselection()[0]
			self.filename = self.files[index] # instead of self.lstb.get(index)
			with open(self.filename) as file:
				content = file.read()
			self.text.delete("1.0", tk.END)
			self.text.insert(tk.END, content)
			self.label_file_name['text'] = self.filename

	def editor(self):
		"""The text where you can write"""
		self.label_file_name = tk.Label(self.root)
		self.label_file_name.pack()
		self.text = tk.Text(self.root)
		self.text['bg'] = "darkgreen"
		self.text['fg'] = 'white'
		self.text['font'] = "Arial 24"
		self.text.pack(fill=tk.Y, expand=1)
		self.text.bind("<Control-s>", lambda x: self.save())



if __name__ == "__main__":
	root = tk.Tk()
	app = Ebook(root)
	app.root.title("Lezioni")
	root.mainloop()

Here is the window appearance in the last version of the ebook maker.

This is an example of the output

Output of the app in html

The three chapter are taken from three different txt files and the title and subtitle are line of text with a * or a ^ symbol at the start of it.

Updated version n.1

This is an improvement with new feature that you can find here:

Advertisement