{"id":2318,"date":"2019-07-16T14:40:59","date_gmt":"2019-07-16T12:40:59","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=2318"},"modified":"2023-08-11T18:16:36","modified_gmt":"2023-08-11T16:16:36","slug":"inserting-an-image-in-powerpoint-with-python","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/inserting-an-image-in-powerpoint-with-python\/","title":{"rendered":"Inserting an Image (that fits) in Powerpoint with Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this code you can see an example to insert an image in Powerpoint with Python and the module python-pptx.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The python pptx module can be seen through this scheme:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/scheme.png\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"353\" src=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/scheme.png\" alt=\"\" class=\"wp-image-2325\" srcset=\"https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/scheme.png 592w, https:\/\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/07\/scheme-320x191.png 320w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Old code may not work with python 3.10<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The code below may not work anymore<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pptx import Presentation\nimport os\n\nprs = Presentation()\nslide = prs.slides.add_slide(prs.slide_layouts[8])\nplaceholder = slide.placeholders[1]\npicture = placeholder.insert_picture('001.png')\nprs.save(\"ESEMPIO.pptx\")\nos.startfile(\"ESEMPIO.pptx\")<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Workaround to<a href=\"AttributeError: module 'collections' has no attribute 'Container'\"> <\/a>AttributeError: module &#8216;collections&#8217; has no attribute &#8216;Container&#8217;<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With python 3.10, this code does not work. There is a workaround as you can see <a href=\"https:\/\/stackoverflow.com\/questions\/69468128\/fail-attributeerror-module-collections-has-no-attribute-container\" data-type=\"link\" data-id=\"https:\/\/stackoverflow.com\/questions\/69468128\/fail-attributeerror-module-collections-has-no-attribute-container\">here<\/a>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import collections \nimport collections.abc\nfrom pptx import Presentation\nimport os\n\nprs = Presentation()\nslide = prs.slides.add_slide(prs.slide_layouts[8])\nplaceholder = slide.placeholders[1]\npicture = placeholder.insert_picture('001.png')\nprs.save(\"ESEMPIO.pptx\")\nos.startfile(\"ESEMPIO.pptx\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now it should work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The code that fits the image in the placeholder<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is the code to add to make the image fit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And this is the code to make the slides with the _add_image that uses the code above, replacing the slide.splaceholders[1].add_picture(&#8216;&#8230;png&#8217;) stuff.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from pptx import Presentation\nimport os\nfrom PIL import Image\n\n# pip install python-pttx\n# thanks to Thomas Winters for this\ndef _add_image(slide, placeholder_id, image_url):\n    placeholder = slide.placeholders[placeholder_id]\n\n    # Calculate the image size of the image\n    im = Image.open(image_url)\n    width, height = im.size\n\n    # Make sure the placeholder doesn't zoom in\n    placeholder.height = height\n    placeholder.width = width\n\n    # Insert the picture\n    placeholder = placeholder.insert_picture(image_url)\n\n    # Calculate ratios and compare\n    image_ratio = width \/ height\n    placeholder_ratio = placeholder.width \/ placeholder.height\n    ratio_difference = placeholder_ratio - image_ratio\n\n    # Placeholder width too wide:\n    if ratio_difference > 0:\n        difference_on_each_side = ratio_difference \/ 2\n        placeholder.crop_left = -difference_on_each_side\n        placeholder.crop_right = -difference_on_each_side\n    # Placeholder height too high\n    else:\n        difference_on_each_side = -ratio_difference \/ 2\n        placeholder.crop_bottom = -difference_on_each_side\n        placeholder.crop_top = -difference_on_each_side\n\n\nprs = Presentation()\n\nlayout8 = prs.slide_layouts[8]\nslide = prs.slides.add_slide(layout8)\n\ntitle = slide.shapes.title.text = \"This is Powerpoint\"\nsub = slide.placeholders[2].text = \"Python has the power\"\n_add_image(slide,1,\"003.png\")\n\nprs.save(\"MyPresentation.pptx\")\nos.startfile(\"MyPresentation.pptx\")<\/pre>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<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>\n\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the whole code<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Look at this code related to python and powerpoint<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This example let you insert text and images in powerpoint using python in a very neat way. <a href=\"https:\/\/pythonprogramming.altervista.org\/python-pptx-put-images-in-powerpoint\/\">Click here<\/a> or on the imagine below:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/pythonprogramming.altervista.org\/python-pptx-put-images-in-powerpoint\/\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/pythonprogramming.altervista.org\/wp-content\/uploads\/2019\/12\/IMAGE_PPTX.png?fit=947%2C635&amp;ssl=1\" alt=\"\"\/><\/a><\/figure>\n\n\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":"In this code you can see an example to insert an image in Powerpoint\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/inserting-an-image-in-powerpoint-with-python\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":2323,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,196],"tags":[99,405,4],"class_list":["post-2318","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-powerpoint","tag-image","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\/2318","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=2318"}],"version-history":[{"count":9,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2318\/revisions"}],"predecessor-version":[{"id":13217,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/2318\/revisions\/13217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/2323"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=2318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=2318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=2318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}