这是我第一次尝试 Python: 我正在尝试从命令提示符中获取信息(API key )并将其输入到名为“CalendarData”的 Excel 电子表格中
这是Main.py中的代码
#imports presets and schedule 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")
这是我遇到的错误。
(env) C:\Users\Marta>python run.py API key 是什么? u+QBLoEyLWGSHqDDdMk 追溯(最近一次通话): 文件“C:\Users\Marta\run.py”,第 1 行,位于 导入主 文件“C:\Users\Marta\main.py”,第 56 行,位于 sheet.append[presets.iloc[0,7]] = API_key TypeError: 'method' 对象不支持项目分配
最佳答案
我不确定我是否理解您在这里想要实现的目标, 但是如果你想将 API_key 的值插入到工作表中 我建议使用“追加”
如果您想将 API_key 的值插入您应该使用的单个列
sheet.append([API_key])
如果 API_key 是一个列表,并且你想将它的每个值插入同一行的不同列,你应该先将它转换为一个列表,然后使用“追加” 像这样:
newlist = API_key.split(",") # converting API_key to a list
sheet.append(newlist) #inserting it to the sheet
如果你想改变一个特定单元格的值,你应该使用这一行
sheet["A5"] = API_key # change the value of cell A5 to API_key
总而言之 您的代码应如下所示:
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")
希望能帮到你 无论如何,如果这是你第一次尝试 python,我建议你阅读“Workbook”的文档,这个包有很好的文档记录,当我第一次开始使用它时它对我帮助很大 https://xlsxwriter.readthedocs.io/workbook.html
关于Python 脚本...类型错误 : 'method' object does not support item assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72392424/