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')