Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script... TypeError: 'method' object does not support item assignment

Tags:

python

This is my very first Python attempt: I am trying to take the info (API key) from the command prompt and enter it into Excel spreadsheet called "CalendarData"

here's the code in Main.py

#imports presets and scedule and team id's

 presets = pd.read_excel(fileName,sheet_name="presets")

from openpyxl import Workbook
book = Workbook()
sheet = book.active
API_key = input("What is the API key? \n")
sheet.append[presets.iloc[0,7]] = API_key   <--  "presets" is the name of the tab in the spreadsheet
book.save("CalendarData.xlsx")

Here's the error I am getting.

(env) C:\Users\Marta>python run.py What is the API key? u+QBLoEyLWGSHqDDdMk Traceback (most recent call last): File "C:\Users\Marta\run.py", line 1, in import main File "C:\Users\Marta\main.py", line 56, in sheet.append[presets.iloc[0,7]] = API_key TypeError: 'method' object does not support item assignment

like image 626
Marta Avatar asked May 05 '26 07:05

Marta


1 Answers

I am not sure I understand what you are trying to achieve here, but if you desire to insert the value of API_key to the sheet I would suggest using "append"

if you want to insert the value of API_key to a single column you should use

 sheet.append([API_key])

if API_key is a list, and you want to insert each of its values to a different column in the same row, you should first convert it to a list and then use "append" like this:

newlist = API_key.split(",")  # converting API_key to a list 
sheet.append(newlist)         #inserting it to the sheet 

if you want to change the value of a spesific cell you should use this line

sheet["A5"] = API_key # change the value of cell A5 to API_key 

in conclusion your code should look like this:

book = openpyxl.load_workbook("CalendarData.xlsx") #opening an allredy exsisting xml file
sheet = book["presets"] #selecting worksheet named "presets"
API_key = input("What is the API key? \n") 
sheet["H2"] = API_key #changing the value of a spesific cell (for example cell "H2") to "API_key"
book.save("CalendarData.xlsx")

hope I could help, in any case, if this is your first python attempt I would suggest reading the documentation of "Workbook", this package is very well documented and it helped me a lot when I first started using it https://xlsxwriter.readthedocs.io/workbook.html

like image 188
Ohad Sharet Avatar answered May 06 '26 19:05

Ohad Sharet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!