繁体   English   中英

使用 openpyxl 在 excel 表的最后一行添加数据

[英]Adding data in last row of excel sheet using openpyxl

我正在尝试将用户的几个数据添加到几列的最后一行,我使用了这种方法:

            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)

当我应用它时,我收到一个错误,它会在每个新列的下一行打印数据,如下所示:

Excel 数据表照片

可能最简单的方法是创建一个列表和 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM