Automate Excel Reports with Visible Formulas Using XlsxWriter in Python
Automate Excel report creation with Python by writing explicit formulas that any Excel user can read.
Learn how to create user-friendly Excel reports with Python using the XlsxWriter library.
By incorporating explicit Excel formulas, your reports can be easily read and edited by anyone without any programming experience.
Problem Statement
The main issue with an Excel automation script written using Python Pandas is users' experience without programming skills.
Your tool can be seen as a black box that takes data from an Excel file, processes it on the back end and exports it to another Excel file.
Some users cannot trust a report if they can’t access and modify the formula to populate the results.
As a data scientist, how you can improve the transparency of your tools?
Therefore, replacing Excel with Python can impact the user acceptance of your solutions.
Objective
In this article, I propose an alternative to overcome this problem with the Python library xlswriter.
How do you build Excel automation tools with Python?
Situation: Sales Analytics in a Fashion Retail Company
You are a data analyst in a fashion retail company responsible for sales reporting.
Reports are generated by the system that manages the point of sales in stores.
In these reports, you have
- Sales quantity in pieces
- Items code with a total of 50 items
- Date and week covering a full year
Task: Create Sales Reports by Family
For reporting purposes, you need to process these reports and map the dataset with additional features that will be used to analyze sales trends.
These additional features are linked with the item code
- Item Family: Leather Goods, Accessories, Ready-to-wear or Others
- Scope: boolean value to inform if the item is in the scope of analysis
Objective: Automate the Report Creation
As you need to perform this task more than a hundred times per month, you are looking for a solution to automate the process.
🏫 Discover 70+ case studies using Python to automate reporting and support business optimization 🏪 in this Cheat Sheet
Solutions: Pandas vs. XlsxWriter
Initial solution using Python Pandas
Initially, you built a Python solution that automated the processing using pandas.
Data is imported from several Excel files using pandas_read excel, processed by your script and exported to another Excel file.
This script was converted to an executable file (.exe), so your colleagues can use it without your support.`
- Design and test your Python script
- Use py installer to export your Python script in an executable file
- Share your .exe file (with detailed instructions) with your colleagues
For more information on how to build this solution,
Issues with user acceptance when you use pandas
Some users were complaining that they could not access the formula used to process the data.
Hello Samir, why can’t we see the formulas in the final report? Are you sure that you take the right columns?
This issue raised some concerns among your colleagues who were questioning the tool's accuracy as they couldn’t check it.
New solution with xlsxwriter for transparent reports
The idea is to use the Python library xlsxwriter to perform the calculation in the Excel file.
You can write formulas in Excel cells that the users of the output file can read (and modify).
Thus, you keep the automation capabilities of Python while providing more visibility to your users who are only familiar with Excel.
Implementation of the solution with Python’s xlsxwriter
If you look at the documentation of xlsxwriter, you will find several methods to create formulas on Excel cells.
You can install this library using Pip
pip install xlsxwriter
You can write formulas applied to a single cell,
Also, apply your formula to an array,
You can then build a solution for our easy-processing task
The three additional columns are generated using Excel formulas that can be read on each cell.
Conclusion
To wrap up, XlsxWriter offers a user-friendly approach to automating Excel reports with Python.
By providing users with access to the formulas used to process data, we have addressed the issue of user acceptance that can arise when using Python for automation.
Low processing speed
However, because of the structure of the library and the computing power needed to create formulas in an Excel file, you will lose processing speed.
Limited functionalities
Beyond the limitations of Excel formulas, you also need to cope with the limited functions of the xlsxwriter.
For instance, you cannot build pivot tables with it.
For advanced calculation and processing, you must educate your users and bring transparency using another way to get their trust.
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