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</code><code>matplotlib & seaborn: data visualization</code><code>openpyxl: Excel file handling</code><code>docx: Word document handling

Example 1 – Load data from CSV

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

Example 2 – Clean and preprocess data

df.drop_duplicates(inplace=True)</code><code>df.fillna(0, inplace=True)

Example 3 – Summarize and compute statistics

grouped = df.groupby('Product')['Sales'].sum()</code><code>print(grouped)

Example 4 – Create a bar chart

import matplotlib.pyplot as plt</code><code>import seaborn as sns</code><code>sns.barplot(x=grouped.index, y=grouped.values)</code><code>plt.title('Total Sales by Product Category')</code><code>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</code><code>wb = Workbook()</code><code>ws = wb.active</code><code>for index, row in grouped.items():</code><code>    ws.append([index, row])</code><code>wb.save('sales_report.xlsx')

Example 7 – Add styling to Excel cells

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

Example 8 – Create a Word document

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

Example 9 – Insert the chart into the Word document

doc.add_picture('sales_bar_chart.png')</code><code>doc.save('sales_report_with_chart.docx')

Example 10 – Full automated reporting script

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

MatplotlibpandasData 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

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.