使用 OPENPYXL 从 EXCEL 文件中提取数据

Using OPENPYXL to extract data from an EXCEL file

我有以下代码打印 excel 文件中的所有项目。我想要做的是输入一个名称,然后在电子表格中搜索该名称,并在 D 列中显示相应名称的价格。名称列在 A 列中。我在电子表格中的列是(A 列:球员,第 B:Team 列,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")

嗯,你可以这样做:

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