{"id":3602,"date":"2019-09-14T19:25:07","date_gmt":"2019-09-14T17:25:07","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3602"},"modified":"2019-09-15T16:40:48","modified_gmt":"2019-09-15T14:40:48","slug":"python-ebook-maker-4-1-finale","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-4-1-finale\/","title":{"rendered":"Python ebook maker &#8211; 4.1 (finale)"},"content":{"rendered":"<p>We have made a very simple app to create an ebook with very few lines of code.<\/p>\n<p>The final part of the code to make an ebook is this (until new ideas come):<\/p>\n<pre class=\"lang:default decode:true \">import tkinter as tk\r\nimport glob\r\nfrom time import sleep\r\nimport os\r\n\"\"\"\r\naggiunto ctrl+s &lt;Control+s&gt; to bind of text\r\naggiunta label all'editor\r\n\"\"\"\r\n\r\nclass Ebook:\r\n\tdef __init__(self, root):\r\n\t\t\"\"\"Define window for the app\"\"\"\r\n\t\tself.root = root\r\n\t\tself.root.geometry(\"850x400\")\r\n\t\tself.root[\"bg\"] = \"coral\"\r\n\t\tself.menu()\r\n\t\tself.editor()\r\n\r\n\t# Widgets on the left ===============|\r\n\tdef menu(self):\r\n\t\t\"\"\"Listbox on the left with file names\"\"\"\r\n\t\tself.frame2 = tk.Frame(self.root)\r\n\t\tself.frame2[\"bg\"] = \"coral\"\r\n\t\tself.frame2.pack(side='left', fill=tk.Y)\r\n\r\n\t\tself.button = tk.Button(self.frame2, text=\"Save\", command = self.save)\r\n\t\tself.button.pack()\r\n\t\tself.button_ebook = tk.Button(self.frame2, text=\"Save ebook\", command = self.save_ebook)\r\n\r\n\t\tself.frame1 = tk.Frame(self.root)\r\n\t\tself.frame1[\"bg\"] = \"coral\"\r\n\t\tself.frame1.pack(side='left', fill=tk.Y)\r\n\t\tself.button_ebook.pack()\r\n\t\tself.lstb = tk.Listbox(self.frame1, width=30)\r\n\t\tself.lstb['bg'] = \"black\"\r\n\t\tself.lstb['fg'] = 'gold'\r\n\t\tself.lstb.pack(fill=tk.Y, expand=1)\r\n\t\tself.lstb.bind(\"&lt;&lt;ListboxSelect&gt;&gt;\", lambda x: self.show_text_in_editor())\r\n\t\tself.files = glob.glob(\"lezioni\\\\*.txt\")\r\n\t\tfor file in self.files:\r\n\t\t\tself.lstb.insert(tk.END, file)\r\n\r\n\tdef save(self):\r\n\t\twith open(self.filename, \"w\") as file:\r\n\t\t\tfile.write(self.text.get(\"1.0\", tk.END))\r\n\t\tself.label_file_name[\"text\"] += \"...saved\"\r\n\r\n\tdef save_ebook(self):\r\n\t\thtml = \"\"\r\n\t\twith open(\"ebook.html\", \"w\", encoding=\"utf-8\") as htmlfile:\r\n\t\t\tfor file in self.files:\r\n\t\t\t\twith open(file, \"r\") as singlefile:\r\n\t\t\t\t\tfor line in singlefile:\r\n\t\t\t\t\t\tif line[0] == \"*\":\r\n\t\t\t\t\t\t\tline = line.replace(\"*\",\"\")\r\n\t\t\t\t\t\t\thtml += f\"&lt;h2&gt;{line}&lt;\/h2&gt;\"\r\n\t\t\t\t\t\telif line[0] == \"^\":\r\n\t\t\t\t\t\t\tline = line.replace(\"^\",\"\")\r\n\t\t\t\t\t\t\thtml += f\"&lt;h3&gt;{line}&lt;\/h3&gt;\"\r\n\t\t\t\t\t\telse:\r\n\t\t\t\t\t\t\thtml += f\"&lt;p&gt;{line}&lt;\/p&gt;\"\r\n\t\t\thtmlfile.write(html)\r\n\t\tos.startfile(\"ebook.html\")\r\n\r\n\r\n\tdef show_text_in_editor(self):\r\n\t\t\"\"\"Shows text of selected file in the editor\"\"\"\r\n\t\tif not self.lstb.curselection() is ():\r\n\t\t\tindex = self.lstb.curselection()[0]\r\n\t\t\tself.filename = self.files[index] # instead of self.lstb.get(index)\r\n\t\t\twith open(self.filename) as file:\r\n\t\t\t\tcontent = file.read()\r\n\t\t\tself.text.delete(\"1.0\", tk.END)\r\n\t\t\tself.text.insert(tk.END, content)\r\n\t\t\tself.label_file_name['text'] = self.filename\r\n\r\n\tdef editor(self):\r\n\t\t\"\"\"The text where you can write\"\"\"\r\n\t\tself.label_file_name = tk.Label(self.root)\r\n\t\tself.label_file_name.pack()\r\n\t\tself.text = tk.Text(self.root)\r\n\t\tself.text['bg'] = \"darkgreen\"\r\n\t\tself.text['fg'] = 'white'\r\n\t\tself.text['font'] = \"Arial 24\"\r\n\t\tself.text.pack(fill=tk.Y, expand=1)\r\n\t\tself.text.bind(\"&lt;Control-s&gt;\", lambda x: self.save())\r\n\r\n\r\n\r\nif __name__ == \"__main__\":\r\n\troot = tk.Tk()\r\n\tapp = Ebook(root)\r\n\tapp.root.title(\"Lezioni\")\r\n\troot.mainloop()<\/pre>\n<p><iframe loading=\"lazy\" title=\"Python ebook maker - 4 (finale)\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/GtRZMpofnW8?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>Here is the window appearance in the last version of the ebook maker.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/ebooks4b.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3619\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/ebooks4b.png\" alt=\"\" width=\"852\" height=\"432\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/ebooks4b.png 852w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/ebooks4b-320x162.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/ebooks4b-768x389.png 768w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/a><\/p>\n<p>This is an example of the output<\/p>\n<figure id=\"attachment_3621\" aria-describedby=\"caption-attachment-3621\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/example.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-3621 size-full\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/example.png\" alt=\"\" width=\"570\" height=\"460\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/example.png 570w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/09\/example-320x258.png 320w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/a><figcaption id=\"caption-attachment-3621\" class=\"wp-caption-text\">Output of the app in html<\/figcaption><\/figure>\n<p>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.<\/p>\n<h2>Updated version n.1<\/h2>\n<p>This is an improvement with new feature that you can find <a href=\"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-update-1\/\">here<\/a>:<\/p>\n<p><iframe loading=\"lazy\" title=\"Python ebook maker 5 (updates)\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/GiAAs2i1NeI?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<script>\r\nvar title = \"PyBook the Ebook maker\";\r\nvar links = [\r\n[\"https:\/\/pythonprogramming.altervista.org\/my-personal-notepad-toggle-tkinter-fullscreen\/\",\"Pybook part 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/from-a-notepad-to-an-ebook-maker\/\",\"PyBook part 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-makes-ebooks-3-with-quick-recap\/\",\"PyBook part 3\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-ebooks-maker-4-save-files\/\",\"PyBook part 4\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-4-1-finale\/\",\"PyBook part 4.1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-update-1\/\",\"Updates until v. 1.6\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/py-ebook-v-1-8-make-ebooks-with-python-wip-dark-mode\/\",\"Version 1.8 Dark mode +-\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/tkinter-how-to-hide-and-show-frames\/\",\"Hide n Show Frames\"]\r\n\t\t];\r\n<\/script>\r\n<script>\r\n\t\r\nif (typeof next2 != \"undefined\"){let next2 = 0;}\r\n\t\r\nnext2 = 0;\r\n\thtml = \"\";\/\/<b style='color:coral;font-size:1.2em'>Other posts about \" + title + \"<\/b><br>\";\r\nfor (address of links) \r\n{\r\n\r\n\tif (next2 == 1){\r\n\t\thtml += \"<div style='background:coral'>\";\r\n\t\thtml += \"Next link => <a href='\" + address[0] + \"'>\" + address[1] + \"<\/a>\";\r\n\t\thtml += \"<\/div><br>\";\r\n\t\tnext2 = 0;\r\n\t}\r\n\tif (address[0] == document.URL) {\r\n\t\tnext2 = 1;\r\n\t}\r\n}\r\n\r\nif (typeof next != \"undefined\") {let next = 0;}\r\nif (typeof addressStart != \"undefined\") {let addressStart = \"\";}\r\nnext = 0;\r\naddressStart = \"<a href='\";\r\nfor (address of links) {\r\n\tif (next == 1){\r\n\t\thtml += \">>>\" + addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t\tnext = 0;\r\n\t}\r\n\telse if (addressStart + address[0] != document.URL)\r\n\t{\r\n\t\thtml += addressStart + address[0] + \"'>\" + address[1] + \"<\/a><br>\";\r\n\t}\r\n\telse\r\n\t{\r\n\t\tnext = 1;\r\n\t\tnext_address = address[0]\r\n\t\tnext_title = address[1]\r\n\t\thtml += \"<span style='color:gray'>\" + address[1] + \"<\/span><br>\";\r\n\t}\r\n\r\n}\r\n\r\n\thtml += `<span style=\"font-size:8px\">Powered by <a href=\"https:\/\/pythonprogramming.altervista.org\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2673\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png\" alt=\"\" width=\"70\" height=\"25\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2.png 156w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/altervista2-150x56.png 150w\" sizes=\"auto, (max-width: 70px) 100vw, 70px\" \/>pythonprogramming.altervista.org<\/a><\/span>`\r\n\thtml = \"<div style='background:yellow'>\" + html + \"<\/div>\";\r\n\tdocument.write(html)\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"Make an ebook with Python\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/python-ebook-maker-4-1-finale\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3603,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[551,49],"tags":[553,321,4,51],"class_list":["post-3602","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ebooks","category-tkinter","tag-ebook","tag-html","tag-python","tag-tkinter"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3602","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=3602"}],"version-history":[{"count":5,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3602\/revisions"}],"predecessor-version":[{"id":3638,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3602\/revisions\/3638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3603"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}