I hope, it helps you in understanding how to read excel file, try to correctly specify your file path. In my case ./ means current file where my python file exist. Move your excel file where your python file exist
Install:
pip install pandas openpyxl
Solution 1
import pandas as pd
df = pd.read_excel('./TfidfVectorizer_sklearn.xlsx')
df
Solution 2
import openpyxl
book = openpyxl.load_workbook('./TfidfVectorizer_sklearn.xlsx')
sheet = book.active
cells = sheet['A1': 'D5']
for c1, c2, c3, c4 in cells:
print(f"{c1.value} {c2.value} {c3.value} {c4.value}")
Answer from Muhammad Ali on Stack OverflowHow to read excel sheet data in python - Stack Overflow
How can I open an Excel file in Python? - Stack Overflow
Fastest Way to Read Excel in Python
Read excel data and performing calculation
You can use pandas package.
import pandas as pd
You can pass the sheet name as a parameter to pandas.read_excel():
file_name = # path to file + file name
sheet = # sheet name or sheet number or list of sheet numbers and names
df = pd.read_excel(file_name, sheet_name=sheet)
print(df.head()) # print first 5 rows of the dataframe
If you're working with an Excel file with a single sheet, you can simply use:
df = pd.read_excel(file_name)
print(df.head())
Or, when you are working with an excel file with multiple sheets, you can use pandas.ExcelFile:
xl = pd.ExcelFile(file_name)
xl.sheet_names
# > [u'Sheet1', u'Sheet2', u'Sheet3']
df = xl.parse("Sheet1")
df.head()
Try the xlrd library.
[Edit] - from what I can see from your comment, something like the snippet below might do the trick. I'm assuming here that you're just searching one column for the word 'john', but you could add more or make this into a more generic function.
from xlrd import open_workbook
book = open_workbook('simple.xls',on_demand=True)
for name in book.sheet_names():
if name.endswith('2'):
sheet = book.sheet_by_name(name)
# Attempt to find a matching row (search the first column for 'john')
rowIndex = -1
for cell in sheet.col(0): #
if 'john' in cell.value:
break
# If we found the row, print it
if row != -1:
cells = sheet.row(row)
for cell in cells:
print cell.value
book.unload_sheet(name)