Skip to content
Advertisement

Fill Excel cells with random numbers

I have a question about how to fill some cells in Excels with random values? For example, I have a part of the code with:

numbers_random = (random.random() for _ in range(10))
worksheet.write('A1:J1', numbers_random)

Which gave me the error of: TypeError: Unsupported type <class ‘generator’> in write()

How to fix it?

Advertisement

Answer

To write multiple cells, use write_row() or write_column() as shown in the docs.

Also, try changing the first line to a list comprehension (using square brackets instead of parentheses):

 numbers_random = [random.random() for _ in range(10)]
Advertisement