Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read specific sheets from My XLS file in Python

Tags:

python

excel

xlsx

As of now i can read EXCEL file's all sheet.

e.msgbox("select Excel File")
updated_deleted_xls = e.fileopenbox()
book = xlrd.open_workbook(updated_deleted_xls, formatting_info=True)
openfile = e.fileopenbox()
for sheet in book.sheets():
for row in range(sheet.nrows):
for col in range(sheet.ncols):
thecell = sheet.cell(row, 0)
xfx = sheet.cell_xf_index(row, 0)
xf = book.xf_list[xfx]
like image 294
user3698866 Avatar asked Sep 11 '25 20:09

user3698866


2 Answers

If you open your editor from the desktop or command line, you would have to specify the file path while trying to read the file:

import pandas as pd
df = pd.read_excel(r'File path', sheet_name='Sheet name')

Alternatively, if you open your editor in the file's directory, then you could read directly using the panda library

import pandas as pd
df = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='Title Sheet')
df1 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx',sheet_name='Transactions')
df2 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='NewCustomerList')
df3 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='CustomerDemographic')
df4 = pd.read_excel('KPMG_VI_New_raw_data_update_final.xlsx', sheet_name='CustomerAddress')
like image 167
muyiwa davis Avatar answered Sep 13 '25 11:09

muyiwa davis


Maybe Pandaswould be helpful ( the go-to package for data) :

import pandas as pd 
df = pd.read_excel('filname.xls', sheet = 0)

Edit: Since a lot of time has passed and pandas matured the arguemnts have change. So for pandas >1.0.0

import pandas as pd 
df = pd.read_excel('filname.xls', sheet_name = 0)
like image 26
Marvin Taschenberger Avatar answered Sep 13 '25 10:09

Marvin Taschenberger