Create a Presentation with Python-Presentations

Let’s take a look at my new app to make presentations. I made other scripts to create presentations, but this one uses PIL to make it, so I think it’s quite fun. It uses some code that I used to make covers, so I thought to change it to make a whole presentation, instead of a single cover. After we create the slides (images), we will join them into a pdf file that we can use to show it.

What do you need

You will need the PIL module (that is very useful, so don’t waste your time and install it).

You install it this way: go in the cmd (windows button + r and then type cmd and press enter):

pip install pillow

Pillow is a fork of PIL and works well.

The code to make the slides

This is the whole code to make the image slides. You insert the text like you can see in the last lines of code.

from PIL import Image, ImageDraw, ImageFilter, ImageFont
from random import randint, random, choice, randrange

def sfondo():
    i = Image.new("RGB", (1000,600), "black")
    font = ImageFont.truetype("arial.ttf", 4)
    d = ImageDraw.Draw(i)
    w,h = i.size
    for x in range(0, w, 8):
        for y in range(0, h, 8):
            boolean = randint(0,9)
            bool2 = int(random()*100)
            ca, cb, cc = [randrange(100,255,10) for x in range(3)]
            d.text((x,y), str(boolean), (ca,cb,cc,bool2), font=font) # left corner up (h=0);
    i = i.filter(ImageFilter.SMOOTH)
    return i
#i.save("1929\\matrix2.png")

# ================= SECOND PART

def add_words():
    num = 8
    font = ImageFont.truetype("impact.ttf", num)
    d = ImageDraw.Draw(i)
    w,h = i.size
    for x in range(30, 100, num+50):
        opacity = 100 + int(random()*255)
        for y in range(0, h, num+16):
            x = randint(50,600)
            y = randint(150,500)
            stringa = choice("""Google
    firebase
    Python
    """.splitlines())
            opacity = randint(100,255)
            num = randint(8,70)
            ca, cb, cc = [randrange(100,255,10) for x in range(3)]
            d.text((x,y), stringa, (ca,cb,cc,opacity), font=ImageFont.truetype("impact.ttf", num))
    #i = i.filter(ImageFilter.SMOOTH)
    #i = i.filter(ImageFilter.CONTOUR)
    i = i.filter(ImageFilter.SMOOTH)
#add_words()

def random_color():
    a,b,c = [randrange(100,255,10) for x in range(3)]
    return a,b,c

counter = 0
def create_slide(testo):
    global counter
    i = sfondo()
    counter +=1
    opacity = randint(100,255)
    num = randint(8,70)
    d = ImageDraw.Draw(i)
    testo = testo.split("\n")
    lentesto = len(testo)
    print(lentesto)
    # Stampa il titolo di sicuro
    a,b,c = random_color()
    d.text((20,50), testo[0], (a,b,c,opacity), font=ImageFont.truetype("impact.ttf", 54))
    for n in range(lentesto):
        if n==0:
            pass
        else:
            a,b,c = random_color()
            d.text((30,100+50*n), testo[n], (a,b,c,opacity), font=ImageFont.truetype("impact.ttf", 50))
    
   
    filename = f"1929\\00{counter}.png"
    i.save(filename)
    print("Salvato: " + filename)
    i.show()

# Put your text with max 6 elements for line / each line is a slide
slides = [
# ======================  [1] ===  " Le cause della crisi"   
"""La crisi del 1929
Le cause:
- crollo in borsa
- panico clienti banche
- debolezza sistema bancario
- Debiti di guerra
""",
# ========================  [2]
"""Dopo la Ia G.M.
- Europa in crisi economica
- Germania => ripagare => Francia, Ingh.
- Inghilterra e Francia => debiti => USA
- Germania => Francia e Ingh. => USA
"""
    
         ]
for slide in slides:
    print(slide)
    create_slide(slide)

Now we make the pdf from the slides images

from fpdf import FPDF
from PIL import Image
import glob


def makePdf(pdfFileName, listPages, dir=''):
    "Takes filename and number of pages abd creates pdf"
    if (dir):
        dir += "/"

    cover = Image.open(dir + str(listPages[0]))
    width, height = cover.size

    pdf = FPDF(unit="pt", format=[width, height])

    for page in listPages:
        pdf.add_page()
        pdf.image(dir + str(page), 0, 0)

    pdf.output(dir + pdfFileName + ".pdf", "F")


x = [f for f in glob.glob("1929/*.png")]
print(x)
y = len(x)


nomefile = input("Nomedelfile: ")

makePdf(nomefile, x)

The examples slides (2 slides)

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.

Python and Powerpoint


Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.