Fundamentals 6 min read

Using Python to Quickly Create Professional Data Reports

This tutorial demonstrates how to use Python libraries such as pandas, matplotlib, openpyxl, and python-docx to load, clean, analyze, visualize, and automatically generate Excel and Word reports from CSV data, complete with code examples for each step.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Python to Quickly Create Professional Data Reports

In today's fast‑paced environment, data‑driven decision making is essential, and manually creating reports is time‑consuming and error‑prone; Python serves as a versatile tool to automate the entire reporting workflow.

Required libraries

pandas: data processing and analysis
matplotlib & seaborn: data visualization
openpyxl: Excel file handling
docx: Word document handling

Example 1 – Load data from CSV

import pandas as pd
# Load data
df = pd.read_csv('sales_data.csv')
# Show first rows
print(df.head())

Example 2 – Clean and preprocess data

df.drop_duplicates(inplace=True)
df.fillna(0, inplace=True)

Example 3 – Summarize and compute statistics

grouped = df.groupby('Product')['Sales'].sum()
print(grouped)

Example 4 – Create a bar chart

import matplotlib.pyplot as plt
import seaborn as sns
sns.barplot(x=grouped.index, y=grouped.values)
plt.title('Total Sales by Product Category')
plt.show()

Example 5 – Save the chart to a file

plt.savefig('sales_bar_chart.png')

Example 6 – Generate an Excel report

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
for index, row in grouped.items():
ws.append([index, row])
wb.save('sales_report.xlsx')

Example 7 – Add styling to Excel cells

from openpyxl.styles import Font
title_font = Font(bold=True)
ws['A1'].font = title_font
ws.column_dimensions['A'].width = 30

Example 8 – Create a Word document

from docx import Document
doc = Document()
doc.add_heading('Sales Report', level=1)
doc.add_paragraph('This report shows the total sales by product category.')
doc.save('sales_report.docx')

Example 9 – Insert the chart into the Word document

doc.add_picture('sales_bar_chart.png')
doc.save('sales_report_with_chart.docx')

Example 10 – Full automated reporting script

def generate_report():
# Read data
df = pd.read_csv('sales_data.csv')
# Clean data
df.drop_duplicates(inplace=True)
df.fillna(0, inplace=True)
# Analyze
grouped = df.groupby('Product')['Sales'].sum()
# Visualize
sns.barplot(x=grouped.index, y=grouped.values)
plt.savefig('sales_bar_chart.png')
# Excel report
wb = Workbook()
ws = wb.active
for index, row in grouped.items():
ws.append([index, row])
wb.save('sales_report.xlsx')
# Word report
doc = Document()
doc.add_heading('Sales Report', level=1)
doc.add_paragraph('This report shows the total sales by product category.')
doc.add_picture('sales_bar_chart.png')
doc.save('sales_report_with_chart.docx')
# Run the function
generate_report()

By following these steps, you can transform raw CSV data into polished Excel and Word reports within minutes, ensuring consistency and accuracy through automation.

PythonMatplotlibpandasData Reportingopenpyxlpython-docx
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.