TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Automate Operational Reporting with Python

Automate the email distribution of supply chain operational reports with visuals built using Python.

Samir Saci
TDS Archive
Published in
8 min readSep 7, 2022

--

This diagram outlines the process for automating a weekly operational report creation and email distribution using Python. It begins with data extraction from a Warehouse Management System (WMS) database, continues through SQL queries to extract prepared order lines, followed by Python scripts that calculate key performance indicators (KPIs), and ends with an automated report delivered by email, including visuals created in the HTML report.
(Image by Author)

You are a data scientist in a fashion retail distribution centre, responsible for performance reporting.

Every week, you connect to the systems, extract data, and perform analyses to build operational dashboards.

How can you automate the process with Python?

You send weekly reports with operational indicators to your management via email.

This process is time-consuming because you do it manually with Excel.

In this article, we will build a fully automated Python solution to share a one-page operational report in an HTML email.

I. How can you send automatic reports by email using Python?
1. Supply Chain Performance Reporting
Weekly report of the orders prepared and delivered from a warehouse.
2. Optimize Performance Management with Automated Reporting
Automate the preparation and distribution of reports
3. Results of the automation solution
HTML reports distributed by email
II. Solution
1. Extraction of Logistics Data from Systems
SQL queries and python scripts to extract and process transactional data
2. Build Charts with Python
Matplotlib to generate graphs with comments
3. Generate HTML pages
Include visuals and comments in HTML pages
4. Send the report by email
Use smtplib to send these HTML pages by email
III. Conclusion
1. Animate your Python Graphs with Pillow
Generate animated GIFs of your graphs to improve user experience
2. Automate Graphic Design
Generate visuals and illlustrations using Python
3. Generative AI for Email Reporting: GPT x Python
Improve the user experience witha smart agent powered by GPT

How can you send automatic reports by email using Python?

Supply Chain Performance Reporting

Optimizing supply chain performance for an international clothing group producing garments, bags and accessories in Asia with local warehouses and direct factory replenishment.

This supply chain diagram illustrates the flow of goods from factories to warehouses, and then to retail stores. It shows how replenishment orders are sent from supply planners to factories, goods are sent to warehouses for pick-and-pack preparation, and finally, distribution planners ensure deliveries to stores or sales channels. The goal is to manage inventory efficiently and streamline logistics.
WLogistics Network of a Fast Fashion Retailer — (Image by Author)

What do we mean by performance?

Optimize Performance Management with Automated Reporting

Learn how to streamline your warehouse operations reporting with Python automation and say goodbye to manual data processing and visualization.

Regularly, you have to share warehouse operational indicators with your management.

This is a manual process in which you need to

  1. Extract data from the Warehouse Management System (WMS)
  2. Process data and built visuals with Excel
  3. Send a short report by email with the visuals and comments

Can we avoid wasting time with some automation?

Objective

To be efficient, you would like to automate this process using Python.

Your solution will be a simple Python script, deployed on the cloud, that will automatically perform these four steps:

  • Extract prepared order lines of last week from the WMS SQL database
  • Process the data and compute KPIs with key insights
  • Automatically send an HTML email with visuals and comments
blue-themed flowchart showing the steps to automate a weekly operational report using Python. The process starts with extracting prepared order lines from a WMS database, processing the data with SQL queries and Python scripts to calculate KPIs, and sending the final report via email. The visuals in the report are automatically generated in Python.
Process in 4 steps — (Image by Author)

How does it look like?

Results of the automation solution

The final report will look like the image below:

  • The title will be adapted to the current week
  • A bar plot visual will be included
  • A comment area will provide insights based on the visual
A sample visual of a weekly logistics workload report generated using Python. The report contains a bar chart that shows order and line counts per day over a week. It also includes comments about the performance, such as the total number of order lines processed and which day was the busiest. The report is delivered via email as an HTML file, with updated comments based on the visual data.
Final Result — (Image by Author)

The whole process will be automated to send the reports on time without your support.

If you are looking for examples of application for operational management, have a look at this short video

🏫 Discover 70+ case studies using Python to automate reporting and support business optimization 🏪 in this Cheat Sheet

Solution

Let us explore all the steps to generate your final report.

Workflow diagram showing steps to automate operational reporting with Python. Extract records from the SQL database, process them, generate KPIs, create charts in PNG format, embed charts in an HTML report, and send the report via email using an SMTP server. The flow ends with an operational report sent to users. A preview of the report shows a bar chart with comments.
Steps to generate the automated reports sent by email using Python— (Image by Author)

It starts with extracting data …

Extraction of Logistics Data from Systems

I have shared a CSV file with dummy data because you won’t have access to my WMS databases.

But your solution will be connected to your WMS

  • Create your SQL Query to extract shipment records
  • Use pandas.read_sql_query to do the query
  • The results will be a pandas data frame

How to extract the right indicators from this dataset?

Data Processing using Python

An important indicator is the number of lines per order.

Processing tasks will add this column to your data frame.

Let’s visualize now

Build Charts with Python

It would be best to have a simple bar plot chart showing the number of Lines and Orders prepared per day.

A bar chart generated with Python visualizing the warehouse workload per day in lines and orders. The x-axis represents days of the week (Mon-Sun), and the y-axis represents the number of orders and lines prepared daily. The red bars represent the number of lines, and the blue bars represent the number of orders.
Visual of the workload per day using bar plots— (Image by Author)

How do we include these results in an email?
We use HTML.

Save the image in an HTML Page using Python

To be embedded in your HTML page, you need to save it

Can we add some observations based on the data?

Add insights to the report

Your report's added value summarises the week of operations in three bullet points.

Therefore, you need to compute the right indicators to bring visibility to your top management.

These insights will be included in your HTML file for the comment area.

