Slide Puzzle Python Code Examples

Introduction

Love it or loathe it, PowerPoint is widely used in most business settings. Thisarticle will not debate the merits of PowerPoint but will show you how to usepython to remove some of the drudgery of PowerPoint by automating the creation ofPowerPoint slides using python.

  • This code is already very efficient (compared to my 3 other 8-puzzle solvers)! The only thing is that the empty cell is at start instead of at the end of the 3x3 matrix, as most n-puzzlers are. I am not sure if it is built to work in this way only., Anyway, I tried to fix this but the way is not evident and it seems it will take some time.
  • Example of picture of puzzle piece and cropping 2. Piece Segmentation. Since both light conditions and piece color don’t change inside the dataset, segmentation is achieved using simple binary thresholding. Before applying the binarization, a median filter is applied to the grayscale image in order to remove white noise on the puzzle piece.

15 Puzzle Game This game is the 15 Puzzle Game. In this game, there is a 4.4 board with 15 numbers and an empty square. The numbers are then shuffled randomly. The goal of the game is to move the numbers in such a way that the numbers are ordered again as shown in the picture below. The rules are simple. You can only move tiles into the empty.

Fortunately for us, there is an excellent python library for creating andupdating PowerPoint files: python-pptx. The API is very well documented so itis pretty easy to use. The only tricky part is understanding the PowerPoint documentstructure including the various master layouts and elements. Once youunderstand the basics, it is relatively simple to automate the creation of yourown PowerPoint slides. This article will walk through an example of reading inand analyzing some Excel data with pandas, creating tables and building a graphthat can be embedded in a PowerPoint file.

PowerPoint File Basics

Python-pptx can create blank PowerPoint files but most people are going toprefer working with a predefined template that you can customize with your own content.Python-pptx’s API supports this process quite simply as long as you know a fewthings about your template.

Before diving into some code samples, there are two key components you need tounderstand: Slide Layouts and Placeholders. In the images below you can seean example of two different layouts as well as the template’s placeholders whereyou can populate your content.

In the image below, you can see that we are using Layout 0 and there is one placeholderon the slide at index 1.

In this image, we use Layout 1 for a completely different look.

In order to make your life easier with your own templates, I created a simplestandalone script that takes a template and marks it up with the various elements.

I won’t explain all the code line by line but you can see analyze_ppt.py ongithub. Here is the function that does the bulk of the work:

The basic flow of this function is to loop through and create an example ofevery layout included in the source PowerPoint file. Then on each slide, it willpopulate the title (if it exists). Finally, it will iterate through all of theplaceholders included in the template and show the index of the placeholder aswell as the type.

If you want to try it yourself:

Refer to the input and output files to see what you get.

Creating your own PowerPoint

For the dataset and analysis, I will be replicating the analysis inGenerating Excel Reports from a Pandas Pivot Table. The article explainsthe pandas data manipulation in more detail so it will be helpful to make sureyou are comfortable with it before going too much deeper into the code.

Let’s get things started with the inputs and basic shell of the program:

After we create our command line args, we read the source Excel file into apandas DataFrame. Next, we use that DataFrame as an input to create thePivot_table summary of the data:

Consult the Generating Excel Reports from a Pandas Pivot Table if this doesnot make sense to you.

The next piece of the analysis is creating a simple bar chart of sales performanceby account:

Here is a scaled down version of the image:

We have a chart and a pivot table completed. Now we are going to embed that informationinto a new PowerPoint file based on a given PowerPoint template file.

Before I go any farther, there are a couple of things to note. You need to know whatlayout you would like to use as well as where you want to populate your content.In looking at the output of analyze_ppt.py we know that the title slideis layout 0 and that it has a title attribute and a subtitle at placeholder 1.

Here is the start of the function that we use to create our output PowerPoint:

Slide puzzle python code examples using

This code creates a new presentation based on our input file, adds asingle slide and populates the title and subtitle on the slide. It looks like this:

Pretty cool huh?

The next step is to embed our picture into a slide.

From our previous analysis, we know that the graph slide we want to use islayout index 8, so we create a new slide, add a title then add a picture intoplaceholder 1. The final step adds a subtitle at placeholder 2.

Here is our masterpiece:

For the final portion of the presentation, we will create a table for eachmanager with their sales performance.

Here is an image of what we’re going to achieve:

Creating tables in PowerPoint is a good news / bad news story. The good newsis that there is an API to create one. The bad news is that you can’t easilyconvert a pandas DataFrame to a table using the built in API. However, weare very fortunate that someone has already done all the hard work for usand created PandasToPowerPoint.

This excellent piece of code takes a DataFrame and converts it to a PowerPointcompatible table. I have taken the liberty of including a portion of it inmy script. The original has more functionality that I am not using so Iencourage you to check out the repo and use it in your own code.

The code takes each manager out of the pivot table and builds a simple DataFramethat contains the summary data. Then uses the df_to_table to convertthe DataFrame into a PowerPoint compatible table.

If you want to run this on your own, the full code would look something like this:

All of the relevant files are available in the github repository.

Conclusion

One of the things I really enjoy about using python to solve real world businessproblems is that I am frequently pleasantly surprised at the rich ecosystem of verywell thought out python tools already available to help with my problems. In this specific case,PowerPoint is rarely a joy to use but it is a necessity in many environments.

After reading this article, you should know that there is some hope for you next timeyou are asked to create a bunch of reports in PowerPoint. Keep this article in mind andsee if you can find a way to automate away some of the tedium!

Python Powerpoint Slides

Comments

Contents

Python For Loop

Python For Loop can be used to iterate a set of statements once for each item of a sequence or collection.

The sequence or collection could be Range, List, Tuple, Dictionary, Set or a String.

In this tutorial, we will learn how to implement for loop for each of the above said collections. Going forward, we have detailed example programs with Python For Loop.

Syntax – Python For Loop

The syntax of For Loop in Python is

You can access the item variable inside for block. iterable could be a sequence or collection.

Flow Diagram – Python For Loop

The flow chart of Python For Loop is

When program execution enters for loop for the first time, it checks if there is an item from iterable. If an item is available, the program executes statement(s) inside for block. After execution of the statement(s), the program checks if there is next item available. If True, then the statement(s) are executed again for this next item. The for loop block executes for each item in the iterable. After executing the statement(s) for all items in iterable, the execution comes to a situation where there is no next item in the iterable. At that time, as there is no next item, the for loop is deemed completed, and the execution continues with the next statements in the program.

Example 1: Python For Loop with Range

In this example, we will use a for loop to iterate over a range of numbers.

Python Program

Run

Output

The range is from 25 until 29. So, the range has items: 25, 26, 27 and 28. The statements inside the for loop are executed for each of these elements.

For these examples, the body of for loop has only one print statement. But, you may write as many statements as required by following indentation.

Example 2: For Loop with List

In this example, we will take a list and iterate over the items of list using for loop.

Python Program

Run

Please note that, during each iteration, we are able to access the item for which the loop is running. In this case, we are able to access the item using variable x.

Output

Example 3: For Loop with Tuple

In this example, we will take a tuple and iterate over the items of tuple.

Python Program

Run

The same explanation holds here as well. We could access this item of tuple in for loop using variable x.

Output

Example 4: For Loop with Dictionary

In this example, we will take a dictionary and iterate over the key:value pairs of the dictionary using for loop.

Python Program

Run

mydictionary variable returns an iterable to the keys of the dictionary. Therefore, we could get only key into x. But using x as key we are accessing the value from dictionary inside for block.

Output

Example 5: For Loop with Set

In this example, we will take a set of item, and iterate over the each of the items using for loop.

Python Program

Run

Output

As set is an unordered collection, the order in which for loop accesses the next item from the set is not obvious.

Example 6: For Loop with String

String is a collection of characters. As long as we can get an iterable for the object, we can use for loop.

In this example, we will take a string and iterate over the characters using for loop.

Python Program

Run

Output

Slide Puzzle Python Code Examples For Beginners

Code

Python For Loop – break

From the syntax and flow diagram, we know that for loop will be over only after executing statement(s) for all the elements in the iterable. But, we can break the for loop and end it before it has actually run for all the elements in the iterable using break statement.

In the following program, we shall break the for loop, when the value of element is 7. To realize this, we will use a if statement and execute break statement when the condition evaluates to True.

Python Program

Run

Output

If there had not been the break statement, for loop would have executed until x is 10.

Python For Loop – continue

We can skip the execution of further statements in the for loop body, during that iteration, using continue statement. When continue statement is executed, the for loop continues with the execution of next element in the iterable, rather than completing all the statements in the for loop body.

In the following example program, we shall continue the for loop, when the value of element is 7.

Python Program

Run

Output

When x is 7, continue statement is executed. The print statement after the continue statement in the for loop has been skipped and continued with the next element in the loop, which is 8.

Python For Loop with Else Block

Slide Puzzle Python Code Examples Free

Just like in an if-else statement, you can use an else block with for loop. This else block is executed after the for loop is completed with all the elements in the iterable.

Slide Puzzle Python Code Examples

In the following example, we shall write an else block with for loop.

Python Program

Run

Output

Python Nested For Loop

Python For Loop is just like another Python command or statement. So, can write a for loop inside another for loop and this is called nesting. For loop inside another for loop is called Nested For Loop.

Free Python Code Examples

In the following program, we shall write a nested for loop, to print a pattern of numbers to the console.

Python Code Examples Math

Python Program

Run

Output

Python Coding Puzzles

Summary

Slide Puzzle Python Code Examples Using

In this tutorial of Python Examples, we learned to use Python For Loop on different collections, and with statements like break, continue, else, etc., using well detailed examples.

Related Tutorials