如何在xlsx中输入1行数据?

问题描述 投票:1回答:1

我正在尝试创建一个程序,该程序从用户(我)获取输入,以创建客户可能想要订购的项目目录。到目前为止,它还没关系。但我似乎有一个小问题。它不是每个项目输入填充1行数据,而是填充3行。

我已经尝试过删除了

ws.write(row, col + 1, title)
ws.write(row, col + 2, cost, money)

但它只将ISBN输入写入excel文件。

#data headers
ws.write('A2', 'ISBN', bold)
ws.write('B2', 'Title', bold)
ws.write('C2', 'Cost', bold)

#data to input into cells
isbn = input('ISBN: ')
title = input('Title: ')
cost = input('Cost: ')


#starting rows and columns
row = 2
col = 0

#write it out row by row
for details in (isbn, title, cost):
    ws.write(row, col, isbn)
    ws.write(row, col + 1, title)
    ws.write(row, col + 2, cost, money)
    row += 1

#total
ws.write(row, 1, 'Total', bold)
ws.write(row, 2, '=SUM(C:C) ')

wb.close()

预期:Excel每个项目输入1行数据

实际结果:Excel每个项目输入3行数据

python user-input xlsxwriter
1个回答
0
投票

问题出在你的for循环中。您正在循环列表中的三个项目。如果您只想写一次3个单独的项目,则可以删除循环。

或者,如果你想通过将input()移动到一个循环中继续接受输入并写入它(但请确保添加一些退出循环的方法)。

© www.soinside.com 2019 - 2024. All rights reserved.