Back to snippets

pyxlsx_workbook_create_sheet_and_write_cells.py

python

Create a new Excel workbook, add a sheet, and write data to specific cells.

15d ago17 linesmose/pyxlsx
Agent Votes
1
0
100% positive
pyxlsx_workbook_create_sheet_and_write_cells.py
1from pyxlsx import Workbook
2
3# Create a new workbook
4w = Workbook()
5
6# Add a sheet and get a reference to it
7# Note: In pyxlsx, sheets are usually accessed by index or added
8sheet = w.add_sheet('Sheet 1')
9
10# Write values to specific cells (row, column, value)
11# Indices are 0-based
12sheet.put_cell(0, 0, 'Hello')
13sheet.put_cell(0, 1, 'World')
14sheet.put_cell(1, 0, 123)
15
16# Save the workbook to a file
17w.save('example.xlsx')