{"id":2102,"date":"2019-06-03T12:34:13","date_gmt":"2019-06-03T10:34:13","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2102"},"modified":"2019-07-16T21:58:21","modified_gmt":"2019-07-16T19:58:21","slug":"a-program-to-use-powerpoint-with-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/a-program-to-use-powerpoint-with-python\/","title":{"rendered":"A program to use Powerpoint with Python"},"content":{"rendered":"<p>A program to use Powerpoint with Python, with the pptx module. Python is very useful for a lot of things that can make you save a lot of time. Let&#8217;s see how to use it to make a presentation faster and in a more pleasant way with the complete instructions to create a whole presentation just with Python with no need to manage Powerpoint.<\/p>\n<p><iframe loading=\"lazy\" title=\"A program to use Powerpoint with Python\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/wS4HfjiAc_w?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<h2>Taking data from an external txt file<\/h2>\n<p>The txt file is this<\/p>\n<pre class=\"lang:default decode:true \">Titolo 1\r\n\r\nParagrafo 1.\r\n\r\nTitolo 2\r\n\r\nParagrafo 2...<\/pre>\n<p>&nbsp;<\/p>\n<p>The code is this<\/p>\n<pre class=\"lang:default decode:true \">from pptx import Presentation\r\nimport os, glob\r\n\r\nprs = Presentation()\r\n\r\ndef slide(number=0):\r\n    title_slide_layout = prs.slide_layouts[number]\r\n    slide = prs.slides.add_slide(title_slide_layout)\r\n    title = slide.shapes.title\r\n    subtitle = slide.placeholders[1]\r\n    title.text = file.readline()\r\n    file.readline()\r\n    subtitle.text = file.readline()\r\n    file.readline()  # salta la riga vuota\r\n\r\n\r\ndef choose_file():\r\n    # returns the file chosen in the dir\r\n    flist = glob.glob(\"*.txt\") \r\n    for f in flist:\r\n        print(flist.index(f), f)\r\n    fchoose = int(input(\"Choose NUMBER: \"))\r\n    return flist[fchoose]\r\n\r\nif __name__ == \"__main__\":\r\n    file_chosen = choose_file()\r\n    with open(file_chosen, 'r', encoding=\"utf-8\") as file:\r\n        file_lenght = len(file.readlines())\r\n        file.seek(0)\r\n        slide(0)\r\n        for i in range(int(file_lenght \/ 4)):\r\n            slide(1)\r\n    file_to_save = file_chosen[:-4] + \".pptx\"\r\n    prs.save(file_to_save)\r\n    os.startfile(file_to_save)<\/pre>\n<p>When the program runs, you are asked to choose a txt file among the ones in the folder (there is only one in this particular case):<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2312\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/001.png\" alt=\"\" width=\"209\" height=\"61\" \/><\/a><\/p>\n<p>So you insert the number on the left of the title you want<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/002-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2313\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/002-1.png\" alt=\"\" width=\"206\" height=\"69\" \/><\/a><\/p>\n<p>The output is this<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/003-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2314\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/003-1.png\" alt=\"\" width=\"276\" height=\"418\" \/><\/a><\/p>\n<p>And you will see another file in the folder, the pptx file yet created:<\/p>\n<p><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/005.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2316\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/06\/005.png\" alt=\"\" width=\"303\" height=\"124\" \/><\/a><\/p>\n<p>The power.py is the python file, the prova.txt is the file with the data and the prova.pptx is the powerpoint generated by the code.<\/p>\n<h2>Insert an image in Powerpoint with Python<\/h2>\n<p>The <a href=\"https:\/\/www.youtube.com\/redirect?q=https%3A%2F%2Fpythonprogramming.altervista.org%2Finserting-an-image-in-powerpoint-with-python%2F&amp;v=bAJ5hfE6wgs&amp;event=video_description&amp;redir_token=6JIi2LTx146s4xxOKOt2jLXw1Pd8MTU2MzM5MzQ0NUAxNTYzMzA3MDQ1\">post with the code to fit the image (watch the video) is here<\/a>.<\/p>\n<p><iframe loading=\"lazy\" title=\"Image in Powerpoint with Python\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/bAJ5hfE6wgs?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","protected":false},"excerpt":{"rendered":"Powerpoint: use Python to create a presentation with Powerpoint, without even opening the program.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/a-program-to-use-powerpoint-with-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2104,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,196],"tags":[405,4],"class_list":["post-2102","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-powerpoint","tag-powerpoint","tag-python"],"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\/2102","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=2102"}],"version-history":[{"count":4,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2102\/revisions"}],"predecessor-version":[{"id":2328,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2102\/revisions\/2328"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2104"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}