{"id":4311,"date":"2019-12-13T07:44:46","date_gmt":"2019-12-13T06:44:46","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=4311"},"modified":"2019-12-13T07:48:53","modified_gmt":"2019-12-13T06:48:53","slug":"create-a-presentation-with-python-presentations","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/create-a-presentation-with-python-presentations\/","title":{"rendered":"Create a Presentation with Python-Presentations"},"content":{"rendered":"<p>Let&#8217;s take a look at my new <strong>app<\/strong> to make <strong>presentations<\/strong>. I made other scripts to create presentations, but this one uses <strong>PIL<\/strong> to make it, so I think it&#8217;s quite <strong>fun<\/strong>. It uses some code that I used to make <strong>covers<\/strong>, so I thought to change it to make a <strong>whole presentation<\/strong>, instead of a single cover. After we create the <strong>slides<\/strong> (images), we will join them into a <strong>pdf<\/strong> file that we can use to show it.<\/p>\n<h2>What do you need<\/h2>\n<p>You will need the <strong>PIL<\/strong> <strong>module<\/strong> (that is very useful, so don&#8217;t waste your time and install it).<\/p>\n<p>You install it this way: go in the <strong>cmd<\/strong> (<strong>windows button<\/strong> + r and then type cmd and press enter):<\/p>\n<p><strong>pip install pillow<\/strong><\/p>\n<p><strong>Pillow<\/strong> is a fork of PIL and works well.<\/p>\n<h2>The code to make the slides<\/h2>\n<p>This is the whole code to make the image slides. You insert the text like you can see in the last lines of code.<\/p>\n<pre class=\"lang:default decode:true\">from PIL import Image, ImageDraw, ImageFilter, ImageFont\r\nfrom random import randint, random, choice, randrange\r\n\r\ndef sfondo():\r\n    i = Image.new(\"RGB\", (1000,600), \"black\")\r\n    font = ImageFont.truetype(\"arial.ttf\", 4)\r\n    d = ImageDraw.Draw(i)\r\n    w,h = i.size\r\n    for x in range(0, w, 8):\r\n        for y in range(0, h, 8):\r\n            boolean = randint(0,9)\r\n            bool2 = int(random()*100)\r\n            ca, cb, cc = [randrange(100,255,10) for x in range(3)]\r\n            d.text((x,y), str(boolean), (ca,cb,cc,bool2), font=font) # left corner up (h=0);\r\n    i = i.filter(ImageFilter.SMOOTH)\r\n    return i\r\n#i.save(\"1929\\\\matrix2.png\")\r\n\r\n# ================= SECOND PART\r\n\r\ndef add_words():\r\n    num = 8\r\n    font = ImageFont.truetype(\"impact.ttf\", num)\r\n    d = ImageDraw.Draw(i)\r\n    w,h = i.size\r\n    for x in range(30, 100, num+50):\r\n        opacity = 100 + int(random()*255)\r\n        for y in range(0, h, num+16):\r\n            x = randint(50,600)\r\n            y = randint(150,500)\r\n            stringa = choice(\"\"\"Google\r\n    firebase\r\n    Python\r\n    \"\"\".splitlines())\r\n            opacity = randint(100,255)\r\n            num = randint(8,70)\r\n            ca, cb, cc = [randrange(100,255,10) for x in range(3)]\r\n            d.text((x,y), stringa, (ca,cb,cc,opacity), font=ImageFont.truetype(\"impact.ttf\", num))\r\n    #i = i.filter(ImageFilter.SMOOTH)\r\n    #i = i.filter(ImageFilter.CONTOUR)\r\n    i = i.filter(ImageFilter.SMOOTH)\r\n#add_words()\r\n\r\ndef random_color():\r\n    a,b,c = [randrange(100,255,10) for x in range(3)]\r\n    return a,b,c\r\n\r\ncounter = 0\r\ndef create_slide(testo):\r\n    global counter\r\n    i = sfondo()\r\n    counter +=1\r\n    opacity = randint(100,255)\r\n    num = randint(8,70)\r\n    d = ImageDraw.Draw(i)\r\n    testo = testo.split(\"\\n\")\r\n    lentesto = len(testo)\r\n    print(lentesto)\r\n    # Stampa il titolo di sicuro\r\n    a,b,c = random_color()\r\n    d.text((20,50), testo[0], (a,b,c,opacity), font=ImageFont.truetype(\"impact.ttf\", 54))\r\n    for n in range(lentesto):\r\n        if n==0:\r\n            pass\r\n        else:\r\n            a,b,c = random_color()\r\n            d.text((30,100+50*n), testo[n], (a,b,c,opacity), font=ImageFont.truetype(\"impact.ttf\", 50))\r\n    \r\n   \r\n    filename = f\"1929\\\\00{counter}.png\"\r\n    i.save(filename)\r\n    print(\"Salvato: \" + filename)\r\n    i.show()\r\n\r\n# Put your text with max 6 elements for line \/ each line is a slide\r\nslides = [\r\n# ======================  [1] ===  \" Le cause della crisi\"   \r\n\"\"\"La crisi del 1929\r\nLe cause:\r\n- crollo in borsa\r\n- panico clienti banche\r\n- debolezza sistema bancario\r\n- Debiti di guerra\r\n\"\"\",\r\n# ========================  [2]\r\n\"\"\"Dopo la Ia G.M.\r\n- Europa in crisi economica\r\n- Germania =&gt; ripagare =&gt; Francia, Ingh.\r\n- Inghilterra e Francia =&gt; debiti =&gt; USA\r\n- Germania =&gt; Francia e Ingh. =&gt; USA\r\n\"\"\"\r\n    \r\n         ]\r\nfor slide in slides:\r\n    print(slide)\r\n    create_slide(slide)\r\n<\/pre>\n<h2>Now we make the pdf from the slides images<\/h2>\n<pre class=\"lang:default decode:true \">from fpdf import FPDF\r\nfrom PIL import Image\r\nimport glob\r\n\r\n\r\ndef makePdf(pdfFileName, listPages, dir=''):\r\n    \"Takes filename and number of pages abd creates pdf\"\r\n    if (dir):\r\n        dir += \"\/\"\r\n\r\n    cover = Image.open(dir + str(listPages[0]))\r\n    width, height = cover.size\r\n\r\n    pdf = FPDF(unit=\"pt\", format=[width, height])\r\n\r\n    for page in listPages:\r\n        pdf.add_page()\r\n        pdf.image(dir + str(page), 0, 0)\r\n\r\n    pdf.output(dir + pdfFileName + \".pdf\", \"F\")\r\n\r\n\r\nx = [f for f in glob.glob(\"1929\/*.png\")]\r\nprint(x)\r\ny = len(x)\r\n\r\n\r\nnomefile = input(\"Nomedelfile: \")\r\n\r\nmakePdf(nomefile, x)\r\n<\/pre>\n<h2>The examples slides (2 slides)<\/h2>\n<p>This is the output. Think that the color will change randomly, so each time you create the images, they will be different until you freeze then into the pdf file.<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4312\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/001.png\" alt=\"\" width=\"1000\" height=\"600\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/001.png 1000w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/001-320x192.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/001-960x576.png 960w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/001-768x461.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/002.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4314\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/002.png\" alt=\"\" width=\"1000\" height=\"600\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/002.png 1000w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/002-320x192.png 320w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/002-960x576.png 960w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/002-768x461.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/a><\/p>\n<h4>Python and Powerpoint<\/h4>\r\n\r\n\t<script>\r\nvar title = \"Python and powerpoint\";\r\n\t\tvar links = [\r\n\t\t[\"https:\/\/pythonprogramming.altervista.org\/?p=5719&preview=true\",\"Present with SVG files\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-pptx-put-images-in-powerpoint\/\",\"Put images in powerpoint with python-pptx\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/create-a-presentation-with-python-presentations\/\",\"Python-Presentations... without Powerpoint\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-to-create-quickly-powepoint-presentation\/\",\"PyPowerpoint 1\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-to-powerpoint-2-0\/\",\"PyPowerpoint 2\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/python-and-powerpoint-3-0\/\",\"PyPowerpoint 3\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/powerpoint-pil-png-animated-gif\/\",\"Create a Gif with Powerpoint and Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/inserting-an-image-in-powerpoint-with-python\/\",\"Fit Images in Powerpoint with Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/a-program-to-use-powerpoint-with-python\/\", \"Use Powerpoint with Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/create-presentations-with-python-pptx\/\",\"Create a Powerpoint with Python\"],\r\n[\"https:\/\/pythonprogramming.altervista.org\/simple-presentation-in-pure-python-while-you-learn-tkinter\/\", \"Createa Slides without Powerpoint\"]\r\n\t\t\t\r\n];\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>\r\n<br>\r\n\t<!----- pubblicit\u00e0-------- vedi h:\\ads\\codice_di_prima.txt per il codice che era qui --------------------->\r\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- Altervista-pythonprogramming-336X280 -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-4189782812829764\"\r\n     data-ad-slot=\"2548661001\"\r\n     data-ad-format=\"auto\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\r\n\t<!-- fine ad -->\n","protected":false},"excerpt":{"rendered":"make a presentation that is nice and fun to do with Python and PIL&#8230; then save it as a PDF\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/create-a-presentation-with-python-presentations\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":4315,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[196,647],"tags":[161,162,405,648,4],"class_list":["post-4311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powerpoint","category-presentations","tag-pil","tag-pillow","tag-powerpoint","tag-presentations","tag-python"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"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\/4311","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=4311"}],"version-history":[{"count":4,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4311\/revisions"}],"predecessor-version":[{"id":4319,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4311\/revisions\/4319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/4315"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=4311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=4311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=4311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}