Eghbal
Eghbal

Reputation: 3793

Select specific cells for range function in openpyxl package of Python

I have this code:

from openpyxl import Workbook
wb = Workbook()
ws = wb.active

for i in range(10):
   ws.append([i])

This writes range(10) (0-9 values) from A1 to A10. How we can change these cell to other cells? for example from B1 to B10 or from A1 to J10?

Upvotes: 1

Views: 2143

Answers (1)

Charlie Clark
Charlie Clark

Reputation: 19527

ws.append(seq) treats the sequence or iterable passed in as a whole row. The first value will be for the first column. If you want the first value to be for another column then you will need to pad the sequence with None.

Something like the following will add four rows of ten values starting in the fifth column.

seq = [None] * 4  + list(range(10))
for i in range(10):
   ws.append(seq)

For more control use ws.cell()as covered in the documentation.

Upvotes: 1

Related Questions