DeveloperBreeze

Tutorials Programming Tutorials, Guides & Best Practices

Explore 149+ expertly crafted tutorials tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

Automating Excel Reports with Python and OpenPyXL

Tutorial December 10, 2024
python

# Add a new header for the total sales
sheet.cell(row=1, column=5, value="Total Sales")

# Calculate total sales for each product
for row in range(2, sheet.max_row + 1):
    jan = sheet.cell(row=row, column=2).value
    feb = sheet.cell(row=row, column=3).value
    mar = sheet.cell(row=row, column=4).value
    total = jan + feb + mar
    sheet.cell(row=row, column=5, value=total)

# Save the updated workbook
workbook.save('sales_data_with_totals.xlsx')

To make the report visually appealing, we’ll apply some formatting: