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

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

from openpyxl.styles import Font, Alignment

# Bold headers
for col in range(1, 6):
    cell = sheet.cell(row=1, column=col)
    cell.font = Font(bold=True)

# Set column widths
sheet.column_dimensions['A'].width = 15
for col in ['B', 'C', 'D', 'E']:
    sheet.column_dimensions[col].width = 12

# Center-align headers
for col in range(1, 6):
    cell = sheet.cell(row=1, column=col)
    cell.alignment = Alignment(horizontal="center")

# Save the formatted workbook
workbook.save('sales_report_formatted.xlsx')