If you are conducting research, you may need to create ppt files quite often. One of the most valuable use cases for creating presentations is to showcase current raw results to our colleagues.

However, while doing the actual work, we can have many maaaany plots to begin the extraction of conclusions. Adding all of them to the pptx can be boring and repetitive: an ideal task for using some code!

1. Pandoc

Pandoc is a really powerful software that allows conversion .tex to .doc or .tex to .pptx, and also from .md to both of the mentioned formats.

If you do not know what is .md extension, it stands for “markdown” files. A nice syntax to write notes that can be exportable to a wide variety of formats.

Installing pandoc is straightforward (for Windows, Linux or Mac) you can follow this link.

By the way: pandoc is a very convenient way to perform spellcheck of your Overleaf projects, for example. Download the source of the project, convert it from .tex to .doc and you can use the powerful MS Word spell checker!

2. Testing a basics of pandoc and markdown

Now that we have pandoc installed, let’s write something really basic in markdown.

2.1 Elementary example

Let’s open an empty text file with the following content:

# Title of my presentation

## A section

Some text

Let’s save this file as file.md. Try the following command to get a first glance of its effect.

pandoc file.md -o output.ppt

This will create a really basic slide using code the code shown above.

2.2 A more advanced one, the core of our program

In this guide they present a small but really useful piece of code

:::::::::::::: {.columns}
::: {.column width="50%"}
Left column:
- Bullet
- Bullet
- Bullet
:::
::: {.column width="50%"}
![](some_image_of_your_choice.jpg)
:::
::::::::::::::

As you may have guessed, you need to add an image in your current directory (some_image_of_your_choice.jpg). Save this code as test.md and try to export it using the following command

pandoc test.md -o output.pptx

See the results and try to figure out a bit about what is happening! Note that adding images goes like

![](the_image)

![](other_image)

.
.
.

So, we want to repeat this process multiple times. We will code a function that will store all the lines of the future file to be exported.

3. The python script

From now on, we will address each of the part of the code independently, then we put them together.

3.1 Globals

We will define some global parts of the code such as the folder where we will sweep all the subdirectories (which we will deliver through the command line)

import os
import sys

# GLOBAL VARIABLES  
FOLDER = sys.argv[1]
ROOT_DIRECTORY = os.getcwd()
  • FOLDER: we will introduce the folder we are going to fully explore through the command line.
  • ROOT_DIRECTORY: we gather the root directory. It will be useful to define the paths.

3.2 Gathering all the images

We need to code a function to explore the folder that may contain the results we want to show. for that, we will use the method walk from the os module: this function yields an iterator over the files and the folders (and its parents). For more information about it you can read this webpage.

def searcher(folder:str) -> list:
    all_images = []
    for root, dirs, files in os.walk(f'{ROOT_DIRECTORY}/{folder}'):
        for f in files:
            if f.endswith('.png') or f.endswith('.svg') or f.endswith('.pdf'):
                all_images.append(os.path.join(root, f))
    
    return all_images
def markdown_file(images: list) -> list:
    lines_file = []
    for elem  in images:
        lines_file.append(f'![]({elem})\n\n')
    
    return lines_file

The output of the markdown_file function will then be transformed to a .md file, and then, we export it to .pptx.

3.3 Exporting to .pptx

Now we have to code the core funtion to create the actual presentation fully automatized. For that, we jut need to deliver the name of the input file and its associated output file:

def create_presentation(input_file:str, output_file:str):
    return os.system(f'pandoc {input_file} -o {ROOT_DIRECTORY}/{output_file}')

3.4 The main program

Now we import all the structure we have built so far. To recap:

  1. We look inside all the folders and subfolders looking for images.
  2. We create a list with the lines of a future markdown file with all the images
  3. We Create the markdown file, export to .pptx.
# Core part of the program
images_detected = searcher(FOLDER)

with open('file.md', 'w') as md_file:
    md_file.writelines(markdown_file(images_detected))

create_presentation('file.md', 'using_python.pptx')

4. Closing remarks

I recommend, whenever possible, importing all the figures in .svg format. Current versions of LibreOffice Impress and MS PowerPoint support them well, and you can resize without losing quality as many times as you want.

This is a neat way to build up a brief slideshow with your raw data. You can use similar logic for your own projects to write .doc files and many others.