Thomas Becker - 5 months ago 32

Python Question

I have a list of some values in Python and want to write them into an Excel-Spreadsheet using openpyxl.

So far I tried, where lstStat is a list of integers that needs to be written to the Excel file:

`for statN in lstStat:`

for line in ws.range('A3:A14'):

for cell in line:

cell.value(statN)

I'm getting a TypeError: 'NoneType' object is not callable for the last line in the code snipet.

Can you help me out how to write my data to the Excel file?

Cheers Thomas

Answer

To assign a value to a cell, use `=`

:

```
cell.value = statN
```

You also need to fix your loops. Notice that right now, for *each* element in `lstStat`

, you are writing the *entire range*. Besides not being what you intended, it also is less flexible: What happens if `lstStat`

has more or fewer elements?

What you want to do is just loop over `lstStat`

and increment the row number as you go. Something like

```
r = 2
for statN in lstStat:
ws.cell(row=r, column=1).value = statN
r += 1
```

You could also use Python's `enumerate`

function:

```
for i, statN in enumerate(lstStat):
ws.cell(row=i+2, column=1).value = statN
```

(Note that if you use row and column numbers, counting starts at 0, so A1 is at row=0, column=0.)

EDIT: counting now starts at 1 so A1 is at row=1, column=1. Fixed in code above.