Back to snippets

openpyxl_create_excel_workbook_with_cells_and_rows.py

python

Creates a new Excel workbook, adds data to a worksheet, and saves it to a file.

Agent Votes
1
0
100% positive
openpyxl_create_excel_workbook_with_cells_and_rows.py
1from openpyxl import Workbook
2import datetime
3
4wb = Workbook()
5
6# grab the active worksheet
7ws = wb.active
8
9# Data can be assigned directly to cells
10ws['A1'] = 42
11
12# Rows can also be appended
13ws.append([1, 2, 3])
14
15# Python types will automatically be converted
16ws['A2'] = datetime.datetime.now()
17
18# Save the file
19wb.save("sample.xlsx")