我目前面临的挑战是编写一些代码,这些代码执行以下操作:
接受将被指定为batchNumber
接受将被指定为batchLocation
查看名为customerData
将用户输入batchNumber
与第一列中的数据匹配
将batchLocation
写入第三列中与batchNumber
相邻的单元格
我认为这比我想象的要容易得多,但我似乎在兜圈子
excel文档的结构为
Batch Number Product Type Location
1234 Guitar *Blank*
2345 Drums *Blank*
我尝试过从excel部分修改ATBS代码,但似乎无法让它与用户输入一起工作,而不是使用字典中的数据。代码运行时没有错误,但除非数据出现在第一行,否则不会进行更改?我也尝试过使用iter_rows()
,但没有太大的成功
import openpyxl
wb = openpyxl.load_workbook('customerData.xlsx')
sheet = wb['SalesOrders']
# User Input of data
CUSTOMER_UPDATE = {}
batchNumber = str(input('Please scan the batch number: '))
batchLocation = input('Please scan the location: ')
CUSTOMER_UPDATE[batchNumber] = batchLocation
# Loop through the rows and update the location
for rowNum in range(2, sheet.max_row): #Used to skip the first row as it is only a header.
productName = sheet.cell(row=rowNum, column=1).value
if productName in CUSTOMER_UPDATE:
sheet.cell(row=rowNum, column=3).value = CUSTOMER_UPDATE[productName]
wb.save('customerDataCopy.xlsx')
我认为您需要为每一行测试
productName
,因此您需要在for
循环下缩进if
,如下所示:相关问题 更多 >
编程相关推荐