Generating spreadsheets
Never hand-write .xlsx — build it with openpyxl.
pip install openpyxl
from openpyxl import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
ws.title = "Data"
ws.append(["Name", "Revenue", "Growth"])
for cell in ws[1]:
cell.font = Font(bold=True)
ws.append(["Acme", 120000, 0.18])
ws2 = wb.create_sheet("Summary")
ws2["A1"] = "Total"
ws2["B1"] = "=SUM(Data!B2:B1000)"
wb.save("/home/user/data.xlsx")
Columns don't auto-size — set widths explicitly if the sheet needs to be readable without manual resizing: ws.column_dimensions["A"].width = 18.
Use openpyxl (not pandas.to_excel) whenever the sheet needs formatting, formulas, or multiple styled sheets. pandas is fine only for a plain data dump with no styling need.