Find if a value exists in a column in Excel using python

馋奶兔 提交于 2019-11-29 12:49:24

I use xlrd all the time and it works great for me. Something like this might be helpful

from xlrd import open_workbook

def main():
    book = open_workbook('example.xlsx')
    sheet = book.sheet_by_index(0)
    collection_year_col = 2 #Just an example
    test_year = 2010
    for row in range(sheet.nrows):
        if sheet.cell(row,collection_year_col).value == test_year:
            runCode()

def runCode():
    #your code

I hope this points you in the right direction. More help could be given if the details of your problem were known.

Try using xlwings library to interface with Excel from python

example from their docs:

from xlwings import Workbook, Sheet, Range, Chart
wb = Workbook()  # Creates a connection with a new workbook
Range('A1').value = 'Foo 1'
Range('A1').value
>>> 'Foo 1'
Range('A1').value = [['Foo 1', 'Foo 2', 'Foo 3'], [10.0, 20.0, 30.0]]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!