我有以下代码打印excel文件中的所有项目。我想要做的是输入一个名称,然后在电子表格中搜索此名称,并在D列中显示相应名称的价格。名称列在A列中。我在电子表格中的列是(A列) :玩家,B栏:团队,C栏:分数,D栏:成本,E栏:位置)。
我的目标是搜索玩家名称并打印此玩家的价格。
from openpyxl import load_workbook
print ("Going to execute the script")
workbook = load_workbook("LeaguePlayers.xlsx", use_iterators = True)
name = print(input("Enter player name: "))
for worksheet in workbook:
for row in worksheet.iter_rows():
for cell in row:
if(cell.value != None):
print (cell.value)
print ("End of script execution")
答案 0 :(得分:1)
嗯,你可以这样做:
player = raw_input("Enter player name")
wb = load_workbook("LeaguePlayers.xlsx")
ws = wb.active
for cell in ws.columns[0]: # get first column
if cell.value == player:
cost = cell.offset(column=4).value
print("{0} costs {1}".format(player, cost))
break