Skip to main content
Practice

Analyzing Sales Data to Derive Insights

In this lesson, we will conduct a simple sales analysis based on the sales data and add quantitative insights derived from the analysis to the report.


How Does the Insight Analysis Code Work?

The practice code operates in 6 steps as follows:


1. Initialize Variables for Data Analysis

Initializing Variables
total_sales_by_product = {'Camera': 0, 'Computer': 0, 'Smartphone': 0}

highest_monthly_sales = 0

best_selling_product = ""
  • total_sales_by_product: A dictionary to store the total sales of each product. Initially, it starts at 0 for all products.

  • highest_monthly_sales: Stores the sales of the month with the highest sales.

  • best_selling_product: A variable to store the best-selling product.


2. Read Excel Data and Add to Table

Adding Excel Data and Creating Table
for row in sheet.iter_rows(min_row=2, values_only=True):
month, product, sales, price, revenue = row
  • sheet.iter_rows: Reads the data from the Excel sheet row by row. min_row=2 skips the first row (column titles) and only gets the data.

  • values_only=True: Ensures that only the cell values are retrieved.


Creating Table
row_cells = table.add_row().cells
  • Adds a new row to the table.

Adding Data to Table
row_cells[0].text = str(month)
row_cells[1].text = product
row_cells[2].text = str(sales)
row_cells[3].text = str(price)
row_cells[4].text = str(revenue)
  • Adds the data to each cell. Numerical data is converted to text to be inserted as a textual value.

3. Calculate Total Sales by Product and Find the Top-Selling Product

Calculating Total Sales by Product
total_sales_by_product[product] += revenue
  • total_sales_by_product[product] += revenue: Accumulates the sales for each product.

Finding the Top-Selling Product
if revenue > highest_monthly_sales:
highest_monthly_sales = revenue
best_selling_product = product
  • If the sales revenue is greater than the current highest, it updates the highest sales and the corresponding product.

4. Add Sales Analysis Results

Adding Sales Analysis Results
doc.add_heading('Sales Analysis and Insights', level=1)
  • Adds a heading "Sales Analysis and Insights" to the document.

5. Summary of Total Sales by Product

Summary of Total Sales by Product
for product, total_sales in total_sales_by_product.items():
doc.add_paragraph(f"Total sales for {product} in Q1: ${total_sales:,}")
  • doc.add_paragraph: Adds a paragraph to the document summarizing the total sales of each product in Q1.

6. Add the Best-Selling Product

doc.add_paragraph(f"Best-selling product: {best_selling_product} (Monthly sales: ${highest_monthly_sales:,})")
  • Adds the best-selling product and the sales for the corresponding month to the document.

Sales Analysis and Insights

  • Total sales for Camera in Q1: $65,400,000

  • Total sales for Computer in Q1: $120,250,000

  • Total sales for Smartphone in Q1: $242,500,000

Best-selling product: Smartphone (Monthly sales: $90,000,000)


We have learned how to analyze Excel data and use Python code to add tables and insights to a report in docx format.

To derive more insights, try advancing the Python code further using an AI code editor. :)

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.