简体   繁体   English

我希望能够从python中找到excel中的值并在下一列中打印该值

[英]I want to be able to locate a value in excel from python and print the value in the next column

I am trying to be able to let a user give me a value in python and then the code finds the value in an excel document and prints the value from the next column. 我试图让用户在python中给我一个值,然后代码在excel文档中找到该值并从下一列中打印该值。

I can't seem to find any code for it and i am struggling to write it myself, here is my code so far: 我似乎找不到任何代码,而我正在努力自己编写它,到目前为止,这是我的代码:

 dotill = input("would you like to enter till mode?")
 if dotill == "yes":
     barcode = input("please start scanning")
     find (bardcode) in:
         excel_document = openpyxl.load_workbook('scanza.xlsx',data_only=True)
          heet = excel_document['completetransaction'] 

Can anyone help? 有人可以帮忙吗?

You might want to have a look into the pandas package. 您可能想看看pandas包。 It provides an interface with excel. 它提供了与excel的接口。

A part of your question could than be solved using a pandas DataFrame from your excel file that with the path PATH/TO/WORKSHEET beeing searched for the value TESTVALUE which are to replaced in the example below: 然后,可以使用来自excel文件的pandas DataFrame来解决您的问题的一部分,该文件的路径为PATH / TO / WORKSHEET beeing,搜索值TESTVALUE,该值将在以下示例中替换:

import pandas as pd

df = pd.read_excel(PATH/TO/WORKSHEET)

if TESTVALUE in df.values:
    print('Value in sheet!')
else:
    print('Value not in sheet!')

pd.read_excel(path) generates a DataFrame from an excel file. pd.read_excel(path)从excel文件生成一个DataFrame。 df.values gets you an array of the values. df.values可为您提供一个值数组。 The if statement checks for your value within the array. if语句检查数组中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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