[英]Adding data in last row of excel sheet using openpyxl
I am trying to add several data from user to the last rows of several columns, i used this method:我正在尝试将用户的几个数据添加到几列的最后一行,我使用了这种方法:
firstname= input("enter your first name: ")
lastname= input("enter your last name: ")
newpass = input("enter a password: ")
newbalance = input("enter balance: ")
ws.cell(column=1,row=ws.max_row +1, value=firstname)
ws.cell(column=2,row=ws.max_row +1, value=lastname)
ws.cell(column=4,row=ws.max_row +1, value=newpass)
ws.cell(column=5,row=ws.max_row +1, value=newbalance)
wd.save(filename)
when i apply this i get an error that it prints the data at the next row of each new column, like this:当我应用它时,我收到一个错误,它会在每个新列的下一行打印数据,如下所示:
Possibly the easiest is to just create a list and append.可能最简单的方法是创建一个列表和 append。
...
firstname= input("enter your first name: ")
lastname= input("enter your last name: ")
newpass = input("enter a password: ")
newbalance = input("enter balance: ")
# ws.cell(column=1,row=ws.max_row +1, value=firstname)
# ws.cell(column=2,row=ws.max_row +1, value=lastname)
# ws.cell(column=4,row=ws.max_row +1, value=newpass)
# ws.cell(column=5,row=ws.max_row +1, value=newbalance)
# Create a list with values including string for column 3
list_append = [firstname, lastname, '', newpass, newbalance]
# 'append' will add the list to the last row of the sheet starting at the first column
ws.append(list_append)
wd.save(filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.