I made some changes to some code I made to create slides in powerpoint using python. I also added a simple way to delete some slides. The code is this:
import os import collections import collections.abc from pptx import Presentation def run( name="slides.pptx", content=["siamo qui"]): """Create slides out of the arg list like in the example""" prs = Presentation() # Iterate the list 'context' to create slides for s in content: # Choose a layout(1=normal page) layout = prs.slide_layouts[1] # add a slide with the above layout slide = prs.slides slide = slide.add_slide(layout) # give it title and content slide.shapes.title.text = s[0] slide.placeholders[1].text = s[1] prs.save(name) os.startfile(name) def delete(slide_to_delete): content.pop(slide_to_delete) # -------------------------- data -------------------- filename = "Slides.pptx" content=[ ["Capitolo 1", "Era una notte buia e tempestosa"], ["Capitolo 2", "... e vissero felici e contenti"] ] # ----------------------------------------------------- delete(1) # 0 is the first slide run(filename, content)
In case you want to add images, let’s try this code, without having to search for images, but making google look for them.
import os import collections import collections.abc from pptx import Presentation from pptx.util import Inches import requests from bs4 import BeautifulSoup import requests from io import BytesIO from PIL import Image prs = Presentation() #positioning image top = Inches(2.5) left = Inches(1) height = Inches(4.5) def imagelink(name): url = f"https://www.google.com/search?q={name}&tbm=isch" # the URL of the search result page response = requests.get(url) # make a GET request to the URL soup = BeautifulSoup(response.text, "html.parser") # parse the HTML content with BeautifulSoup # find the first image link by searching for the appropriate tag and attribute img_tag = soup.find("img", {"class": "yWs4tf"}) if img_tag is not None: img_link = img_tag.get("src") print(img_link) # print the first image link return img_link else: print("No image found on the page.") # print(soup.prettify()) # imglink("obama") ############################### class MySlide: def __init__(self, data): #preparing layout self.layout = prs.slide_layouts[data[3]] #adding slide self.slide= prs.slides.add_slide(self.layout) #working with heading self.heading= self.slide.shapes.title self.heading.text= data[0] #working with sub_heading self.sub_heading=self.slide.placeholders[1] self.sub_heading.text=data[1] if data[2] != "": url = data[2] response = requests.get(url) # Load the image content into BytesIO object image_bytes = BytesIO(response.content) # Open the image using PIL img = Image.open(image_bytes) img.save("immagine.png") #adding image self.slide.shapes.add_picture("immagine.png", left, top, height=height) #end of class slides = [ ["heading 1", #data[0] "sub_heading 1", imagelink("Obama"), 1], ["heading 2", #data[0] "sub_heading 2", imagelink("cat"), 1], ["heading 3", #data[0] "sub_heading 3", imagelink("dog"), 1], ["heading 4", #data[0] "sub_heading 4", imagelink("king"), 1], ["heading 5", #data[0] "sub_heading 5", imagelink("run"), 1] ] for each_slide in slides: MySlide(each_slide) #saving pptx prs.save('result.pptx')
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame