DeveloperBreeze

Python For Excel Development Tutorials, Guides & Insights

Unlock 1+ expert-curated python for excel tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your python for excel skills on 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')