I am currently trying to write data to an excel spreadsheet using Python3 and openpyxl. I have figured out how to do it when assigning one single value, but for some reason when I introduce a For loop it is giving me an error. This program will eventually be used to filter through a python dictionary and print out the keys and values from the python dictionary. For now, I am just trying to create a loop that will input a random integer in the spreadsheet for every key listed in the dictionary (not including nested keys). If anyone can help me determine why this error is coming up it would be much appreciated. Thanks in advance!
# Writing Dictionary to excel spreadsheet
import openpyxl
import random
wb = openpyxl.load_workbook("ExampleSheet.xlsx")
sheet = wb.get_sheet_by_name("Sheet1")
sheet["B1"].value = "Price" #This works and assigns the B1 value to "price" in the spreadsheet
my_dict = {'key': {'key2' : 'value1', 'key3' : 'value2'} 'key4' : {'key5' : 'value3', 'key6' : 'value4'}} #an example dictionary
n = len(my_dict)
for i in range(0,n):
sheet['A'+ str(i)].value = random.randint(1,10) #This does not work and gives an error
wb.save('ExampleSheet.xlsx')
OUTPUT >>> AttributeError: 'tuple' object has no attribute 'value'
The first column of pyxl, is one based, so if you modify your loop to go over range(1,n) your issues should be resolved