The objective is to have a ready-to-be-read report.

Create the HTML Page with the Report

The repository contains an HTML page template that you can use to create your email.

A structured HTML report layout with key sections labeled generated with Python. The header includes a logo, followed by an H1 title showing “Workload Report for Week-1.” The central image is a bar chart representing daily workloads, followed by a comment area with operational insights. The footer includes the contact information of the supply chain analytics team.
Structure of the HTML file for the report sent by email — (Image by Author)

The structure is simple, with

  • A header with a logo in a PNG file
  • A title with the current week's updated
  • Your visual using a PNG image
  • A comment area with updated insights
  • A footer where you can put information about the author

We start by including the illustrations.

Include the images in the HTML report

To include the header and your visual in the HTML, you’ll create MIMEImage objects with a content-id that will be put in the HTML code.

And we add the text …

Add the insights in the HTML code

I have put some markers where the insights will be written in the HTML code.

The idea is to use the replace function to modify them and put the values returned by the Python script.

Now that the page is ready, we can send it.

Create and send the email with Python

To send your email using Python, you can use the library smtplib.

Yellow-themed step-by-step guide on sending an email with Python. The list includes five steps: downloading the SMTP library, connecting to the SMTP server, entering the email address and password, and providing the delivery address. These steps guide users through sending emails automatically using Python.
Automated process to send an email with python — (Image by Author)

You need to add the following information (example FYI)

  • SMTP server, port: for instance, ‘smtp.google.com’, 465
  • Your email address and the delivery address
  • Your mailbox password

Then, you can send your HTML email with updated visuals and insights.

You can find the source code with dummy data here

💡 Follow me on Medium for more articles related to 🏭 Supply Chain Analytics, 🌳 Sustainability and 🕜 Productivity.

Conclusion

This simple example gives you a template to build your reporting automation solution.

You can now,

  • Add visuals or tables
  • Bring more insights or enrich the text with conditions

What if you want to animate your graphs?

Animate your Python Graphs with Pillow

For some graphs, you want to provide information about a dynamic process.

In that case, static plots may not convey the message or insights expected.

Can we improve warehouse productivity with pathfinding algorithms?

As a supply chain solution manager, I have conducted reengineering studies to find ways to improve picking productivity.

Two line plots represent the picking routes of a warehouse operator using two different algorithms. Each dot on the plot corresponds to a picking location, with dashed lines connecting the dots in the order of picking. The top plot shows Algorithm 1’s route, and the bottom plot shows Algorithm 2’s route. Each location is annotated with a number, indicating the sequence of picking locations. This visualization allows for comparing the two algorithms’ efficiency.
Pathfinding algorithm comparative study — (Image by Author)

In another article, you can find a comparative study between two pathfinding algorithms to minimize walking distance.

Can we spot the difference with static graphs? No.

Therefore, I used Python’s Pillow library to animate these graphs and generate the visual below.

Example of Animated Visuals — (Image by Author)

The detailed tutorial is available in the link below if you want to include a similar animation in your email report.

Do you want to add nice visuals to your report?

Automate Graphic Design

Use Python Pillow to automate the creation of visuals or illustrations to feed your report.

Animation with hundred Warehouse labels with a large yellow upward arrow pointing toward the storage location generated with Python Pillow. It contains a barcode, SKU code 6090761375244, and icons indicating that the stored items are boxes, pants, and should be handled with care due to liquid content.
Example of Graphic Design — (Image by Author)

For example, the labels above have been generated automatically with a Python script.

This a great way to boost user attention and improve interactions by

  • Conveying ideas and concepts with animated visuals
  • Animate graphs to show a trend

💡 For more information,

Have you ever heard about generative AI?

Generative AI for Email Reporting: GPT x Python

Since OpenAI introduced ChatGPT’s advanced features, we have had the opportunity to enhance this solution with an intelligent GPT agent.

A diagram showing a two-step process for analyzing supply chain data using an AI agent. Step 1 is “Data Upload,” where users upload a dataset, either a CSV file or a sample file. Step 2 is “Select Variable,” where users choose between quantity and turnover for analysis. The AI agent then generates analysis with visuals, including sales distribution and demand variability charts.
“The Supply Chain Analyst” — (Image by Author)

Imagine if users can ask the agent to build custom reports and send them via email.

This diagram outlines how the custom GPTs for Supply Chain Analytics work. It starts with the user asking a question or requesting an analysis, the agent retrieves data (from the provided dataset or sample), processes it with a core Python script, and returns output as charts or comments. The flow clearly illustrates three key steps: initial prompt, data processing using the script, and final analysis outputs that is used by “The Supply Chain Analyst”.
Advanced Solutions Architecture with GPT — (Image by Author)

The “The Supply Chain Analyst” example shows how we can design analytics products with a user interface boosted by Large Language Models (LLMs).

The design approach is quite simple.

You need

  • A core module in a Python Script: this will be the solution designed in this article
  • Additional prompts to guide the agent on how to interact with users

If you want to build your own GPT, have a look at these articles

If you prefer the video, you can have a look at the YouTube Tutorial

About Me

Let’s connect on Linkedin and Twitter. I am a Supply Chain Engineer who uses data analytics to improve logistics operations and reduce costs.

For consulting or advice on analytics and sustainable supply chain transformation, feel free to contact me via Logigreen Consulting.

If you are interested in Data Analytics and Supply Chain, look at my website.

💌 New articles straight in your inbox for free: Newsletter
📘 Boost your Productivity with Data Analytics: Productivity Cheat Sheet

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Samir Saci
Samir Saci

Written by Samir Saci

Top Supply Chain Analytics Writer — Case studies using Data Science for Supply Chain Sustainability 🌳 and Productivity: https://bit.ly/supply-chain-cheat

Responses (1)